I'm working to update older mods for 2.1, and have found a number of instances of warnings in luacheck like:
mutating non-standard global variable data
accessing undefined variable data
In both cases, the relevant lines look like:
data.raw["cargo-pod"]["cargo-pod"].created_effect = {
...
data:extend({
My understanding of Lua leads me to think that these mods are referencing the global variable "data", and changing the code to directly reference global variables clears the error:
_G.data.raw
...
_G.data.extend
But it feels incorrect to do everything in globals directly. I read https://lua-api.factorio.com/latest/aux ... orage.html and I'm wondering if I could instead shift these mods to use the storage variable instead? Would that be better? What would that look like?
LUA warnings on older mods
Re: LUA warnings on older mods
data.raw and data:extend always (and only) exist in the Data stage during game startup. They are defined by the game engine before the mod's data.lua runs inside the shared Data stage Lua context, and persist while base and all mods run data.lua, data-updates.lua, and data-final-fixes.lua. Luacheck is giving you false warning because it doesn't know that. The code is correct and changing it to _G.data would just add clutter.
storage only exists during the Control stage, in the per-mod runtime sandbox context. It is defined by the game engine when the mod's runtime context is created for control.lua. It is not correct to use storage during the Data stage.
storage only exists during the Control stage, in the per-mod runtime sandbox context. It is defined by the game engine when the mod's runtime context is created for control.lua. It is not correct to use storage during the Data stage.
My mods: Multiple Unit Train Control, RGB Pipes, Shipping Containers, Rocket Log, Smart Artillery Wagons.
Maintainer of Auto Deconstruct, Cargo Ships, Vehicle Wagon, Honk, Shortwave.
Maintainer of Auto Deconstruct, Cargo Ships, Vehicle Wagon, Honk, Shortwave.
Re: LUA warnings on older mods
Fantastic detail and explanation! Thank you very much!

