Lua Parancsok
Lua Parancsok
KOMMENTEK
Egysoros komment:
o -- This is a single-line comment
Többsoros kommenttömb:
o --[[ This is a multi-line
o comment ]]
Soron belüli komment tömb:
o t = {1, 2, --[[in-line block comment]] 3}
Kommenttömb, amely tartalmaz: ']]' :
o --[=[ Block comment that contains "]]". ]=]
ADATTÍPUSOK
OPERÁTOROK
SZÁMTANI OPERÁTOROK
Összeadás: `x + y'
Substract: x - y
Multiply: x * y
Divide: x / y
Integer divide: x // y
Reminder of Division: x % y
Exponentiate: x^y
RELÁCIÓSJEL OPERÁTOROK
Is a equal to b?: a == b
Is a not equal to b?: a ~= b
a is less than b?: a < b
a is less than or equal b?: a <= b
a is greater than or equal a?: a > b
a is greater than or equal a?: a >= b Returns a boolean.
LOGIKAI OPERÁTOROK
BITWISE OPERATORS
Bitwise AND:
o x & y (Lua 5.3, 5.4)
o bit32.band(x, y) (Lua 5.2)
Bitwise OR:
o x | y (Lua 5.3, 5.4)
o bit32.bor (x, y) (Lua 5.2)
Bitwise XOR:
o x ~ y (Lua 5.3, 5.4)
o bit32.bxor(x, y) (Lua 5.2)
Bitwise NOT:
o ~x (Lua 5.3, 5.4)
o bit32.bnot(x) (Lua 5.2)
Shift:
o x << y (Lua 5.3, 5.4)
o bit32.lshift(x, y) (Lua 5.2)
o x >> y (Lua 5.3, 5.4)
o bit32.rshift(x, y) (Lua 5.2)
OTHER OPERATORS
if condition then
-- code
elseif anotherCondition then
-- code
else
-- code
end
FOR LOOPS
For (numeric):
for i=1, 10 do
-- code
end
For (generic):
for key, value in pairs(table) do
-- code
end
WHILE/REPEAT LOOPS
While:
while condition do
-- code
end
Repeat-Until:
repeat
-- code
until condition
Label: ::label::
Goto label: goto label
FUNCTIONS
BASICS
Example1:
function getSum(num1, num2)
return num1 + num2
end
MULTIPLE VALUES
for j = 1, numOfStr do
print(string.format("%d : %s", j, splitStrTable[j]))
end
VARIADIC FUNCTION
Usage:
io.write("Sum : ", getSumMore(1,2,3,4,5,6), "\n")
INVOKING FUNCTIONS
print()
print("Hi")
You can omit parentheses if the argument is one string or table literal:
print "Hello World" <--> print("Hello World")
dofile 'a.lua' <--> dofile ('a.lua')
print [[a multi-line <--> print([[a multi-line
message]] message]])
f{x=10, y=20} <--> f({x=10, y=20})
type{} <--> type({})
CLOSURE
TABLES
CREATING TABLES
ACCESSING VALUES
SETTING VALUES
t[1] = "a"
t.key = "value"
LOOKUPS
STANDARD LIBRARIES
STRING LIBRARY
MATH LIBRARY
TABLE LIBRARY
Insert:table.insert(t, value)
Remove: table.remove(t, position)
Sort: table.sort(t, compareFunction)
ADVANCED
METATABLES
SETTING A METATABLE
local myTable = {}
local myMetatable = {}
setmetatable(myTable, myMetatable)
GETTING A METATABLE
local mt = getmetatable(myTable)
METAMETHODS
MODULES
COROUTINES
Coroutines are like threads except that they can't run in parallel (status:
running, susepnded, dead or normal).