Page 1 of 1

game.active_mods crashes when loading 0.17 world (in 0.18)

Posted: Tue Jun 23, 2020 1:27 pm
by GOKOP
So in my mod I want to check if alien biomes is active:

Code: Select all

if game.active_mods["alien-biomes"] then
This is run in control.lua a function that's called in on_init and on_load. When I load a world last saved in 0.18, everything is fine. But when I load a world last saved in 0.17, I get this error:

Code: Select all

Error while running event my-mod::on_load()
__my-mod__/control.lua:25: attempt to index global 'game' (a nil value)
And the stack trace points to the line shown in the code snippet.

Re: game.active_mods crashes when loading 0.17 world (in 0.18)

Posted: Tue Jun 23, 2020 1:47 pm
by Pi-C
GOKOP wrote: Tue Jun 23, 2020 1:27 pm So in my mod I want to check if alien biomes is active:

Code: Select all

if game.active_mods["alien-biomes"] then
This is run in control.lua a function that's called in on_init and on_load. When I load a world last saved in 0.18, everything is fine. But when I load a world last saved in 0.17, I get this error:

Code: Select all

Error while running event my-mod::on_load()
__my-mod__/control.lua:25: attempt to index global 'game' (a nil value)
And the stack trace points to the line shown in the code snippet.
Strange that it works in 0.18! The following is from the decription of the the data lifecycle:
During the script.on_load() event handler access to the game table is not available. This handler is meant for only 3 things:

1. Re-setup meta-tables. Meta-tables are not persisted through save-load.
2. Re-setup conditional event handlers (subscribing to an event only when some condition is true to save processing time).
3. Create local references to data stored in the global table

Attempting to change the contents of the global table during the script.on_load() event handler is not allowed. Doing so can lead to desyncs if the mod is used in multiplayer and will generate an error if the game detects it has been changed in any way.
How about storing a flag? Put this in on_init and on_configuration_changed:

Code: Select all

global.alien_biomes = game.active_mods["alien-biomes"] and true or false
Later, you only need to check

Code: Select all

if global.alien_biomes then … end

Re: game.active_mods crashes when loading 0.17 world (in 0.18)

Posted: Tue Jun 23, 2020 2:01 pm
by Bilka
https://lua-api.factorio.com/latest/Lua ... ctive_mods can be used in on_load in 0.18, it is not available in 0.17.

Re: game.active_mods crashes when loading 0.17 world (in 0.18)

Posted: Tue Jun 23, 2020 2:15 pm
by GOKOP
Pi-C wrote: Tue Jun 23, 2020 1:47 pm Strange that it works in 0.18! The following is from the decription of the the data lifecycle:
Actually I haven't been saving any games while testing so it's not impossible that on_load was never called. I'm new to modding this game and I'm not too sure when it is called and when it's not.

Anyway, I've put that check in a separate function that runs in on_init and on_configuration_changed, sets a variable in global and then I check it in on_load. It works! Thanks
Bilka wrote: Tue Jun 23, 2020 2:01 pm https://lua-api.factorio.com/latest/Lua ... ctive_mods can be used in on_load in 0.18, it is not available in 0.17.
That's also good to know, thanks!