Quick Guide to LUA
This is a quick guide, primarily for people already familiar with some sort of scripting language, to get familiar with Lua's syntax and symantics.
Language Conventions
In identifiers any combination of letters, digits and underscores can be used, except when starting with a digit. Identifiers starting with an underscore are reserved for special uses. The identifiers you can use is locale dependent, so try to use only widely used characters. Another note is that Lua is case-sensitive, so VAR and Var are two different identifiers. The double dash (--) is used as comment sign.
-- This is a comment -- [[ This is a block comment ]]
In Lua global variables can immediatly be used, they don't need a declaration.
print(var) -- nil var = "hello" print(var) -- 'hello'
Also to delete a global variable, you just set nil as value.
print(var) -- 'hello' var = nil print(var) -- nil
Types
The eight basic types in Lua are nil, boolean, number, string, userdata, function, thread, and table. However variables in Lua have no predefined type and can contain any type of value. Using one variable for different types is sometimes usefull but can also create almost unreadable code, so don't use it too much.
- Nil is the non-value in Lua (just like null in a lot of other languages)
- Booleans are either true or false. In conditions any value other than false or nil is considered to be true
- Numbers are in Lua real, double-precision numbers.
- Strings are a sequence of characters and are delimited by single or double quotes. Literal strings are delimited by double square brackets and don't intrepred escape characters. At runtime Lua can automatically convert strings to numbers and numbers to strings. To insert a special character the following escape characters can be used:
\a bell
\b back space
\f form feed
\n newline
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\" double quote
\' single quote
\[ left square bracket
\] right square bracket - Tables are Lua's arrays, that can use numbers as well as strings as index. Tables have no fixed size and are, like Java's arrays, objects. An identifier of a table is not fixed to the table itself (like a pointer), so when you create a table and 'copies' it to another identifier, both return the changed result.
a = { } a[1] = 50 b = a print(b[1]) -- 50 a[1] = 20 print(a[1]) -- 20 print(b[1]) -- 20
Expressions
The arithmetic operators that are supported by Lua are + (add), - (subtract), * (multiplicate) and / (divide). Supported relational operators are < (smaller than), > (larger than), <= (smaller or equal to), >= (larger or equal to), == (equal to), ~= (not equal to). With the relational operators should be noted that when comparing variables of different types, Lua consideres the values also different. (ie. the numeric 10 is not equal to the string "10"). Logical operators that are available are and, or and not and string concenation is done with .. (double dot).
var = "Hello" print(var) -- 'Hello' print(var .. " World!") -- 'Hello World!'
Assignment
Assignment of a value to a variable is done by the usual = operator, as shown the previous examples. Also Lua supports the assignment of multiple values and variables in one statement, by seperating them with a comma, which is especially usefull when you need to switch variables.
var = "Hello" print(var) -- 'Hello' a, b = "I am a", "I am b" print(a) -- 'I am a' print(b) -- 'I am b'
Besides the previously shown global variables Lua also supports local variables, which are declared with the keyword local. These variables are set only for the current block (if-block, while-loop, for-loop). To have a finer control over this you can create blocks with the keywords do and end.
varA = "Hello" do local varB = " World!" print(varA .. varB) -- 'Hello World!' end print(varA .. varB) -- 'Hello'
Control Structures
Lua supports a couple of well known control structures. if executes a piece of code if a certain boolean statement is true.
if a>0 then print("a is greater than 0") else print("a is smaller or than equal to 0") end
while iterates a piece of code while a certain boolean statement is true. repeat does the same as while but executes at least once (like the do-while statement in some other languages).
a = 0 while a<5 do print("a is " .. a) a = a+1 end -- a is 0 -- a is 1 -- a is 2 -- a is 3 -- a is 4
The numeric for iterates a piece of code and executes and increases a variable by one step each time.
i = 0 for i=0,10,2 do print("i is " .. i) end -- i is 0 -- i is 2 -- i is 4 -- i is 6 -- i is 8 -- i is 10
Besides the numeric for, Lua also known a generic for, which will iterate through all options of a function, the next example will print all values and their indices in array.
for index, value in ipairs(array) do print(index .. " --> " .. value) end
To stop a loop like these before it reaches it's stop criterium, you'll have to 'break out of it.' This is done with the keyword break. After the break, the script will continue immediatly at the end of the loop that's broken. To end the execution of a function you use return. Return can also return the values from a function to the calling part of the script. Lua will only allow break and return just before the end, else and until keywords.
-- Work in Progress
Source: Supreme Commander Fansite ()