Lua Logo
Variables in Lua

What are variables?

We work with a computer and have a lot of values to keep in mind when we need them later on. These data will be stored in the computers memory at a certain place. How this will be done and where this memory is located...we do not need to know this while we are working with LUA.

However, to be able to work with these variables later on we give them names. With these names we will get back the data at every time we need them.

nach obenVariables - Names

It doesn't matter how we call a variable. However, there are some rules to consider.

  • A variable name has to start either with a letter (character) or an underscore.
  • Upper case and lower case in a name of a variable is very important: player1 is not equal to Player1
  • Only characters, numbers an underscores are allowed.
  • Reserved words are not allowed for using as name for a variable.

nach obenVariables - Scope of application

There are areas existing, where Variables are vaild. The reasons are different but it is helpful. In many programming languages there are different sopes of application existing.

LUA offers only two (fortunately).

  • Global
  • Local

Globalvariables are existing allways and everywhere.

Local Local variables are only existing in the block where they have been created and they exist as long as the block is active. We only have to consider one exception. This will be explained at the chapter "Functions".

Something about blocks:

At the programming language LUA everything will be done inside blocks. They start with a statement, followed by a block of assignments and finally the word "end" (again with one exception; explained later on). Blocks can also be inserted and existing inside other blocks.

We will often meet again blocks. However, I will try to visualize the difference between local and global.

Imagine a house made of glass. You can look through the glass in one direction only. From outside it is not possible to look inside the house, that should be our block. Variables, called as local inside our house of glass, can not be seen from outside the house. Only variables without the word local in front of it can be seen from outside of our house. Such variables are called global.

For the compiler a local variable will be identified, when it has the word local in front of it.

By the way, local and end are two of the reserved words in LUA.

nach obenTypes (values) of Variables

Variables store values. These values can be of different kind. They can be numbers, characters (strings), conditions (true or false) etc.

There are existing:

  • NIL (nothing)(empty)(not existing)
  • numbers
  • charcacters (letters, words, etc.)
  • conditions (true/false)(yes/no)(1/0)
  • functions (as special case of a block)
  • tables (with a lot of others variables stored inside)

Two others are existing, but above mentioned should be enugh for this moment.


nach obenNIL


Even there is almost nothing to tell about NIL, we are happy that it is existing. Every time we want to work with a variable that is not existing, we will get back the value NIL. Our computer does not crash, will not do anything strange or follow up with en error.

If we want to delete a variable, we assign the value nil to this variable. Now, our variable is not existing anymore.

Assignment:

Until now, we have already learned a lot of variables. But not, how to allocate or assign a value to a variable. As we have a value now, we will do so


willy234 = nil

The "=" is called operator of allocation (assignment). With this operator our variable willv234 will be assigned to the value NIL. Our variable has been deleted.


nach obenNumbers

As everyone knows, we need numbers for calculation. Therefore this type is called "number". Almost in all programming languages there are differences existing in the type "number". At LUA, we do not have any differences.

Allocation (Assignment)

_grandmasAge = 74
willysIQ = 105
myBalance = -55.26
PointFiveTimesSquareRootTwo = 0.707107


What is this code about?

  • Don' use a comma for the decimal point of numbers. Use the point.
  • For using negativ numbers (like my balance) use a minus in front.
  • Assignment with the operator =
  • Different notations of names for variables.


nach obenCharacters (Strings)

We do not only have numbers to work with. For example we also want to write something on the screen. For this purpose we do have another Type: "string"

A string is a concatenation of ASCII-values (what the hell is this???)

To make it easy: these are numbers as well. As we have declined them as a string, our computer knows that he has to calculate with them.

A character can be represented by an integer value which is stored in the range of 0 to 255.

For example: a character will be stored and inside the memory we have the integer number 56. This is equal to the character "D"

For this compilation the computer will have access to the ASCII[1]-table.

A concatenation of the numbers 76, 85, and 65 will mean LUA after compilation.


Assignment:

grandmasName = "Miss Elly"
willysPubName = 'Diamand Knot'
varTyp1 = "function"
myCV = [[ born : yes place of birth : no idea ]]

What is this code about?

