Corona – Variables

A variable is a storage space for a value that your program will use.Imagine you are creating a game. You might want to keep track of the game score. You would use a variable to do this. Often your programs will have many variables that refer to lots of different things.To assign a variable a value use the equal sign. For example:

score = 0

You can think of variables in the same way that you used x and y in algebra. These letters were placeholders for many possible values. In Lua, like many other programming languages variables get a name. The name can be any combination of characters. You can use variables as placeholders for values that will change as your program runs. For example you might display your score by setting the text property of a Text Object. For example:

scoreText = display.newText(...)
score = 0
-- later in your program
scoreText.text = score

Variables are called variables because the stored values can change. As your program runs you can change the value assigned to a variables. You can assign a new value at anytime to a variable using the equal sign.

The example above declares two variables scoreText and score. The first holds a reference to a new Text Object. The second holds a number, 0 in this case.

score = 10000

Note: Lua doesn’t support composite assignment operators like += and -=. Instead you’ll need to use:

score = score + 100

In Lua variables can be Global or Local. All variables are Global unless specifically declared as Local. Global variables can be accessed anywhere in the program. Local variables can only be accessed in the code block where they were declared. Declare a Local variable with the key word “local” the first time it appears. For example:

core = 100
local color = "red"

Here “score” is a global variable and color is a local variable.

Scope

Scope is a term that refers to where a variable can be accessed. The scope Global variables is easy to determine. Since they are accessible everywhere.

Local variables on the other hand take a little more thought. Unlike global variables, local variables have their scope limited to the block where they are declared. A block is the body of a control structure, the body of a function, or a chunk (the file or string with the code where the variable is declared).

x = 10             -- global variable
local i = 1        -- local to the chunk

while i<=x do
   local x = i*2   -- local to the while body
   print(x)        --> 2, 4, 6, 8, ...
   i = i + 1
end

if i > 20 then
   local x         -- local to the "then" body
   x = 20
   print(x + 2)
else
   print(x)        --> 10  (the global one)
end

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

It is good programming style to use local variables whenever possible. Local variables help you avoid cluttering the global environment with unnecessary names. Moreover, the access to local variables is faster than to global ones.

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *