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.
Make it an error to access global outside of events
Make it an error to access global outside of events
I have mods! I guess!
Link
Link
Re: Make it an error to access global outside of events
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.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
-
- Smart Inserter
- Posts: 2767
- Joined: Tue Apr 25, 2017 2:01 pm
- Contact:
Re: Make it an error to access global outside of events
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.
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles | New Gear Girl & HR Graphics
Re: Make it an error to access global outside of events
Good point, they do run in a special context, as is. They have access to game.
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.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.
I have mods! I guess!
Link
Link
-
- Smart Inserter
- Posts: 2767
- Joined: Tue Apr 25, 2017 2:01 pm
- Contact:
Re: Make it an error to access global outside of events
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.Honktown wrote: Tue Jan 10, 2023 1:30 pmHence, "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.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.
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles | New Gear Girl & HR Graphics