Game Development Reference
In-Depth Information
You will notice that it is easy to miss that, in which case the interpreter will only spawn errors. In such
cases, using the long brackets is very helpful.
Lua also has levels of nesting with long brackets; you can have various levels by inserting an equal
sign between the two opening brackets and one between the two closing brackets, like so:
testmsg = [=[ One ]=]
print(testmsg)
You can use this for some very interesting-looking source code where you can use
testmsg = [======[ One ]======]
-- Prints One
-- 5
This is a decimal number
-- 5.3
This is a floating point number
-- 0.0531
This is a scientific notation number
-- 65261
This is a hexadecimal number
Values and Types
In languages like C, you have to define a variable with a particular type. For example, you might
need define a variable i as an integer ( int ), like so:
int i;
With Lua, you do not have to define the type of variable; you can simply assign the value, and the
value can change on the fly. In Visual Basic 6 (not to be confused with Visual Basic.NET), this type of
variable was called a variant variable, and had to be explicitly defined as follows:
dim i as variant
In contrast, Lua stores variables in memory. Lua stores a variable's value and type together.
All variables in Lua are first-class values. This simply means that these values can be stored in
variables, passed to other functions as arguments, and returned from a function.
There are eight different types of variables in Lua, which I'll describe next.
nil
This is the same as null . If you have a variable that holds a reference to the last-held value and
the garbage collector doesn't clean it up, you can set the variable to nil to indicate that the space
referenced can be garbage collected.
 
Search WWH ::




Custom Search