PDA

View Full Version : When are globals really globals?



Minty23185Fresh
August 1st, 2016, 17:34
I have defined four "constants" to be used throughout the lua script files in my extension. They're just names for numeric indicies into some tables. I defined them thus in one of the files:


CTEV_VSBL = 0;
CTEV_GM = 1;
CTEV_SELF = 2;
CTEV_TRGT = 3;


Within the file in which they're defined they work fine. In another file, the interpreter thinks they're nil. I get addition with nil or concatination with nil errors when they're used in the other file.
Example:


sDesc = sDesc .. " " .. goodFile.getTableString(CTEV_GM);


If I Debug.console( ) the CTEV_GM value in the offending script the value is reported as nil as is the value returned by the getTableString( ) function. So the concatenation throws an error.

I thought it might be a preparsing or load order issue with the files so I made sure the file with the "constants" is listed first in my extension.xml file. No help!

To ensure my code is functioning I localized the "constants" to both files, I just defined the "constants" in each file, thus:


local CTEV_VSBL = 0;
local CTEV_GM = 1;
local CTEV_SELF = 2;
local CTEV_TRGT = 3;


Then everything works fine!

Any ideas why my exposed CTEV_... variables are failing to act as global variables?

Moon Wizard
August 1st, 2016, 20:04
There are no global variables within the FG Lua context. Each script for each package, window or control runs in its own scope.

The only exception is that FG API packages (such as Comm, DB, ...) and any ruleset defined global packages (such as ActionsManager, EffectManager, ...) are accessible from all scripts.

So to mimic global variables, you would define the variables without the local keyword in a ruleset defined global package. Then, you access the variable by referencing the variable in the global package.

Regards,
JPG

LordEntrails
August 2nd, 2016, 00:11
I always enjoy these posts. It's like listening to two people speaking in Martian :)

Minty23185Fresh
August 2nd, 2016, 06:41
It's actually Luatian, a highly specific and reduced subset of Martian.. :o

And once again, thanks Moon Wizard. As usual I misread or misunderstood the mod docs and/or ref docs!