Page 16 of 32

Re: Need help with lua? - LunaLua General Help

Posted: Wed Feb 10, 2021 4:46 am
by Emral
t[player.character] returns the object at that index, so in your case { obj1 = Graphics.loadImage(Misc.resolveFile("modernhud-2.png")) } if the player is mario, and nil in every other scenario.
So you can access obj1 as part of t[player.character] if the player is mario.

Re: Need help with lua? - LunaLua General Help

Posted: Wed Feb 10, 2021 4:50 am
by Marioman2007
Enjl wrote:
Wed Feb 10, 2021 4:46 am
nil in every other scenario.
So you can access obj1 as part of t[player.character] if the player is mario.
What are the other scenarios.

Re: Need help with lua? - LunaLua General Help

Posted: Wed Feb 10, 2021 6:59 am
by Emral
marioman2007 wrote:
Wed Feb 10, 2021 4:50 am
Enjl wrote:
Wed Feb 10, 2021 4:46 am
nil in every other scenario.
So you can access obj1 as part of t[player.character] if the player is mario.
What are the other scenarios.
If the player is not mario. Because you only have one table entry for when player.character is mario (CHARACTER_MARIO).

Re: Need help with lua? - LunaLua General Help

Posted: Wed Feb 10, 2021 7:24 am
by Marioman2007
Still can't understand :(

Re: Need help with lua? - LunaLua General Help

Posted: Thu Feb 11, 2021 5:50 am
by Marioman2007
Hey! Enjil, this issue is taking lots of pages, so let's take it to here.

Re: Need help with lua? - LunaLua General Help

Posted: Sat Feb 20, 2021 8:35 am
by BlueStaggo
I'm using SMBX 2.0.0b4 and I want to make it so, when I touch the goal tape (npc-197), everything goes dark, like in Super Mario World. I tried attaching a "Level - End" event to the tape's death and run this code when the event happens:

Code: Select all

function onEvent(eventName)
    if eventName == "Level - End" then
        Darkness.create{
            falloff = Darkness.falloff.HARD,
            shadows = Darkness.shadows.NONE,
            bounds = nil,
            section = -1,
            ambient = 0x000000
        }
    end
end
But it won't make the screen go dark. I've also tried using the onNPCKill event but that didn't work either. How can I make it dark when I finish the level?

Re: Need help with lua? - LunaLua General Help

Posted: Sun Feb 21, 2021 6:13 am
by Hoeloe
First thing you want to do is hit F2 and make sure your script is actually running.

Re: Need help with lua? - LunaLua General Help

Posted: Sun Feb 21, 2021 9:43 am
by BlueStaggo
Hoeloe wrote:
Sun Feb 21, 2021 6:13 am
First thing you want to do is hit F2 and make sure your script is actually running.
I've made sure that my script was running. It had other things in it like moving layers in a sine wave. Maybe I can't run code while the end sequence is running?

Re: Need help with lua? - LunaLua General Help

Posted: Sun Feb 21, 2021 8:13 pm
by Hoeloe
There's no reason why not. Have you checked that your event is triggering correctly?

Re: Need help with lua? - LunaLua General Help

Posted: Mon Feb 22, 2021 1:23 pm
by MrDoubleA
BlueStag wrote:
Sat Feb 20, 2021 8:35 am
I'm using SMBX 2.0.0b4 and I want to make it so, when I touch the goal tape (npc-197), everything goes dark, like in Super Mario World. I tried attaching a "Level - End" event to the tape's death and run this code when the event happens:

Code: Select all

function onEvent(eventName)
    if eventName == "Level - End" then
        Darkness.create{
            falloff = Darkness.falloff.HARD,
            shadows = Darkness.shadows.NONE,
            bounds = nil,
            section = -1,
            ambient = 0x000000
        }
    end
end
But it won't make the screen go dark. I've also tried using the onNPCKill event but that didn't work either. How can I make it dark when I finish the level?
I've already made a more accurate goal tape as a part of my NPC pack (;

