Wiki
Clone wikiCore / OtherResources
Lua references and guides
Codea version 1.5 uses version 5.1 of Lua (as demonstrated by the result of the statement print(_VERSION)
). The following sites have information you may find useful about the Lua language and programming in it:
- Lua version 5.1 reference manual: The version used by Codea.
- Programming in Lua (1st Ed): Aimed at version 5.0 of Lua, but remains largely relevant to version 5.1.
- lua-users.org: An internet site for and by users of the programming language Lua.
Frequently asked questions
The following Lua-specific matters frequently give rise to questions or requests for help on the Codea Forum.
'Full stop' syntax and the 'colon' syntax
The 'full stop' syntax myVariable.Name
is a shorthand for the syntax myVariable["Name"]
. It is used to index Lua tables and certain Codea userdata types.
The 'colon' syntax in function calls myVariable:Name(...)
is a shorthand for the syntax myVariable.Name(myVariable, ...)
. It is used to implement features in Lua that are like object-oriented programming (OOP).
The 'colon' syntax in function definitions function myVariable:Name(...)
is a shorthand for the syntax function myVariable.Name(self, ...)
. Again, it is used to implement features in Lua that are like OOP. It is used extensively together with the class()
function provided by the Codea API.
For example:
c = color(255, 0, 0, 255) -- Opaque red
myMesh = mesh()
myMesh:setColors(c) -- Correct: Equivalent to myMesh.setColors(myMesh, c)
myMesh.setColors(c) -- Wrong: Will result in an error that the first argument is bad and not a mesh
Short-cut evaluation of operators and
and or
Like many other programming languages, Lua uses short-cut evaluation for the logical operators and
and or
.
and
returns its first argument if this value is false
or nil
, and does not evaluate the second operand in those circumstances.
or
returns its first argument if this value is different from false
and nil
, and does not evaluate the second operand in those circumstances.
For example:
function message()
print("Message has been called!")
return true
end
function setup()
print(true or message()) -- Output: true
print(message() or true) -- Output: Message has been called!
end
Updated