Introduction to Lua
This crash course provides a basic introduction to the language, enough to begin writing your own basic scripts.
Lua has 8 basic data types:
Lua has the usual arithmetic operators, +, -, *, /, ^ (exponentiation). The precedence is the usual.
The following are available, >,
<=, ==, ~= (not equal)
The following are available and, or, not. false and nil are considered false, everything else is considered true (so the number 0 is also considered true). The operator and returns the first argument if it is false and the second otherwise. The operator or returns the first argument if it not false otherwise it returns the second argument.
Single line comments begin with --
--This line is not executed
Multiline comments are written with similar delimitation as strings as shown below:
--[[This line is not executed
nor this one ]]
--[=[ another long
-[==[ yet three more
There is no need to declare a variable before assigning to it. By default variables are global.
assigns 1 to the variable a.
Lua allows multiple assignments:
x = 5
a,b = 5*x,"dog"
assigns 25 to a, and "dog" to b.
To create a local variable, the keyword "local" is used. This variable will be visible only within the block it is declared in. A block is the body of a control structure such as function or a loop.
local a,b = 1, 5
creates local variables "a" and "b".
Lua has the following control structures
i = i-1
i = i+1
--1 to 10 in steps of 0.5
for i=1,10,0.5 do
-- k is the key
-- v is the value
for k,v in pairs(tble) do
--this only loops until 5.
for i=1,10 do
if i==5 then break end
Functions perform the bulk of the work in Lua. They are called by using parenthesis after their name:
calls the function "print" with the argument a string."
x = math.sin(2/3)
assigns the sine of 2/3 to the variable "x"."
Functions are created as in the example below
This creates a function f, which returns the input doubled. A function can return more than one value.
This function returns two values, the double of a number and the square. Both values can be recovered by:
The variable s will contain the value 10, and ss will contain the value 25.
THE ONLY data structure in Lua is the table. It is an associative array which can be indexed with numbers, strings or other Lua data types (except nil). Note that by default Lua tables indicies start from 1.
Tables are created in the following ways
-- create empty table
-- create integer indexed table
print(a) -- prints 3.14
print(a) -- prints 2.71
As above, brackets are used to access table elements. Another method is by using the period notation
A Lua table can contain any valid Lua object, including functions and tables. Standard Lua uses tables of functions in lieu of namespaces.
Objects (the colon notation)
Lua is not an object oriented language, but it does offer basic support via the colon operator. A table can contain both variables and functions. Lua enables functions in a table to refer to the table they live in.
Suppose I have the following table with variables a and b. Also, suppose this table contains the function sum which adds the two numbers and a constant. The code for the situation is the following:
Note that the sum function has been written as a field of the table nn, so the table nn contains three things. The sum function is defined to take a parameter t so that it can be passed any table that contains a and b.
The naive way to get call the sum function is
The table nn is passed into the function. Lua allows an easier way to make such a call using the colon:
print( nn:sum(7) )
What this does is secretly pass nn as the first parameter in the call to sum and 7 as the second parameter.
This notation also allows an alternative way to define sum:
Note that the secretly passed parameter is called self.