Angry362 / Lifestyle-Mod-Project-Zomboid-

0 stars 0 forks source link

Events.OnGameStart.Remove is called during OnGameStart, causing the next registered "Start" function to be skipped #18

Closed djhert closed 11 months ago

djhert commented 11 months ago

Hello!

The MenuOverlay.lua function LSMainMenuOverlay:destroy() calls Events.OnGameStart.Remove during the OnGameStart event, which causes the next registered OnGameStart function to not be run. Calling the Events.OnGameStart.Remove function during the OnGameStart Event changes the index of the underlying array's elements, making the "next" function the current index; the next iteration then skips that function.

This is easy enough to test. Using the latest Lifestyle from Steam, add the following to the very bottom of the MenuOverlay.lua file. This will add 3 new events to OnGameStart: one before, and 2 after the LSManuMenuOverlay.

-- Setup a print for OnGameStart
local function OGSB()
    print("TEST: OnGameStart BEFORE")
end
Events.OnGameStart.Add(OGSB)

-- Setup 2 prints OnGameStart, but last
local function OGSA()
    print("TEST: OnGameStart After")
end
local function OGSAA()
    print("TEST: OnGameStart After After")
end
Events.OnGameBoot.Add(function() 
    Events.OnGameStart.Add(OGSA)
    Events.OnGameStart.Add(OGSAA)
end)

I also changed the LSMenuOnDestroy() function in MenuOverlay.lua to the following for clarity:

function LSMenuOnDestroy()
    if LSMenuremoved == true then
    return
    end
    print("LSDESTROY")
LSMainMenuOverlay:destroy()
end

When running the game, the following is output:

 LOG  : General     , 1704603585886> TEST: OnGameStart BEFORE.
 LOG  : General     , 1704603585886> LSDESTROY.
 LOG  : General     , 1704603585887> TEST: OnGameStart After After.

Observe that the text "TEST: OnGameStart After" is missing, which would have been the next event in the OnGameStart stack. By removing following line in the LSMenuOverlay:destroy() function:

Events.OnGameStart.Remove(LSMenuOnDestroy);

The log is as thus:

 LOG  : General     , 1704604328930> TEST: OnGameStart BEFORE.
 LOG  : General     , 1704604328930> LSDESTROY.
 LOG  : General     , 1704604328931> TEST: OnGameStart After.
 LOG  : General     , 1704604328931> TEST: OnGameStart After After.

Much better, all events are triggered as expected.

You do not need to remove the function from the OnGameStart event stack, as OnGameStart is only called once. If you must though, it should be done outside of the OnGameStart event. This specific event in the MenuOverlay.lua file is the final OnGameStart in base Lifestyle, so likely went unnoticed.

I know this is "rich" coming from me (Fancy Handwork and all), but I promise steps are being taken on my end to fix that now. :D

Need anything else, let me know! Can ping me in Discord (dhert) if you want as well.

Thanks!

Angry362 commented 11 months ago

Hey! I appreciate the thoughtful report and explanation, this mistake went unnoticed and I apologize for any issues it caused. Added the correction for the next update, tyvm :)

Also, I'll take you up on that offer and add you in discord, please do reach out whenever you'd like to discuss anything or just chat.