Lua Variables
A variable is nothing more than a name given to a particular area in memory that stores a value created by a Lua program. Variable names in Lua can contain letters, numbers and underscores, but must start with either a letter or underscore, and are case-sensitive. Unlike some languages, for example C#, Lua is a loosely typed language, which means that when a variable is declared, a datatype does not have to be assigned. A variable is automatically converted to the correct datatype depending on the value that is assigned to it.
When creating a variable, it takes the following format. The equals sign between the variable name and value is known as an assignment operator and will be discussed further in the next section on operators.
variable_name = value
The value being assigned to a variable could potentially be the value of another variable.
variable_name2 = variable_name1
Note that in this case a copy of the value in ‘variable_name1’ is assigned to ‘variable_name2’. There is no link made between the two, so when the value of one of the variables changes it is independent of the other.
Below are some example variables, the last of which is a Boolean variable, that can either be ‘true’ or ‘false’.
-- Number variable storing an integer. example_integer = 5 -- Number variable storing a floating point or decimal number. example_float = 5.1 -- String variable. example_string = "This is a string" -- Boolean variable. example_boolean = true
One other item to note is that variable names must not be the same as reserved words in the Lua programming language. Reserved words have a specific meaning and cannot be used for anything else. They are shown in blue on the preceding and following pages.
The values of variables can be displayed, for example, in a console or terminal window.
print(example_integer) print(example_float) print(example_string) print(example_boolean)
This will display the following.
5 5.1 This is a string true
Variables can also be included as part of a string. In order to do this '..' must be included in front of the variable name. For Boolean variables, the value must be converted to a string using the 'tostring' function.
print("Value of example_integer: " ..example_integer)
print("Value of example_float: " ..example_float)
print("Value of example_string: " ..example_string)
print("Value of example_boolean: " ..tostring(example_boolean))
Output from the above can be seen here.
Value of example_integer: 5 Value of example_float: 5.1 Value of example_string: This is a string Value of example_boolean: true
Finally, if it is necessary to find out the datatype of a variable, this can be achieved using the 'type' function.
print("Datatype of example_integer: " ..type(example_integer))
print("Datatype of example_float: " ..type(example_float))
print("Datatype of example_string: " ..type(example_string))
print("Datatype of example_boolean: " ..type(example_boolean))
This produces the following.
Datatype of example_integer: number Datatype of example_float: number Datatype of example_string: string Datatype of example_boolean: boolean