Lifespan of globals created when using /c console command?
Posted: Sat Jul 26, 2025 2:38 am
I was looking at https://wiki.factorio.com/Console and noticed some snippets define locals while others define globals.
Eg. global:
vs. local:
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?
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.
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
Code: Select all
/c local surface = game.player.surface
for _, ore in pairs(surface.find_entities_filtered({type="resource"})) do
ore.amount = 10000
end
---
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