Re: Need help with lua? - LunaLua General Help

Posted: Wed Feb 24, 2021 4:39 am
by Suckerman7 GM
I need help about a code I'm gonna use for my project.

So, I'm trying to make a sound effect play whenever I trigger an achievement with the activation of an event. It works with one event, like the code is shown below:

Code: Select all

function onEvent(eventname)
    if eventname == "King Bob-omb Defeated" then    
        Audio.playSFX("achievement.ogg")
    end
end
What I want to do is to trigger this code with more than one event. Is there a way to expand this code by adding multiple events into the eventname variable, or do I have to duplicate this code multiple times for each individual event?

Re: Need help with lua? - LunaLua General Help

Posted: Wed Feb 24, 2021 5:52 am
by MrDoubleA
Suckerman7 GM wrote:
Wed Feb 24, 2021 4:39 am
I need help about a code I'm gonna use for my project.

So, I'm trying to make a sound effect play whenever I trigger an achievement with the activation of an event. It works with one event, like the code is shown below:

Code: Select all

function onEvent(eventname)
    if eventname == "King Bob-omb Defeated" then    
        Audio.playSFX("achievement.ogg")
    end
end
What I want to do is to trigger this code with more than one event. Is there a way to expand this code by adding multiple events into the eventname variable, or do I have to duplicate this code multiple times for each individual event?
You can do

Code: Select all

if eventname == "King Bob-omb Defeated" or eventname == "my other event" or eventname == "or even a third event!" then

Re: Need help with lua? - LunaLua General Help

Posted: Wed Feb 24, 2021 8:57 am
by Suckerman7 GM
MrDoubleA wrote:
Wed Feb 24, 2021 5:52 am
Suckerman7 GM wrote:
Wed Feb 24, 2021 4:39 am
I need help about a code I'm gonna use for my project.

So, I'm trying to make a sound effect play whenever I trigger an achievement with the activation of an event. It works with one event, like the code is shown below:

Code: Select all

function onEvent(eventname)
    if eventname == "King Bob-omb Defeated" then    
        Audio.playSFX("achievement.ogg")
    end
end
What I want to do is to trigger this code with more than one event. Is there a way to expand this code by adding multiple events into the eventname variable, or do I have to duplicate this code multiple times for each individual event?
You can do

Code: Select all

if eventname == "King Bob-omb Defeated" or eventname == "my other event" or eventname == "or even a third event!" then
Thanks!

Re: Need help with lua? - LunaLua General Help

