Page 1 of 1

Lifespan of globals created when using /c console command?

Posted: Sat Jul 26, 2025 2:38 am
by HeliGungir
I was looking at https://wiki.factorio.com/Console and noticed some snippets define locals while others define globals.

Eg. global:

Code: Select all

/c surface = game.player.surface
for _, ore in pairs(surface.find_entities_filtered({type="resource"})) do
    ore.amount = 10000
end
vs. local:

Code: Select all

/c local surface = game.player.surface
for _, ore in pairs(surface.find_entities_filtered({type="resource"})) do
    ore.amount = 10000
end
What is the lifespan of globals and locals created by scripts that are executed with the /c command? Is the first version bad practice?

---

Separate question: Why have a variable at all?

Code: Select all

/c for _, ore in pairs(game.player.surface.find_entities_filtered({type="resource"})) do
    ore.amount = 10000
end
Am I overlooking something? It is my understanding that pairs() is only evaluated once, returning a local table that only the For statement can interact with. The For statement iterates over the key-value pairs of the table, providing access to them as _ and ore, then the table is freed upon reaching the closing end. I'm not seeing a good reason to store game.player.surface in a variable here.

Re: Lifespan of globals created when using /c console command?

Posted: Sat Jul 26, 2025 11:49 am
by Osmo
local scope is only within that command (or less)
I don't remember whether global is permanent or lasts until you reload the save.
But all code snippets you provided do the same thing. In this case variables are optional.

Re: Lifespan of globals created when using /c console command?

Posted: Sat Jul 26, 2025 1:02 pm
by Rseding91
'global' variables persist until the map is unloaded (exit to main menu). They also won't persist if someone joins a multiplayer game in progress - a cause of multiplayer desyncs and why persistent values are supposed to be put into the 'storage' variable which does persist through save/load.