Page 1 of 1

Make it an error to access global outside of events

Posted: Tue Jan 10, 2023 12:51 pm
by Honktown
A common mistake I've been seeing for new modders, is that they either use the global table right away (naively assuming it's a constant for the mod) or possibly come from other modding environments, which don't use events and/or don't have the explicit behavior of Factorio's global table.

Their use initially works, as the global enters from not being present in the save, they add to it with events, and it gets saved.

Then things "break" as they reload the game, and their global table is "empty" because they're trying to work with it outside events.

Similar to the error message from accessing game: If the table were accessed outside of events, it could raise an error. A part of the message could be "global should be initialised during on_init"). Searching on_init from the API immediately brings up LuaBootstrap -> on_init, which has good information and links.

Upside: Any new modders trying to edit global will immediately be met with the proper information to not have problems moving forward.
Downside: Existing mods that initialized global outside of on_init will break.

Re: Make it an error to access global outside of events

Posted: Tue Jan 10, 2023 1:17 pm
by Pi-C
You should make an exemption for migration scripts, though. Migrations run outside of events (after script.on_load, where the global table doesn't exist yet anyways, but before script.on_configuration_changed), and quite often they are used to restructure mod data stored in the global table.

Re: Make it an error to access global outside of events

Posted: Tue Jan 10, 2023 1:19 pm
by FuryoftheStars
An exception would have to be made for on_configuration_changed, too, as maybe the mod didn't use global on first release, but a later version they added it.

Re: Make it an error to access global outside of events

Posted: Tue Jan 10, 2023 1:30 pm
by Honktown
Pi-C wrote: Tue Jan 10, 2023 1:17 pm ...
Good point, they do run in a special context, as is. They have access to game.
FuryoftheStars wrote: Tue Jan 10, 2023 1:19 pm An exception would have to be made for on_configuration_changed, too, as maybe the mod didn't use global on first release, but a later version they added it.
Hence, "should". If they didn't need global until an entity were damaged, or their entity were first made, etc. That's fine. Just some text that probably informs a person there is *this way* to do what they were probably wanting to do.

Re: Make it an error to access global outside of events

Posted: Tue Jan 10, 2023 1:44 pm
by FuryoftheStars
Honktown wrote: Tue Jan 10, 2023 1:30 pm
FuryoftheStars wrote: Tue Jan 10, 2023 1:19 pm An exception would have to be made for on_configuration_changed, too, as maybe the mod didn't use global on first release, but a later version they added it.
Hence, "should". If they didn't need global until an entity were damaged, or their entity were first made, etc. That's fine. Just some text that probably informs a person there is *this way* to do what they were probably wanting to do.
Actually, I was more thinking initial release (and some updates after) of the mod had no need of global, but then later releases added features to where they needed it. As such, they would be initializing it in on_init for those that are loading the mod for the first time, but they'd also need to do on_configuration_changed for those that were updating from older versions of the mod.