Beruflich Dokumente
Kultur Dokumente
--[[
Typographical Conventions
This is a section
this = code
-- This is a comment
---> This is programs output
--You can try/extend the examples (without any setup) here: http://www.lua.org/cgi-bin/demo
--Send me your feedback: noocell@gmail.com
]]
BASICS
Comments
--comments line
--[[
multiline
comments (or commented out code block)
]]
Strings
a=example1
String Blocks
a=[[This is a
multiline example of a string, which respects text exactly as written (doesnt respect escapers like /n .++
print(a)
--->
This is a
multiline example of a string, which respects text exactly as written (doesnt respect escapers like /n .
a=[[print(2)]]
print(a)
---> print(2)
Escape Characters
a="First line \nSecond line \n\\ \' \" \n\t after tab "
print(a)
--->
First line
Second line
\'"
after tab
Numbers
a=15
Boolean
a=true
a=false
Nil
-- nil is used to indicate nothing and not existing
local x
print(x)
--nil is returned when a variable has not been initialized (it holds nothing)
---> nil
a=5
a=nil
-- assigning nil deletes a variable (a will now be collected by Lua's garbage collector)
print(a)
print(b)
--->
nil
nil
Assignment
a=5
a, b, c = 5, 7, a-1 --Lua supports multiple assignment (if not enough values are given nil is assigned)
-- Lua first evaluates the values at the right part and then assigns them to left
a, b = b, a
a, b = func()
Operators
+-*/^
(operations)
(modulo A%B = remainder number, after A - whole Bs that can be placed in A. (20%6=2))
== ~= > < >= <= (comparison) -- ~= means not equal e.g.: if x~=y then...
and or not
=
(logical)
(assignment)
..
(concatenation of strings)
Precedence
^
not #
*
- (unary)
%
..
>
<
>= <= ~= ==
and
or
FLOW CLONTROL
Logical Decisions
A and B --means: if expression A is true then return the result of expression B, else A
A or B
--ternary in Lua
result = (("check expression") and "do if true" or "do if false")
Example:
x=10 y=20
max=(x>y and x or y)
false or y
-- equivalent to max=
print("Max="..max)
---> Max=20
x=20 y=10
max=(x>y and x or y)
x or y
-- equivalent to max=
print("Max="..max)
---> Max=20
--Tips:
var = var or value
var = check and act()
var = compare and v1 or v2
var = check and thistoo() or this() -- use and or to call both or one of two functions
if statement
if x>y then
max = x
elseif x<y
max = y
else
max = none
end
while Loop
--When you dont know exactly how many iterations you want
while (n <= 10) do
print(n)
n=n+1
end
repeat Loop
--When you dont know how many iterations you want, but you want code block to run at least once
repeat
print(n)
n=n+1
until n > 10
for n=1,10,1 do
print(n)
end
Remember here: a)the counter dies outside scope, use additional var to store it for outside
b)dont alter the counter inside the loop
c)counter,max,step are only calculated once (if they are expressions)
-- generic for
-- pairs will give all the elements (with numeric and string indexes)
-- use ipairs instead, to get only the elements with integer indexes
FUNCTIONS
add = calculate(2,6,1)
-- to gather all the returned values you must assign appropriate number of reception variables
local function calculate(a,b,c)
sum=a+b+c
sub=a-b-c
return sum, sub
end
add, subtract = calculate(2,6,1)
print(add.." "..subtract)
---> 9 -5
--note the { }
---> 7
--sometimes you want to take the values of a table and give them as arguments to a function
--use function unpack(table) to get all table values as individual returns
tt={red={255,0,0},green={0,255,0},blue={0,0,255}}
print( unpack(tt.red) )
---> 255 0 0
--Lua is adorable!
local variables
--Local variables only exist in a code block (and the underlying code blocks).
--Local variables can have the same name as a existing global variable. In such cases local variables have --a
higher priority than underlying global variables.
-- you can restrict the scope of local vars inside an artificial code block enclosing code in do code end
-- in Lua, a best practice is to declare a var (local/global) by initializing it (lazily, when needed)
-- local variables are faster to create than globals and get autoreleased (GC) after getting out of scope
-- globals are not garbage collected even if you never use them!
functions as variables
--Functions are also a variable type. The way we did to create functions so far is actually a shortcut to assign a
function to a (global) variable:
function calculate()
end
calculate = function()
code...
end
TABLES
t = {}
t*1+=something
t[2]=3
t*something+=1
t.surname=LIAN
--note however that ipairs and #table take into account only integer indexes - no records!
t= ,something, 3, surname=LIAN-
print( t[2] )
print( t.surname )
t[2]=nil
table.insert(t,This value goes at last+1 index) --insert an entry at the end of t table
table.insert(t, 3,This goes at 3rd index) --insert an entry at specific index of t table
15
--as said above, you can assign whatever values (variables of any type) at an index (integer/record)
--therefore, you can also assign functions as the value at an index (use record indexing to gain a tag)
t.add = function()
-- code for addition
end
--so, you can use tables as function modules (collections) by setting functions in place of record values
mymodule.calculator = function()
--recall that functions in Lua are internally created as plain variables of function type
myfunc = function()
--code
end
--or use the same shortcut for module functions creation, with mymodule. in front of the function name
function mymodule.calculator()
-- code for calculator
end