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

Place to get help with not working mods / modding interface.
User avatar
GOKOP
Burner Inserter
Burner Inserter
Posts: 11
Joined: Mon Jun 22, 2020 12:57 am
Contact:

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

Post 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.
Pi-C
Smart Inserter
Smart Inserter
Posts: 1734
Joined: Sun Oct 14, 2018 8:13 am
Contact:

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

Post 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
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
Bilka
Factorio Staff
Factorio Staff
Posts: 3310
Joined: Sat Aug 13, 2016 9:20 am
Contact:

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

Post 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.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
User avatar
GOKOP
Burner Inserter
Burner Inserter
Posts: 11
Joined: Mon Jun 22, 2020 12:57 am
Contact:

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

Post 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!
Post Reply

Return to “Modding help”