PtokaX forum

Development Section => HOW-TO's => Topic started by: plop on 19 June, 2005, 18:05:06

Title: HOW-TO: locals.
Post by: plop on 19 June, 2005, 18:05:06
locals on LUA 5 are more local then on LUA 4.
here I'll try 2 explain how local they really are.
and how 2 use them in the best possible way.
like always you can execute the example scripts on the
LUA (5) command line, which can be found on my website.

--making a local table which can be accesed anywhere in the script.
local number1 = {[1]=1,[2]=2,[3]=3,[4]=4,[5]=5,[6]=6}

-- lets loop the table and stuff them in another local table.
for a,b in ipairs(number1) do
-- check if number2 excist if not make it.
if not number2 then
local number2 = {}
end
number2[a]=b
end

-- function 2 print the table.
function PrintOutcome(thetable2beprinted)
if thetable2beprinted then
for a,b in ipairs(thetable2beprinted) do
print(b)
end
else
print("error table missing")
end
end
-- now lets print all vallue's from the number2 table.
PrintOutcome(number2)
that didn't work. number2 was made but wasn't there.
why ?
if not number2 then
-- only between here and the next end number2 is around.
-- after that lua calls it garbage.
local number2 = {}
end
-- here it's gone.
lets declare it a bit sooner.
--making a local table which can be accesed anywhere in the script.
local number1 = {[1]=1,[2]=2,[3]=3,[4]=4,[5]=5,[6]=6}

for a,b in ipairs(number1) do
-- lets make a empty global
local number2
if type(number2) ~= "table" then
number2 = {}
end
number2[a]=b
end

-- function 2 print the table.
function PrintOutcome(thetable2beprinted)
if thetable2beprinted then
for a,b in ipairs(thetable2beprinted) do
print(b)
end
else
print("error table missing")
end
end
-- now lets print all vallue's from the number2 table.
PrintOutcome(number2)
now the table is still missing.
so we need to declare it even sooner.
lets do it on the start.
--making a local table which can be accesed anywhere in the script.
local number1 = {[1]=1,[2]=2,[3]=3,[4]=4,[5]=5,[6]=6}

local number2
for a,b in ipairs(number1) do
if type(number2) ~= "table" then
number2 = {}
end
number2[a]=b
end

-- function 2 print the table.
function PrintOutcome(thetable2beprinted)
if thetable2beprinted then
for a,b in ipairs(thetable2beprinted) do
print(b)
end
else
print("error table missing")
end
end
-- now lets print all vallue's from the number2 table.
PrintOutcome(number2)
now this works but number2 is still available.
copy the last line a couple times.
not a real local like we wanted.
it's as local as number1 is.
lets make it a function which returns the table.
with this we can send a local copy straight 2 the print function.
--making a local table which can be accesed anywhere in the script.
local number1 = {[1]=1,[2]=2,[3]=3,[4]=4,[5]=5,[6]=6}

function copytable(tablename)
local number2 = {} -- making the number2 table
for a,b in ipairs(tablename) do
number2[a]=b
end
return number2
end

-- function 2 print the table.
function PrintOutcome(thetable2beprinted)
if thetable2beprinted then
for a,b in ipairs(thetable2beprinted) do
print(b)
end
else
print("error table missing")
end
end
-- now lets print all vallue's from the number2 table.
PrintOutcome(copytable(number1))
now this is a nice way.
we have a local copy which is dropped right on the spot it wasn't needed anymore.
we can prove it by trying 2 print number2
add the next line at the end of the previous example script.
PrintOutcome(number2)
it's gone 100%.
hope you understand a bit how local locals really are.

have fun scripting.
plop
Title:
Post by: Dessamator on 19 June, 2005, 19:03:10
hmm, i used some of those for variables not tables, but its a nice way to do it