Jump to content

Global, local variable


Recommended Posts

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

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
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

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
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...