Lua crash courseFollowing crash course is an extremely condensed extract from the official Lua reference manual. For more details refer to the Lua website and to the numerous examples contained in the demo scenes. Lexical conventions
and break do else elseif
end false for function if
in local nil not or
repeat return then true until while
+ - * / % ^ #
== ~= <= >= < > =
( ) { } [ ]
; : , . .. ...
a=4 -- variable a is now 4!
Types and values
nil type of the value nil whose main property is to be different from any other value.
It usually represents the absence of a useful value
bool values false and true (both nil and false make a condition false;
any other value makes it true)
number both integer and floating-point numbers (has internally two distinct
representations: integer and float)
string arrays of characters (strings may contain any 8-bit character,
including embedded zeros)
function Lua functions
userdata can hold arbitrary C data (corresponds to a block of raw memory)
thread independent threads of execution used to implement coroutines
table arrays that can hold values of any type except nil
VariablesStatements
x,y,z = myTable[1],myTable[2],myTable[3]
== equality
~= negation of equality
< smaller than
> bigger than
<= smaller or equal than
>= bigger or equal than
if value1==value2 then
print('value1 and value2 are same!')
end
for i=1,4,1 do -- count from 1 to 4 with increments of 1
print(i)
end
i=0
i=0
myTable={'firstValue',2,3} -- builds a table with 3 values
a=' hello'
stringLength=#'hello world'
Bitwise operators
&: bitwise AND
|: bitwise OR
~: bitwise exclusive OR
>>: right shift
<<: unary bitwise NOT
~: unary bitwise NOT
Coroutines or threads
-- Create a coroutine:
corout=coroutine.create(coroutineMain)
-- Start/resume a coroutine:
if coroutine.status(corout)~='dead' then
local ok,errorMsg=coroutine.resume(corout)
if errorMsg then
error(debug.traceback(corout,errorMsg),2)
end
end
-- The coroutine itself:
function coroutineMain()
while true do
-- some code
end
end
|