Quick Lua tutorial
General
- Lua is a dynamically typed language. This means that variables do not have types; only values do. Therefore, there are no type definitions in the language
- All values carry their own type
- Lua is a case-sensitive language
Variables
- Global variables in Lua do not need to be declared.
- Any variable is assumed to be global unless explicitly declared local.
- Local variables may be declared anywhere inside a block: local varname [= initvalue].
- The scope of local variables begins after the declaration and lasts until the end of the block
- The unit of execution of Lua is called a chunk. A chunk is simply a sequence of statements, which are executed sequentially.
- A semicolon can optionally follow each statement.
Identifiers
- They can be any string of letters, digits, and underscores, not beginning with a digit
- As a convention, identifiers starting with underscore followed by uppercase letters (such as _INPUT) are reserved for internal variables
- Reserved words: and, break, do, else, elseif, end, for, function, if, in, local, nil, not, or, repeat, return, then, until, while
- Reserved strings used for operators: ~= <= >= < > == = + - * / ( ) { } [ ] ; , . .. ...
Comments
Start anywhere outside a string with a double hyphen (--) and run until the end of the line
Literal strings
- Delimited by matching single or double quotes, or by matching ... . Literals in this bracketed form may run for several lines, may contain nested ... pairs, and do not interpret escape sequences.
- They can contain the C-like escape sequences `\a' (bell), `\b' (backspace), `\f' (form feed), `\n' (newline), `\r' (carriage return), `\t' (horizontal tab), `\v' (vertical tab), `\\' (backslash), `\"' (double quote), `\'' (single quote), and `\newline' (that is, a backslash followed by a real newline, which results in a newline in the string).
- A character in a string may also be specified by its numerical value, through the escape sequence `\ddd', where ddd is a sequence of up to three decimal digits. Strings in Lua may contain any 8-bit value, including embedded zeros, which can be specified as `\000'
Numerical constants
- May be written with an optional decimal part and an optional decimal exponent.
- Examples: 3 3.0 3.1416 314.16e-2 0.31416E1
Control structures
- Three control structures are available, with the following syntax:
while expression do
block
end
repeat
block
until expression
if expression1 then
block
elseif expression2 then
block
else
block
end
- In addition, when using a numeric index, the for construct is available:
for index = start, end, step do
block
end
- step is optional; if absent, it is considered = 1; variable index is local to the loop; you can use a break statement to exit a for loop; in this case, assign index to another variable if you need it outside the loop.
- The condition expression of a control structure may return any value.All values different from nil are considered true; only nil is considered false.
- The return statement is used to return values from a function or from a chunk. Functions or chunks may return more than one value; the syntax for the return statement is
- return expression1 [expression2] (note that there are no brackets).
- The break statement can be used to terminate the execution of a loop, skipping to the next statement after the loop.
- A break ends the innermost enclosing loop (while, repeat, or for).
- For syntactic reasons, return and break statements can only be written as the last statements of a block.
- Use the structure do return end to insert a return in the middle of a block