title | tagline |
---|---|
tricks |
quick Lua cheat sheet |
Or, unreadable code that you should really turn into helper functions whenever you can.
logic
not a == not b
both or none
numbers
math.min(math.max(x, min), max) clamp x (upper limit takes precedence)
x ~= x
number is NaN
1/0
inf
-1/0
-inf
math.huge == math.huge-1
check if inf is available without dividing by zero
x % 1
fractional part (always positive)
x % 1 == 0
number is integer; math.floor(x) == x
x - x % 1
integer part; but better use math.floor(x)
x - x % 0.01
x floored to two decimal digits
x - x % n
closest to x
smaller than x
multiple of n
math.modf(x)
integer part and fractional part
math.floor(x+.5)
round
(x >= 0 and 1 or -1)
sign
y0 + (x-x0) * ((y1-y0) / (x1 - x0))
linear interpolation
math.fmod(angle, 2*math.pi)
normalize an angle
tables
next(t) == nil
table is empty
strings
s:match'^something'
starts with
s:match'something$'
ends with
s:match'["\'](.-)%1'
match pairs of single or double quotes
i/o
f:read(4096, '*l')
read lines efficiently