Lua: Exposing globals to other states

MapModData is a predefined global that exists in all Lua states and references the same table. So put things in there as needed.

If you want to share tables between states, do this:
Code:
MapModData.myTable = MapModData.myTable or {}
local myTable = MapModData.myTable
...in each state that needs the table. This way you have a localized pointer to the same table in each state that needs it. The "or" is so you don't have to worry about which file runs first at game init.


Many of my mod's constants are needed by UI states, so I just define theM as fields in MapModData:

MapModData.MY_CONSTANT_1 = x
MapModData.MY_CONSTANT_2 = y
MapModData.MY_CONSTANT_3 = z
etc...


If you need to optimize a particular file or function, just do:
local MapModData = MapModData
(shaves off a couple microseconds...)
 
MapModData is a predefined global that exists in all Lua states and references the same table. So put things in there as needed.

If you want to share tables between states, do this:
Code:
MapModData.myTable = MapModData.myTable or {}
local myTable = MapModData.myTable
...in each state that needs the table. This way you have a localized pointer to the same table in each state that needs it. The "or" is so you don't have to worry about which file runs first at game init.

Thank you. Have you used this method in conjunction with memoization?
 
I don't really see the connection to memoization here. This is just making use of a table (provided by Firaxis) that happens to be a "super-global" already defined in all states (needed because Lua "globals" aren't really global in this sense).

You could also pass table pointers via LuaEvents to create your own "MapModData". But that's not really needed since MapModData is already there.

Do you use memoization in conjunction with tables? I've only used it once or twice in conjunction with function returns (but I'm not a serious programmer so I don't know all the ways this could be used).
 
Back
Top Bottom