Matrix Games Forums

Forums  Register  Login  Photo Gallery  Member List  Search  Calendars  FAQ 

My Profile  Inbox  Address Book  My Subscription  My Forums  Log Out

Scope of Lua Script

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
  Printable Version
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> Scope of Lua Script Page: [1]
Login
Message << Older Topic   Newer Topic >>
Scope of Lua Script - 2/9/2019 3:06:16 AM   
Primarchx


Posts: 3006
Joined: 1/20/2013
Status: online
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?
Post #: 1
RE: Scope of Lua Script - 2/9/2019 3:38:33 AM   
KnightHawk75

 

Posts: 203
Joined: 11/15/2018
Status: offline

quote:

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:
--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'.

(in reply to Primarchx)
Post #: 2
RE: Scope of Lua Script - 2/9/2019 3:46:37 AM   
Primarchx


Posts: 3006
Joined: 1/20/2013
Status: online
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!

(in reply to KnightHawk75)
Post #: 3
RE: Scope of Lua Script - 2/9/2019 4:12:29 AM   
KnightHawk75

 

Posts: 203
Joined: 11/15/2018
Status: offline
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). ;)

(in reply to KnightHawk75)
Post #: 4
RE: Scope of Lua Script - 2/9/2019 4:13:08 AM   
KnightHawk75

 

Posts: 203
Joined: 11/15/2018
Status: offline
Ah never mind... typed #4 before seeing your response. ;)

(in reply to KnightHawk75)
Post #: 5
RE: Scope of Lua Script - 2/9/2019 7:10:55 AM   
michaelm75au


Posts: 12629
Joined: 5/5/2001
From: Melbourne, Australia
Status: offline
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.
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

(in reply to KnightHawk75)
Post #: 6
RE: Scope of Lua Script - 2/9/2019 2:31:11 PM   
Primarchx


Posts: 3006
Joined: 1/20/2013
Status: online
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.

(in reply to michaelm75au)
Post #: 7
RE: Scope of Lua Script - 2/10/2019 6:11:36 AM   
KnightHawk75

 

Posts: 203
Joined: 11/15/2018
Status: offline
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.


(in reply to Primarchx)
Post #: 8
Page:   [1]
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> Scope of Lua Script Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts


Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI

0.113