My code freezes SMBX

Post here for help and support regarding LunaLua and SMBX2's libraries and features.

Moderator: Userbase Moderators

Locus
Fighter Fly
Fighter Fly
Posts: 35
Joined: Sun Mar 17, 2019 12:47 pm
Pronouns: he/him

My code freezes SMBX

Postby Locus » Thu Dec 24, 2020 9:12 am

Hello, I was recently trying to write some lua and managed to get some stuff working. I made it so that once the player hits a goomba the player dies but I also made it so that if a red Goomba is killed they hide the barrier layer. For reference, here is my code:

Code: Select all

 local GOOMBA_ID = 1
 local REDGOOMBA_ID = 2
 local flag = false

function onStart()

end


function onTick()
   Text.print(flag, 100, 100)
end

function onNPCHarm(eventToken, harmedNPC, killReason, culpritOrNil)
    if harmedNPC.id == GOOMBA_ID and killReason == HARM_TYPE_JUMP then
           player:kill() 
        else if harmedNPC.id == GOOMBA_ID and killReason == HARM_TYPE_SPINJUMP then
            player:kill()
            else if harmedNPC.id == REDGOOMBA_ID and killReason == HARM_TYPE_JUMP then
                flag = true
            end
        end
    end
end


function onEvent(eventName)
    if eventName == "barrier" then
        if flag == true then
            triggerEvent("barrier")
        end
    end
end

The strange thing is that the moment I kill the red Goomba the layer hides but immediately the game freezes. If somebody would be able to help out I would greatly appreciate it.

Hoeloe
Phanto
Phanto
Posts: 1465
Joined: Sat Oct 03, 2015 6:18 pm
Flair: The Codehaus Girl
Pronouns: she/her

Re: My code freezes SMBX

Postby Hoeloe » Thu Dec 24, 2020 10:11 am

Code: Select all

function onEvent(eventName)
    if eventName == "barrier" then
        if flag == true then
            triggerEvent("barrier")
        end
    end
end
This is why. The onEvent function runs any time a SMBX event is triggered. Here, if you trigger the event named "barrier", it'll pass the first condition, and then if the flag variable is true (which, as an aside you should test by doing if flag then and not if flag == true then), it'll... trigger the event named "barrier"... which jumps right back into onEvent, both conditions pass, and it... triggers the event named "barrier", which jumps right back into onEvent, both conditions pass, and it... you get the idea. You've created an infinite loop - every time flag is true and you trigger "barrier" once, it triggers itself again, which repeats infinitely, freezing your game.


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 4 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari