Scope of Lua Script

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
User avatar
Primarchx
Posts: 1946
Joined: Sun Jan 20, 2013 9:29 pm

Scope of Lua Script

Post by Primarchx »

So what's the scope of a Lua Script once the game is in session? Are variables you declare in one Script (say at Scenario Load) retained for calls by other Lua Scripts later in that game session? I'm used to having header files with lots of declared functions that once declared hang out until you need them. But in my testing I'm finding that tables I define at the start of the game are nil in other Lua Script calls. What's the scoop?
KnightHawk75
Posts: 1556
Joined: Thu Nov 15, 2018 7:24 pm

RE: Scope of Lua Script

Post by KnightHawk75 »

ORIGINAL: Primarchx

So what's the scope of a Lua Script once the game is in session? Are variables you declare in one Script (say at Scenario Load) retained for calls by other Lua Scripts later in that game session? I'm used to having header files with lots of declared functions that once declared hang out until you need them. But in my testing I'm finding that tables I define at the start of the game are nil in other Lua Script calls. What's the scoop?

There is one environment so whatever you don't declare locally or forget to declare locally is global.
Run this in console:

Code: Select all

 --global context by default
 jim = 'jim is global var available to all scripts';
 
 --local to executing script fragment no matter where it is (event\console\special action)
 local jane = 'jane is local var to our executing script fragement';
 
 function joe() --global function available to all scripts'
  print('joe');
 end
 
 local function sally() 
  print('sally');
 end
 
 function john()
   local function jill()
     print('jill');
   end
   jill();
 end
 

Then optionally run some of these individually in console or event etc.
--joe(); -- will run from anywhere\global context and print 'joe', at least till lines down below change it
--sally(); -- should fail with interpreter saying global function is nil because it was local var.
--print(jim); -- will print the string above as it's global context
--print(jane); -- will print 'nil' because it does not exist in global context.
--john(); --will print 'jill' as it's also has global context though it's internal function jill does not.
--jill(); --should fail as unknown\nil function because it is only locally defined inside inside a global func.

--function joe() --replaces old joe function with new one.
--print('new joe');
--end
--joe(); --will print 'new joe'.
User avatar
Primarchx
Posts: 1946
Joined: Sun Jan 20, 2013 9:29 pm

RE: Scope of Lua Script

Post by Primarchx »

That's what I thought. Turned out my missing tables were having issues that required some diligent '}' additions and placement. Just got a bit worried when suddenly I couldn't find tables I had loaded in a script. Thanks!
KnightHawk75
Posts: 1556
Joined: Thu Nov 15, 2018 7:24 pm

RE: Scope of Lua Script

Post by KnightHawk75 »

Some script that runs _only_ once say at start or for that matter anywhere else you create the intended global table.
{} here just represent start\stop of script for doc purposes.

{
gMyGlobalTable = {{id=1,name='one'},{id=2,name='two'}}
}

Some script executing later in say an event:
{
if (gMyGlobalTable ~=nil) and (#gMyGlobalTable) > 0 then
for k,v in ipairs(gMyGlobalTable) do -- print table
print(string.format("Table Entry %d id:%d name: %s",k,v.id,v.name ));
--print(string.format("Table Entry %d id:%d name: %s",k,v["id"],v["name"] )); --alternate1
end
else
print('TABLE was nil or empty, This should not happen unless you have erased gMyGlobalTable or cleared all its entries via other code elsewhere ');
end

}

If you find your version of gMyGlobalTable doesn't exist later then most likely you are failing to execute it first or are wiping it somehow later, I guess it's possible
there is bug if you're seeing different, but I've not yet seen that happen yet (well at least without eventually finding my problem that was causing it). ;)
KnightHawk75
Posts: 1556
Joined: Thu Nov 15, 2018 7:24 pm

RE: Scope of Lua Script

Post by KnightHawk75 »

Ah never mind... typed #4 before seeing your response. ;)
User avatar
michaelm75au
Posts: 11889
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Scope of Lua Script

Post by michaelm75au »

I am use to having variables defined locally when I write C-type code. So when I start writing Lua, it was a bit of a challenge to remember its variables are 'global' in nature if not defined as 'local'.
Now I tend to put 'local' in front of all my Lua variables, UNLESS I know that I want them to apply to the entire Lua session. Which I personally tend not to do.
Even then, I have been tripped up when a 'global' variable has actually be available between scenarios because the Lua session didn't end, or it has a value I didn't expect because some other code snippet used it without it being defined in the local code block. [:D]
Also global variable use can be a bit of a memory hog as it won't be released until the Lua session finishes or you set it to a empty value.
I can't recall if a new Lua is created for each scenario, or just the once when the game is loaded. There were memory issues from the first, because the old memory wasn't being released (garbage collected) quickly enough resulting in 'out-of-memory' for new scenario loading.
Michael
User avatar
Primarchx
Posts: 1946
Joined: Sun Jan 20, 2013 9:29 pm

RE: Scope of Lua Script

Post by Primarchx »

A thing of interest I learned is that the variable in a FOR loop is always LOCAL. I had been LOCAL declaring/initializing them before I learned that.
KnightHawk75
Posts: 1556
Joined: Thu Nov 15, 2018 7:24 pm

RE: Scope of Lua Script

Post by KnightHawk75 »

Code: Select all

I can't recall if a new Lua is created for each scenario
In testing I found it's per scenario load event. You\thecode re-init a fresh lua\nlua environment, at least in more recent builds the last couple months.

Post Reply

Return to “Lua Legion”