LUA global variables

killmeplease

Mk Z on Steam
Joined
Nov 22, 2007
Messages
2,795
Location
Samara
hello!

there is one file where global variables are declared.
problem is, when i include this file and use these variables from other files, it seems each referring file works with its own copies of global variables

say
theres file1 where x = 1 is declared

file2 (includes file1) sets x to 10

file3 (includes file1) gets x value and it is 1

what am i doing wrong?
 
Are the include files set to VFS=true?

Also, lua sandboxes it's calls, so to really share data between two different files you'll need to use their provided method:

Code:
MapModData.g_bDebug	= MapModData.g_bDebug or true
local g_bDebug		= MapModData.g_bDebug
Where g_bDebug can be the name of anything you want to store, even tables or functions. Put that in every file you want to share the value.
 
Each "state" does have its own globals that are shared anywhere in that state. The command include(filenameA) puts fileA in the same state as the calling file, with all "globals" shared between them. So if you have file A (UIAddIn, VFS=false) with lines include(B), include(C) include(D) (all with VFS=true), then you can assign a global anywhere in these files and read it from anywhere else. All files exist only in one state in this situation.

However, reading your post literally, I think you did something different. Sounds like you created two states associated with file2 and file3 (each added as UIAddIn, VFS=false) and then included file1 from both. You can't "join" two states by including a shared file. What you've really done is create two separate instances of file1 (and all globals and locals declared in it). This is a fine thing to do and very useful for files containing "utility" functions. But don't do it as a way to share data between states. (Also, don't do anything in these multiply-shared files with UI Controls.) Use MapModData to share data between states (it's a pre-existing global in all Lua states that points to the same table).

It's simpler in most cases to make everything in your mod run in a single Lua state (so one UIAddIn and everything else included from there). There are only a few specific reasons why you might want to split off a different state, the main one being for UI. It's useful in UI to have different states because each state can have its own xml file defining graphic elements (that's how Firaxis UI lua/xml pairs work).
 
Back
Top Bottom