### Author Topic: HOW-TO: locals.  (Read 1934 times)

0 Members and 1 Guest are viewing this topic.

#### plop

• Forum God
•    • Posts: 2 464
• Karma: +37/-0 ##### HOW-TO: locals.
« 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.

Code: [Select]
`--making a local table which can be accesed anywhere in the script.local number1 = {=1,=2,=3,=4,=5,=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]=bend-- 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") endend-- now lets print all vallue's from the number2 table.PrintOutcome(number2)`that didn't work. number2 was made but wasn't there.
why ?
Code: [Select]
` 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.
Code: [Select]
`--making a local table which can be accesed anywhere in the script.local number1 = {=1,=2,=3,=4,=5,=6}for a,b in ipairs(number1) do -- lets make a empty global local number2 if type(number2) ~= "table" then number2 = {} end number2[a]=bend-- 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") endend-- 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.
Code: [Select]
`--making a local table which can be accesed anywhere in the script.local number1 = {=1,=2,=3,=4,=5,=6}local number2for a,b in ipairs(number1) do if type(number2) ~= "table" then number2 = {} end number2[a]=bend-- 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") endend-- 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.
Code: [Select]
`--making a local table which can be accesed anywhere in the script.local number1 = {=1,=2,=3,=4,=5,=6}function copytable(tablename) local number2 = {} -- making the number2 table for a,b in ipairs(tablename) do number2[a]=b end return number2end-- 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") endend-- 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.
Code: [Select]
`PrintOutcome(number2)`it's gone 100%.
hope you understand a bit how local locals really are.

have fun scripting.
plop
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

#### PtokaX forum ##### HOW-TO: locals.
« on: 19 June, 2005, 18:05:06 »

#### Dessamator

• Scripter
• Emperor
•      • Posts: 1 262
• Karma: +13/-9 ##### (No subject)
« Reply #1 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
Ignorance is Bliss.

#### PtokaX forum ##### (No subject)
« Reply #1 on: 19 June, 2005, 19:03:10 »