Include files and sharing data

Hambil

Emperor
Joined
Oct 16, 2006
Messages
1,100
Obviously I'm not the first person to have this issue.

File A is my file
File B is an InGame file marked VFS
I want to share data between them

Using a file C as an include doesn't work, as both A and B get a different version of file C with it's own values. I tested this using a random number generator on a variable and printing it out. Two different random numbers.

I could use save data to persist the data, but that seems like a kluge. Actually, the way this include thing works feels like a kluge to start with but, who am I?

I tried including A and B at the top of C instead. That just tossed a bunch or runtime errors...
 
You can use ModMapData (or whatever it's called - Pazyryk knows ;) ) to share data, or you can use LuaEvents to pass a table from one to the other to share data


If you need to persist that data over saves, check out TableSaveLoader from Pazyryk
 
MapModData -- it's a preset global in all Lua states that points to a single table. You can load it up with tables you need to share (and not have to worry about file run order) with something like:

Code:
MapModData.sharedTable = MapModData.sharedTable or {}
local sharedTable = MapModData.sharedTable

Whatever file happens to run first initializes the table you want to share. Each file (in different states or wherever) then has a local variable holding a pointer to that table.


Not needed for your stated situation, but I do suggest TableSaverLoader for persisting data. Others use SaveUtils, I guess because it was around since 2010 (harder to use in my opinion and it relies on some methods that Firaxis has previously killed and resurrected). DonQuiche has, I believe, proposed another solution in the wiki that sounds much more elegant than mine but is untested as far as I know.


On including files from multiple states: Works fine and I do it for many "utility" lua files. For Heaven's sake though, don't try to do anything with Context in these.
 
Back
Top Bottom