Posted: Sat Feb 27, 2021 5:47 am
by AAguy11
I think this may be a problem with SMBX and not my code (I'll share it anyway), but it seems like SaveData isn't saving data. I'm trying to save an integer to SaveData indexed by the filename. The number is increased by 1 everytime a player hits a block, which triggers an event.
The code runs just fine, but the data never saves, it always resets back to 0. I also tested to make sure it was the saving that was the problem, and sure enough, it was.

Here's the code:

Code: Select all

SaveData[Level.filename()] = SaveData[Level.filename()] or {}
local hits = SaveData[Level.filename()].BlockHits
hits = hits or 0


function updateSign()
	for _, sign in ipairs(NPC.get(151, player.section)) do
		sign.msg = "Times you have hit the infinite block: "..tostring(hits)
    end
end


function onStart()
	Misc.dialog(SaveData[Level.filename()].BlockHits) -- ?? Always 0
	Misc.dialog(hits) -- ?? Always 0
	updateSign()
end

function onEvent(eventName)
    if eventName == "Restore" then
		hits = hits + 1
		updateSign()
    end
end

Re: Need help with lua? - LunaLua General Help

Posted: Sat Feb 27, 2021 11:36 am
by Hoeloe
You never update the value in SaveData. When you use the variable "hits" you're copying the value, not a reference to the SaveData location. The "hits" variable is only in your lua file, and has no "memory" of where it gets its values from. When you assign "SaveData[Level.filename()].BlockHits" to it, all you're doing is copying that value over, and when you then update "blockhits", you're just updating "blockhits", not the SaveData value, since "blockhits" doesn't keep track of where its data comes from.

Note that this is NOT the case if you're copying a table over. Tables are stored by reference, which means they aren't "copied" wholesale, but a reference to them is. Numbers and other values, however, are not copied by reference. So, for example:

Code: Select all

local v = SaveData.myValue
v = v or 0
v = v+1
This will NOT update the value in SaveData.

Code: Select all

SaveData.myTable = SaveData.myTable or {}
local t = SaveData.myTable
t.x = 4
This WILL update the value in SaveData. (Note that the "or {}" is done when assigning to SaveData directly - that part is important.)

Re: Need help with lua? - LunaLua General Help

Posted: Sat Feb 27, 2021 8:57 pm
by DB12
When i use handycam.lua, the hud is shrunken and when i drop a powerup it drops in the wrong area

This is the code i used

Code: Select all

local handycam = require("handycam")
handycam[1].zoom = 1.5625
handycam[2].zoom = 1.5625
Also im new, so if this is the wrong place to put this i apologize

Re: Need help with lua? - LunaLua General Help

Posted: Sun Feb 28, 2021 8:35 am
by Hoeloe
What do you mean "the hud is shrunken"? The hud shouldn't be affected by handycam at all. As for reserve powerups - yes those will drop where the base game expects them to. You can fix that with some lua, however.

Re: Need help with lua? - LunaLua General Help

Posted: Sun Feb 28, 2021 8:53 am
by AAguy11
Hoeloe wrote:
Sat Feb 27, 2021 11:36 am
You never update the value in SaveData. When you use the variable "hits" you're copying the value, not a reference to the SaveData location. The "hits" variable is only in your lua file, and has no "memory" of where it gets its values from. When you assign "SaveData[Level.filename()].BlockHits" to it, all you're doing is copying that value over, and when you then update "blockhits", you're just updating "blockhits", not the SaveData value, since "blockhits" doesn't keep track of where its data comes from.
Ah okay, thanks for explaining it, this kind of stuff, "Does it copy the value or is it referencing that thing?" confuses me. Fixed it and got the saving working.

Re: Need help with lua? - LunaLua General Help

Posted: Sun Feb 28, 2021 9:21 am
by Hoeloe
It's in part to do with how complicated the data is. Basically, everything that is a table (i.e. any object that can have "fields" or "indices") is far too complex to be just copying around, so instead of storing the object directly, the program makes one copy of it, and then stores the location of that object in the variable. That location is then treated like a normal variable and copied around wherever needed. But, because it's just the location of the object, any changes made to the object itself are reflected in all of the variables storing it, because it changes the object at that location.

I like to think of it like cardboard boxes. Each variable is a box, and inside the box is a piece of paper with a value written on it. For numbers, strings, booleans, etc. when you write something like local v = myVar, you can picture that as picking up a new box, labelling it "v", then finding the box named "myVar", opening it, copying down what's written on the paper inside, and placing that copy in the new box. Now, "v" and "myVar" have the same value, but if something then changes what's written in myVar it's not going to affect what's written in v and vice versa.

For tables, what's written on the paper is more like an address, so if you do something like myTable.x = 4, you first open the box labelled "myTable", then read the address written on the paper in there, follow the address to a new room. This room is itself filled with its own set of boxes. You then find the box in that room labelled "x" (or make a new one if you can't find it), write "4" on a piece of paper, and put it in that box.

This is a little awkward to explain, but if you can get your head around the metaphor it might help explain a few concepts to you.

Re: Need help with lua? - LunaLua General Help

Posted: Tue Mar 02, 2021 6:59 am
by Goldenemerl64
I'm having problems with Activating auto-scroll on a Collectable Trigger.

Code: Select all

local autoscroll = require("autoscroll") 
function onEvent(Fast)
    if eventName == "Fast" then 
    autoscroll.scrollRight(3.0)
    elseif eventName == "Fast" then
    autoscroll.scrollRight(3.0)
 
    end
end
am I doing it correctly?