Make it an error to access global outside of events
Posted: Tue Jan 10, 2023 12:51 pm
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.
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.