User Tools

Site Tools



A variable is a name that identifies a place where a value can be stored. A variable that has not been assigned a value is nil. Assigning nil to a variable is equivalent to deleting the variable. It is also a way to declare that a variable will be used later in the script.

Variable names must begin with a letter or the underscore character. After the first character, letters, digits, and the underscore character may be used.

The scope of a variable controls where that variable can be used in a script. The global scope is always available. Lua will look-up the names of global variables when the script runs, so global variables do not need to be declared before they are used.

Each block of statements creates a local scope. Variables restricted to the local scope will be deleted when the block ends. The local scope includes any nested blocks, but does not extend to the block it is nested in. A local variable can hide a global variable, or another local variable that was created previously in the same scope.

All variables are in the global scope unless the local keyword is used. When a variable is declared as local, any other uses of the name in the current scope will refer to that variable. If you use the local keyword again with the same name, you will create a second variable that hides the previous one.

x = 10      -- A global variable
local i = 1 -- A local variable

while i <= x do
    local x = i * 2  -- This x is local
    print(x)       --> 2, 4, 6, 8, ...
    i = i + 1

if i > 20 then
    local x        -- x is declared but not assigned
    x = 20
    print(x + 2)   --> 22 (the local one)
    print(x)       --> 10 (the global one)

print(x)           --> 10 (the global one)

You should use local variables as much as possible; at least at the function level. Local variables from different functions won’t conflict with each other. They are also faster than global variables. A common way to improve the speed of a script is to create a local copy of a frequently used global value, usually a function.

local line = ""
local read =
    local char = read(1)
    line = line .. char
until char == '\n'

Multiple variables can be assigned at the same time using a list. The right side of an assignment is completely evaluated then the multiple values are copied to the variables on the left side of the assignment. If there are more values on the right side than there are variables on the left side, the extra values are discarded. If there are more variables than values, the extra variables are assigned nil. Assignment lists also occur when calling or returning from a function. The number of values passed to a function does not need to match the number of parameters declared in the function definition. A function may return multiple values, and they are assigned to the left-side variables as a list.

local a,b = 1,2    -- Both a and b are local variables
a,b = 1,2,3        -- The third value is discarded
a,b,c = 1,2        -- Assigns nil to c
a,b,c = 1,nil,3    -- Explicit nil
c,a = a,c          -- Swaps the values of a and c
a,b,c = pick3()    -- Get three values from the function
print(pick3())     -- Displays the three values

A function that returns multiple values must be at the end of an assignment list. Each expression in a list that isn’t the last will discard multiple values. On the other hand, if you want to call a function at the end of a list but only want a single value, you need to explicitly discard multiple values.

a,b,c,d = 1,pick3()    -- Fills all four variables
a,b,c,d = 1,pick3(),2  --[[ Only the first return value is assigned to b.
                            c is assigned 2, and d is nil ]]
a,b,c,d = 1,(pick3())  -- Parenthesis force a single value. c and d are nil
lgscript/lua_variables.txt · Last modified: 2009/08/27 21:56 by telliamed