Overview
A very basic, but clear guide to the use and syntax of the Lua programming language. An essential reference for those new to Lua or scripting.
Introduction
Taken from:
https://web.archive.org/web/20110811101759/http://wiki.vodpod.com/w/page/12352674/A%C2%A0Short-Lua-Tutorial
(edited for minor corrections and additional examples)
When I saw this guide, I found it to be a very clear and organized reference to the basic syntax and use of operators, control structures, functions, etc. of the Lua language. There is obviously more, but anyone programming in Lua either knows and uses everything here, or could use this as a reference.
If you’re completely new to Lua, this information is an essential reference from which to start.
(I was attempting to check if there were any very basic guides such as this already on Steam, but Guides are currently completely unavailable to search or browse – go Steam.)
Literal values
Lua literals are very standard:
Lua has a single datatype called simple “table” which is used to implement both arrays and associative arrays.
Expressions
Lua arithmetic expressions are basically the same as Javascript.
Identifiers
Identifiers must be alphanumeric, include underscore, and cannot start with a number.
Valid Examples:
INVALID Examples:
Separators
Generally Lua can use either newlines or ‘;’ as statement separators. However, statements do not actually need to be delimited, and some control statements like “if” and “for” can use newlines or just a space, but cannot use ‘;’ as a delimiter.
Note: It is usually recommended to use newlines between statements for clarity, readability, and maintenance (when you or others go back to modify the code at a later date). I’d suggest only formatting as the shown in some of the compact examples below for special circumstances, or if the statements are very short.
Examples:
Note: If using a compressed control strucure, I’d reccommend putting the ‘end’ statement on the next line (possibly also ‘else’ statements for clarity), as it can aid in debugging when an ‘end’ statement is missing elsewhere in your script. Example:
Control Statements
if-then-elseif-else statement
A for loop running “i” from 1 to 10.
A for loop running “j” from 90 to 0 in steps down of 30.
A for loop setting “val” to each entry in the table “array” in turn, and running “index” from 1 to the length of “array”.
A loop over the key,value pairs in a hash table (Lua just calls it a ‘table’).
while-do statement
repeat-until statement
Functions
Functions in Lua must be called using parentheses (like Javascript, but unlike Ruby). Arguments are passed by value and by position. You can omit arguments to function calls and their values will be set to nil. Basically Lua function calls work just like Javascript functions, right down to closures. To return a value from a function, you must use the “return” operator (unlike Ruby).
Tables and Functions
The Lua “table” datatype is used much like the Javascript associative array. Tables can be used like integer-indexed arrays or like hash tables with arbitrary keys.
When using a table like an array, indexes start at 1 (unlike most other languages).
Array example
Associative array example
Using tables as function namespaces
A common technique in Lua is to use tables as namespaces for functions. Laminate uses this extensively.
Conclusion
Please comment with any questions, corrections, or suggestions for additional information.
For any additions however, I may want to keep this guide as a very basic introduction and not have it become too expansive.