Variables In Lua

In Lua, you can use dynamic types of variables. This means that it makes no sense to explicitly specify the type of variables. Simply assign the desired value and the compiler will detect and set the type of the variable. In this case, it is possible that the variable in the different parts of the code changes its type. However, this is not a concern of the programmer.

Variable names

In the name of the variable, you can only use letters and underscore (_var). Variables must start with a letter or underscore character and cannot begin with a number.

Correct variables

 

Wrong variables

 

Variable Types

In Lua, there are several types of variables: nil, logical, number, text, function, table.

 

nil

nil – it is empty. Any variable that has not yet received value is nil.

This example will display an error because the variable b is not explicitly defined and the default is nil, which causes an error when adding.

 

Logical

On logical variable types are either false and true. A value of nil and false are false, any other value is considered true.

This example will return true if the variable a is greater than one.

 

Number

All numbers in Lua are real (double-precision floating point number if you go into details). However, if you add up the two integers, the result will be an integer.

In this case, you will see the number 24.

 

Text

In the text variable, there are double quotes (“) or single quotes (‘) , as well as their combination. In most cases, there is no difference between double and single quotes except when the text found inside the quotation mark.

In this example, the first two variables used different quotes and it will not affect their value. The text of the third line meets a single quote, therefore, to avoid conflict all text is in double quotes. The fourth-line text is taken in single quotes, and the internal apostrophe is shielded by a slash (\ ‘). The fifth line of the whole text is taken in single quotation marks because within the text there are double quotes.

Multiple lines of text can be done in two ways.

  1. Use double-quotes as shown above and in place, insert a new line \n

  1. Mark the multi-line text in double square brackets, as shown below.

Function used to call the code snippet. Functions can return one or more values, depending on the parameters or none at all. In the example below, the function returns the number of digits in a variable lost.

 

 Table

Table is an associative array with many features. As an index can be used not only numbers but also text, except nil. The following example shows how to define a table and its values.

 

Keywords

As variables, you can not use the following reserved keywords:

and, break, do, else, elseif, end, false for function, if in local nil or repeat return then true.

 

Global and local variables

All variables are considered global by default, they are available anywhere in the code. If you want a local variable, for example only within the function, then add the variable name to the local keyword.