Posted 24 April 2013 - 01:14 PM
I am somewhat new to the whole CC scene and Lua in general. but I thought I would share something I found that makes life very easy, this is the use of local versus global variables.
Now, if you've done your homework and looked at the variables section of the wiki you see a somewhat cryptic comment about what local variables do, but (if your like me) it doesn't really explain much.
basically, lua will keep track of a huge amount of data using the "global" table _G (I think that's the name) that is referenced and written to for global variables. so if you set a global variable in one program, run it. and then try to use a different variable in another program, you'll get what was set in the first. This can be VERY VERY BAD. So we use local variables, that stay just long enough for the program to use them, before going away so that they don't screw with anything later.
There is some other fun things with Local variables shown in the script below:
if you look at the functions i1() and i2() you see only two differences, the value it changes, and one calls k as a local variable. What this means is that when the program runs it
1) sets "k" to the value 1 for this program only
2) when i1() runs, it increases the value of "k" by one for the entire program
3) when i2() runs the first time, it references "k" used for the whole program, but only changes "k" for this single run of the program
4) when i1() runs again, it references the outer "k", not the one made by step 3
5) when i2() runs again, it again references the outer "k" from step 4, not the "k" from step 3, even though it is the same function.
This can be very useful when making pathfinding programs or alternative movement apis. Basically anytime that you need to reference and change a value, but only for a certain function, without needing to duplicate it.
Now, if you've done your homework and looked at the variables section of the wiki you see a somewhat cryptic comment about what local variables do, but (if your like me) it doesn't really explain much.
basically, lua will keep track of a huge amount of data using the "global" table _G (I think that's the name) that is referenced and written to for global variables. so if you set a global variable in one program, run it. and then try to use a different variable in another program, you'll get what was set in the first. This can be VERY VERY BAD. So we use local variables, that stay just long enough for the program to use them, before going away so that they don't screw with anything later.
There is some other fun things with Local variables shown in the script below:
Spoiler
local k = 1 --Sets local variable "k" to 1
function i1()
k = k + 1 --Changes the "k" by +1
return k --[[Tells it to implement the change
in k when asks to print or needed to be used ]]
end
function i2()
local k = k + 2 --[[Changes "k" by +2 for i2()
only]]
return k
end
print(k.. ", current variable k") -->1
print(i1().. ", k after i1") -->2
print(k.. ", current variable k") -->2
print(i2().. ", k after i2") -->4
print(k.. ", current variable k") --[[ >2 , note
i2 didn't change it.]]
print(i1().. ", k after i1") -->3
print(k.. ", current variable k")-->3
if you look at the functions i1() and i2() you see only two differences, the value it changes, and one calls k as a local variable. What this means is that when the program runs it
1) sets "k" to the value 1 for this program only
2) when i1() runs, it increases the value of "k" by one for the entire program
3) when i2() runs the first time, it references "k" used for the whole program, but only changes "k" for this single run of the program
4) when i1() runs again, it references the outer "k", not the one made by step 3
5) when i2() runs again, it again references the outer "k" from step 4, not the "k" from step 3, even though it is the same function.
This can be very useful when making pathfinding programs or alternative movement apis. Basically anytime that you need to reference and change a value, but only for a certain function, without needing to duplicate it.