NickRosso Posted May 30, 2012 Share Posted May 30, 2012 I'm just a newcomer, and i'm watching many tutorials on lua.org but i don't understand one thing so far. if it's a local variable: local x = 1 end --this is the end of the variable local x = 2 -- so from now the x is 2? Then where is the end of the global variable? If there is no end of it, how to "destroy it" x = 1 -- how to destroy this? Thanks for any help Nick Link to comment
Edikosh998 Posted May 30, 2012 Share Posted May 30, 2012 I think you cant destroy it, you can do... function () x = nil end Link to comment
NickRosso Posted May 30, 2012 Author Share Posted May 30, 2012 I see. And the other statements are true anyway? EDIT: Two hours ago i would not have any idea what are you talking about. Now i just understand. It seems i started to learn Link to comment
Alpha Posted May 30, 2012 Share Posted May 30, 2012 local x = 1 end --this is the end of the variable local x = 2 -- so from now the x is 2? end to end a variable is wrong. the keyword end is used to end blocks like Loops, Functions, If etc.. Link to comment
Flaker Posted May 30, 2012 Share Posted May 30, 2012 Some info about variable: x = 1 --Global variable. x = nil --Delete x local x = 1 -- it's local variable. (This variable exists only within this file.) function test_func() --Open function local x = 1 --This variable exists only within this function (test_func). while i < x do local y = i * 2 --local variable for while loop (exists only within While-loop ) i = i + 1 end end --Close function Usually, there is no need to remove the global variables, but if the lifetime of your variable is small, use local variable. If it became necessary to remove a global variable, just set it to "nil" Often, you can see such variable: local foo = foo This code creates a local variable "foo" and initializes it to the value of global variable "foo". This idiom is useful when you want to save the original value of "foo" in case other function will change the global variable "foo". Also, it speeds up access to the variable "foo". Link to comment
NickRosso Posted May 31, 2012 Author Share Posted May 31, 2012 local x = 1 -- it's local variable. (This variable exists only within this file.) So the global variable exists in another files if i create them here? Where else? If i create this one i can use x without setting the value in an other file? Just type x in the other file and it will know that is 1? OFF QUESTION: I can use the mulitple assigment here: x, y = 1, 2 print(x,y) But i can't use here. It's not possible? t = {a, b = 1, 4} print(t.a, t.b) Link to comment
Flaker Posted May 31, 2012 Share Posted May 31, 2012 So the global variable exists in another files if i create them here? Where else? If i create this one i can use x without setting the value in an other file? Just type x in the other file and it will know that is 1? Yes, but it is better to avoid it! Since global variables clog memory and slow performance! t = {a, b = 1, 4} print(t.a, t.b) It isn't possible! Link to comment
NickRosso Posted May 31, 2012 Author Share Posted May 31, 2012 Okay thanks, for the reply. Link to comment
Anderl Posted May 31, 2012 Share Posted May 31, 2012 t = { a, b = 1, 4 } print ( t.a, t.b ); This is not variable declaration lol Table must have that form: t = { index1 = value1, index2 = value2, index3 = value3, -- or ['index1'] = value1, ['index2'] = value2, ['index3'] = value3; } print ( t.index1, t['index1'], t.index2, t['index2'] ); --[[ Output: nil nil nil nil -- This is outputting 'nil' because index's values are nil variables So, if we change the table to t = { index1 = 2, index2 = 'lol', index3 = "FUCK YEAH", -- or ['index1'] = 2, ['index2'] = 'lol', ['index3'] = "FUCK YEAH"; } Output: 2 2 lol lol Note: You should not use equal indexes, I just put indexes with same name to show you the 2 ways to write the indexes ]]-- I hope you do understand that. Link to comment
Flaker Posted May 31, 2012 Share Posted May 31, 2012 Also, the table can be created without indexes. (In this case, the indexes will be 1,2,3,4 etc...) local table = {"One", "Two"} Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now