lua question

lua question

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

lua question

Post by smoth »

file.lua

Code: Select all

local choices	= {	"choice1", "choice2", "choice3",}
-----------------------------------------------------------
-- Retrieves bucketChoice table
-----------------------------------------------------------
function Choices()
	return choices
end
gadget.lua

Code: Select all

VFS.Include('file.lua')

local choices = Choices()
Now, it does pass the table, I know this much.

however, is it passing a reference or is it passing the value when I get the return in gadget.lua? Because my understanding is localizing creates a limited scope or does including flie.lua mean that it has a local choices as part of the gadget? The reason I ask is because I cannot access choices without creating a function to return it(WHICH imo is a good thing)

You can guess I am trying to see how I can get some kind of half assed private and public stuff going. (in before lua OO, I am still not conviced that use of metatables is needed)
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: lua question

Post by gajop »

Most likely reference as that's how tables are passed in lua, hiding the variable doesn't matter, as you are not referencing _G["choices"] (which doesn't exist), but the value of the variable set as the result of Choices().
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: lua question

Post by jK »

Code: Select all

local t = {1,2,3}
function GetReference()
  return t
end

function GetNew()
  return {1,2,3}
end
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: lua question

Post by smoth »

so it is returning just the value(s)? or a reference? It looks like it is returning the reference.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: lua question

Post by FLOZi »

Yes reference.
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: lua question

Post by smoth »

So any function that I localize doesn't get "pulled in" with the non localized functions on an include?

How does this scoping work? because obviously the functions can access the variables inside of file.lua but gadget.lua cannot without a helper function?
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: lua question

Post by jK »

TABLES ARE ALWAYS PASSED BY REFERENCE

The only thing that matters is if the table is a new created one or if it is a `cached` variable:

Code: Select all

local v = {1,2,3}
local f = function() return 1 end
local s1 = "foo"

function foo()
   local w = v --// you get the reference to v
   local g = f --// you get the reference to f

   local n = {1,2,3} --// creates a NEW table (each run of foo)
   local h = function() return 1 end --// creates a NEW function (-> damn slow operation!)

   local s2 = s1 --// both s1 & s2 point to the same memory location, but once you modify one of them, they create a new memory construct and KEEP the old one, so changes aren't copied between s2 & s1
   local s = "foobar" --// if "foobar" isn't in the internal string-cache, lua will create a new one with its content. But the string is always passed by value.
end
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: lua question

Post by smoth »

So the values returned, where is the local var that they reference? that stays alive in mem until it is garbage collected? If I alter the value returned it does *NOT* alter the original but instead creates a new version in memory? so in your s2_s1 example, I can alter s1 without altering s2?

Also, I am trying to understand how this localization scoping works with regards to includes because I am moving a tedious bit of code into a utility library and when I include said utility library. That utility library has a bunch of helper stuff that not everything needs. Does lua only pull in what it calls out of the include? does it pull in everything? To me it only seems to pull in non-localized things, functions variables etc.

If this is the case the functions that I am calling, are they non-localized? how are they treated?
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: lua question

Post by jK »

Strings are By Value.

Code: Select all

local t = {1,2,3}
local y = {4,5,6}

local w = y
w[3] = 666

Spring.Echo(y[3]) --// prints: 666

t = 1
y = nil

Spring.Echo(t, y, w) --// prints: 1, nil, table

--The table created for `t` gets freed with next GarbageCollector calling.
--The table created for `y` is still referenced by w! It won't be freed.
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: lua question

Post by smoth »

So every time a string is altered it creates a new construct(strings are immutable)?

What about the functions and their localization?
Post Reply

Return to “Lua Scripts”