accessing global table from outside of the mod

Place to get help with not working mods / modding interface.
Post Reply
robertpaulson
Long Handed Inserter
Long Handed Inserter
Posts: 92
Joined: Sun Jun 18, 2017 2:21 pm
Contact:

accessing global table from outside of the mod

Post by robertpaulson »

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

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: accessing global table from outside of the mod

Post by DaveMcW »

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)

robertpaulson
Long Handed Inserter
Long Handed Inserter
Posts: 92
Joined: Sun Jun 18, 2017 2:21 pm
Contact:

Re: accessing global table from outside of the mod

Post by robertpaulson »

DaveMcW 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)
nice, huge help! ty vm

siniox
Burner Inserter
Burner Inserter
Posts: 8
Joined: Fri Feb 01, 2019 2:19 pm
Contact:

Re: accessing global table from outside of the mod

Post by siniox »

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:

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
})
mod B:

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)
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

Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

Re: accessing global table from outside of the mod

Post by Choumiko »

There is a difference between

Code: Select all

function some_event(event)
  global.something = 10
end
and

Code: Select all

Something = {}
Something.somethingelse = 10
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

Post Reply

Return to “Modding help”