Factorio events not always fired the same way?
Posted: Fri Jun 17, 2016 9:00 pm
How exactly are events fired in Factorio? I've been working on a mod for a while and the experience with my test mod is that I'm amazed that any mod works at all in factorio. 
My mod listens for these events: on_init, on_load, on_tick, on_new_player.
New Game -> Everything is fine. on_init, on_load, on_tick, on_new_player are fired and the game variable exists and my mod's button that is created in the on_new_player event appears in the top bar just fine.
Load saved game previously without mod installed. on_init, on_load, on_tick are fired but on_new_player is not. This isn't a big deal and explains why all mods duplicate gui creation code between on_load and on_new_player. Easily fix in my mod.
Load save game previously with mod installed. on_init, on_load, on_tick are fired but on_new_player is not. Same reason as before. Now for the frustrating part. the "game" variable is suddenly nil for on_init and on_load but on_tick is fine.
OK. So I check if game is nil first. Well now when loading a game that was saved with my mod installed there are no errors but no mod button and no "[debug] whatever" messages. My mod might as well not exist.

My mod listens for these events: on_init, on_load, on_tick, on_new_player.
New Game -> Everything is fine. on_init, on_load, on_tick, on_new_player are fired and the game variable exists and my mod's button that is created in the on_new_player event appears in the top bar just fine.
Load saved game previously without mod installed. on_init, on_load, on_tick are fired but on_new_player is not. This isn't a big deal and explains why all mods duplicate gui creation code between on_load and on_new_player. Easily fix in my mod.
Load save game previously with mod installed. on_init, on_load, on_tick are fired but on_new_player is not. Same reason as before. Now for the frustrating part. the "game" variable is suddenly nil for on_init and on_load but on_tick is fine.
Code: Select all
Unknown key:"Error while running the on_load: __CTest__/Ctest.lua:15: attempt to index global 'game' (a nil value)"