Strings will be embeded inside quotation marks. You can use the double quote as well as the single quote (which will be of benfit later on). However, it has to be the same kind of quote at the beginning and at the end.

We also can include text in [[ and ]]. With these brackets we will be able to continue our text in a new line.

Assignment with the operator =

Strings and numbers can be concatenated very easily. For this purpose we have to use two points. We do not have to convert the number into a string.

Example:

print( "Player"..5 )


nach obenConditions

With conditions we do have a type of variable that only knows 2 conditions. This type is called "boolean" and can be designated in different ways as shown above. With LUA we use true and false.

Allocation (Assignment):

willyIsDrunken = true
grandmaIsBad = false
williysWifeIsBad = true


What is this code about?

  • An Assignment will be done with the reserved words true or false.
  • Willy should not drink so much

nach obenFunctions

Functions are variables that are doing something when they are called. We will be often engaged in using functions later on.

Functions will be started either with

Calculate = function()
-- or with
function Calculate()


and they will be finished with

end


As shown above, we see that there are two possibilites existing to assign a value to the type of variable "function". Both are equivalent.

Between the beginning and the end of a function there is the block which describes what has to be done.

Now, we will come to the exception of local.

Assumption: we do have a block which is a global function. Inside this block we do have a local variable, that will be used of this function. After executing this block the (local) variable is still existing inside the function but only inside this function.

Example:

do -- Beginning of block
local cnt = 1 -- assigned for this block
function inc () -- creating a function
cnt = cnt + 1 -- variable cnt will be used
return cnt -- returning the value for checking
end -- End of function
end -- End of block

print(inc()) --> Output 2
print(cnt) --> Output nil


This variable is not only a copy that will be transfered to the function. Apperently it is the variable itself, that lives in the twilightzone between global and local.

(My) proof, that it is not a copy but the original variable:


do
local cnt = 1
function inc ()
cnt = cnt + 1
return cnt
end
cnt = 5 -- Value will be changed after creating the function
end

print(inc()) --> Output 6


nach obenTables

Tables are variables storing other variables inside them.

Variable - Type table

The variable of type "table" makes working with columns easier. We have the possibilty to arrange our chaos or bringing light into the dark. ;)

This type of variable will engage us for a long time. First of all a brief insight.

Assignment:


myTable = {}


What is this code about?

We already know the name and the operator for an allocation.

A table will be enclosed in { for the beginning and } for the end.

In between { and } we can store variables. In our example there are no values inside { and }. So we have an empty table.

A short example without explanation. Do not worry, this will follow later on. ;)

Assignment:

myTable = { 1, 4, "Willy", true, function() dosomething end}
myTable2 = { X= 255 , Y =10, velocity =88)

reading out the values:

  • myTable[1] -> 1
  • myTable[2] -> 4
  • myTable[3] -> "Willy"
  • myTable[4] -> true
  • myTable[5] -> function() dosomething end
  • myTable2.X -> 255
  • myTable2.Y -> 10
  • myTable2.velocity -> 88
  • myTable2["velocity"] -> 88

As we can see, there are two different kinds of tables existing. One with numbers and the other with names.

One more comment: tables can include other tables as variables.

Inside the table it is not essential to have to look for a good and proper arrangement as it will be easy to live with this chaos inside a table.

This was a short and brief introduction of variables used for programming in LUA. Prior going on with learning LUA, I would like (better have to) to point out that I made a fraud. A fraud about the way I write the names of variables.

We have learned that we do not have any limitation in naming variables apart from some restrictions.

However, it is advisable to make an arrangement for this. We can imagine a lot of arrangements.


My own arrangement:

  • In general variables will start with a lower case
  • Names of functions will start with an upper case
  • Parameters (we will learn about later) will be started with an underscore
  • Every new word inside a name of a variable will have an upper case letter. This is due to readability.

Last but not least: variables are not restricted by their types. A number-variable can be changed into a string-varibale by an proper assignment for example.



[1] ASCII / American Standard Code for Information Interchange
Copying of any content of this site (text or graphics) is not allowed, excepted any source code shown in this tutorial. See also: ....Disclaimer
Copyright © Robert Schmitz 2006