Make it an error to access global outside of events

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
Honktown
Smart Inserter
Smart Inserter
Posts: 1025
Joined: Thu Oct 03, 2019 7:10 am
Contact:

Make it an error to access global outside of events

Post 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.
I have mods! I guess!
Link

Pi-C
Smart Inserter
Smart Inserter
Posts: 1644
Joined: Sun Oct 14, 2018 8:13 am
Contact:

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

Post 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.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2530
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

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

Post 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.
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

Honktown
Smart Inserter
Smart Inserter
Posts: 1025
Joined: Thu Oct 03, 2019 7:10 am
Contact:

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

Post 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.
I have mods! I guess!
Link

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2530
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

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

Post 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.
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

Post Reply

Return to “Modding interface requests”