hi.
I understand that each mod has its own global table. Does anyone know how is this indexed? I'm trying to access a global table of a mod from within base scripts of the game.
i tried few things but i just can't find it, and i couldn't find anything with forum/reddit search.
thank you in advance
accessing global table from outside of the mod
-
- Long Handed Inserter
- Posts: 92
- Joined: Sun Jun 18, 2017 2:21 pm
- Contact:
Re: accessing global table from outside of the mod
Mods are sandboxed, the only way to access the global table is to use the mod's remote interface (if the mod provides it).
Code: Select all
remote.add_interface("my-mod-interface", {
get_global = function(key) return global[key] end,
set_global = function(key, value) global[key] = value end
})
local count = remote.call("my-mod-interface", "get_global", "count") or 0
remote.call("my-mod-interface", "set_global", "count", count + 1)
-
- Long Handed Inserter
- Posts: 92
- Joined: Sun Jun 18, 2017 2:21 pm
- Contact:
Re: accessing global table from outside of the mod
nice, huge help! ty vmDaveMcW wrote: Sun Nov 04, 2018 5:03 am Mods are sandboxed, the only way to access the global table is to use the mod's remote interface (if the mod provides it).
Code: Select all
remote.add_interface("my-mod-interface", { get_global = function(key) return global[key] end, set_global = function(key, value) global[key] = value end }) local count = remote.call("my-mod-interface", "get_global", "count") or 0 remote.call("my-mod-interface", "set_global", "count", count + 1)
Re: accessing global table from outside of the mod
Hello,
so, in mod A (in control.lua) there is Something.somethingelse = 10
i want, through mod B in control.lua, to overwrite the value of Something.somethingelse to 20 instead of 10
i ve tried what you wrote but i don't manage to make it work, the only code that doesn't make an error while loading the game is:
mod A:
mod B:
so the game loads without any error, but the Something.somethingelse value used in game by mod A is still 10 and not 10+10
so, in mod A (in control.lua) there is Something.somethingelse = 10
i want, through mod B in control.lua, to overwrite the value of Something.somethingelse to 20 instead of 10
i ve tried what you wrote but i don't manage to make it work, the only code that doesn't make an error while loading the game is:
mod A:
Code: Select all
Something = {}
Something.somethingelse = 10
remote.add_interface("my-mod-interface", {
get_global = function(key) return global[key] end,
set_global = function(key, value) global[key] = value end
})
Code: Select all
script.on_load(function()
local count = remote.call("my-mod-interface", "get_global", "Something.somethingelse") or 0
remote.call("my-mod-interface", "set_global", "Something.somethingelse", count + 10)
end)
Re: accessing global table from outside of the mod
There is a difference between
and
Something is a global variable, global.something is also a global variable, but it's preserved through a save/load cycle.
Your working code changes global["Something.somethingelse"] to count + 10 and does nothing to Something.somethingelse
Bunch of links:
https://lua-api.factorio.com/latest/Global.html
https://lua-api.factorio.com/latest/LuaRemote.html
Code: Select all
function some_event(event)
global.something = 10
end
Code: Select all
Something = {}
Something.somethingelse = 10
Your working code changes global["Something.somethingelse"] to count + 10 and does nothing to Something.somethingelse
Bunch of links:
https://lua-api.factorio.com/latest/Global.html
https://lua-api.factorio.com/latest/LuaRemote.html