Lua

Cheatsheet

for

for var=start,endVal,step do ... end -- var is local, endVal is inclusive, step is optional (default 1)
for i=1,f(x) do ... end -- f(x) is evaluated only once at start
for i,v in ipairs(array) do ... end -- array for each
for k,v in pairs(table) do ... end -- key value for each
for i=1,10 do -- do not change i during loop!
	if i==5 then break end
	if i==1 then goto continue end
	::continue::
end

Table ~ Array

-- Add elements
table.insert(vtable, elem) -- at end
table.insert(vtable, 1, elem) -- at start
-- Remove elements
elem = table.remove(vtable, pos) -- no pos = last element
-- Count entries in array, stops at first nil element
table.getn(vtable)
#vtable -- same as getn
table.setn(vtable, size) -- set size of array
-- iterate over array
for k, v in ipairs(vtable) do sum = sum + v end
-- Count all entries in a table
local count = 0
for _ in pairs(vtable) do count = count + 1 end
-- Copy all table members from src to dst (shallow)
for k, v in pairs(src) do dst[k] = v end
-- Clear table
for k in pairs(vtable) do table[k] = nil end
-- Sort array
table.sort(vtable, orderFunc) -- orderFnc = true if a->b

typeof

type(var)
"nil", "boolean", "number", "string", "userdata", "function", "thread", "table"

ipairs vs pairs

ipairs(table) iteriert über Elemente eines arrays (also alle table Elemente mit Integer >0 als key), in aufsteigeneder Reihenfolge, hält bei Lücken in den keys an.
pairs(table) iteriert über alle Elemente einer Table (Reihenfolge der Elemente ist dabei zufällig!). Elemente entfernen (=nil setzen) ist okay während for .. in loop, Elemente hinzufügen nicht.

Tenary

https://stackoverflow.com/a/72021612

-- Does not work if trueValue is falsy (false or 0)!
(condition and trueValue or falseValue)
-- Works all the time
(condition and {ifTrue} or {ifFalse})[1]

Block Comments

--[[
print(10)         -- no action (comment)
--]]

-- Adding single hyphen to start of block comment will enable the block again:
---[[
print(10)         --> 10
--]]

Class methods

TODO

local class = {}
function class.normalFunc() 
	-- "static" function
end
local obj = { someValue = 0 }
class.normalFunc()
function class:classFunc() 
	self.someValue = 1 -- object is passed as "self"
end
class:classFunc()

Table to string

local function dump(o)
    if type(o) == 'table' then
	    local s = '{ '
        for k,v in pairs(o) do
            if type(k) ~= 'number' then k = '"'..k..'"' end
            s = s .. '['..k..'] = ' .. dump(v) .. ','
        end
        return s .. '} '
    else
        return tostring(o)
    end
end

Metamethod cheatsheet

https://gist.github.com/oatmealine/655c9e64599d0f0dd47687c1186de99f

A metatable can be defined like

local t = setmetatable({}, {
  __tostring = function() return 'custom tostring behavior!' end
})

Here are the metamethods that you can define, and their behavior

Operators

In an argument signature like f(a, b), a and b don't necessarily have to be instances of your metatable. One of them will always be, but not necessarily the first. Beware!

Calculation operators

Bitwise operators (Lua 5.3)

Equation operators

Misc operators

Behavioral methods

Indexing

Calling

Garbage collection & memory management

Misc

References

https://www.lua.org/manual/5.4/manual.html#2.4
https://ebens.me/post/lua-metatables-tutorial/
https://www.lua.org/versions.html
https://www.lua.org/manual/5.4/contents.html#index