10-minute Lua Tutorial: The Unique Points in a Nutshell

Lua is a simple language to learn, but with plenty of power. It is object-oriented, and supports functional programming, and supports multi-threaded programming. Lua was not designed to be run alone though, and you should have in mind some other program which uses Lua already when learning Lua. It was created to be embedded in other programs, to provide a scripting environment to glue and interact with the mother program (Example: Renoise, as well as many games and game engines). You have a reason for wanting to learn Lua already so let's jump in.

Control keywords

First, the syntax and keywords make Lua seem like programming intended not for programmers used to esoteric syntactic knowledge like matching curly braces and ^ to mean logical-bitwise-exclusive-or or double-pipe (||) to mean "or". Instead those logical operators are known by the keywords and, or, not. Instead of != they use ~=. Some things will be a little more familiar to Shell Scripting programmers because of the keyword do after while and for loops and the then keyword after an if. They also use the end keyword to close blocks, which makes sense to normal people, moreso than matching curly braces or requiring whitespace to be the same (Python). Semicolons aren't required to end lines as in C-based languages, or separate parts of a conditional/loop. Semicolons can, however, be appended to lines, optionally.

There's also a repeat .. until .. and if .. then .. elseif .. [elseif ..] end. Function declarations start with function and end in end. You can prematurely break out of a loop, but there's no continue statement.


Comments are weird though, especially without syntax highlighting which many non-programmers won't have:

-- single line,
-- \[\[ ..multiple lines.. \]\]

(there shouldn't be backslashes before the brackets)

The reason for matching double brackets for multiple-line comments makes sense when you learn that multi-line strings also start with [[ and end in ]].

Tables (equivalent to array, dict, list)

Lua has only one main object type besides strings/numbers. That complex type is called a 'table' and is like a JavaScript object, and similar to a Python dict. It's also similar to a PHP Array but not a C array so much. When used as an array, indexing starts from 1 and not 0 like other programming languages. That is probably more intuitive to non-programmers though. Unquoted strings can be the keys of a table (like JavaScript), as can variables inside brackets (or quoted strings inside brackets). Table members can be accessed with bracket notation as in a dict or JS object or with dot-notation like JS objects (not Python dicts).


foo = { bar = 100, ["baz"] = 200, 300}

Members where keys were not explicitly defined are mapped to integer offsets too, starting from 1, and not including members with key names defined: print(foo[1]) -- prints 300


Strings are either single-quoted or double-quoted like Python or JavaScript (unlike C where characters are single-quoted and strings are double-quoted). Backslash \ escapes special characters. Multi-line strings use matching pairs of , similar to triple-quoted strings in Python. There's no heredoc syntax.

All Lua escaped characters: '\a' (bell), '\b' (backspace), '\f' (form feed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\v' (vertical tab), '\' (backslash), '\"' (quotation mark [double quote]), and '\'' (apostrophe [single quote])

More advanced string manipulation is made easier with the core String module. Likewise with the Table module for tables and the Math module for numbers.

Numbers (both integers and floats/decimals)

Besides Strings, there are Numbers. Again, for non-programmers, a single type of number may be more intuitive than knowing how computers store integers vs floating point numbers, and whether there's single or double precision, etc. You don't need to declare a number as signed or unsigned. Just strings, numbers, and tables.

Numbers can be written in scientific notation or hexadecimal too: 314.16e-2 0.31416E1 0xff 0x56

Other Lua types

The entire list of types in Lua: nil, boolean, number, string, function, userdata, thread, and table

Threads are returned for coroutines. Coroutines are a way to have asynchronous operations running without blocking other threads.

Lua is object oriented. But the .-notation for accessing members of a table isn't anything special. If you have objects whose member functions need to access the self object, then Lua provides some OOP syntactic sugar for that with the :-syntax between object and member function, which will automatically pre-pend the self object as the function's first argument. So if object foo has a member called bathe which takes self, friends as arguments (friends being optional) then you can either call foo.bathe(foo) or simply foo:bathe().

Further reading

That covers the basics of Lua, which should explain things to those with some previous programming experience. You should be able to make sense of example Lua code now. To do anything useful, you'll either need to know the API which your specific program which is embedding Lua offers to Lua. You can also read up on the code Lua modules and details of how Lua is implemented here.