Module util
Various functions useful for Lua game developement.
No dependency.
Usage:
TODO
has (t, v) |
Check if the list contains a value. |
remove (t, x[, n=#t]) |
Remove the first occurence of an element in a list. |
extract (t, k[, n=#t]) |
Extract the list of elements with a specific key from a list of tables |
ipairs (a, b[, ...]) |
Chainable ipairs. |
each (t, fn[, n=#t]) |
Applies a function to every item in list t. |
map (t, fn[, n=#t]) |
Applies a function to every item in list t and returns the associated new list. |
all (t, fn[, n=#t]) |
Test if all the values in the list are true. |
any (t, fn[, n=#t]) |
Test if at least one value in the list is true. |
invert (t) |
Returns a new table where the keys and values have been inverted. |
copy (t) |
Perform a deep copy of a table. |
requirer ([prefix=""]) |
Returns a table which, when indexed, will require() the module with the index as a name (and a optional prefix). |
Basic maths
-
aabb (x1, y1, w1, h1, x2, y2, w2, h2)
-
AABB collision check.
Parameters:
- x1
number
first rectangle top-left x coordinate
- y1
number
first rectangle top-left y coordinate
- w1
number
first rectangle width
- h1
number
first rectangle height
- x2
number
second rectangle top-left x coordinate
- y2
number
second rectangle top-left y coordinate
- w2
number
second rectangle width
- h2
number
second rectangle height
Returns:
true
if the objects collide, false otherwise
List operations
-
has (t, v)
-
Check if the list contains a value.
Parameters:
- t
table
the list
- v
value to search
Returns:
bool
true if is in list, false otherwise
-
remove (t, x[, n=#t])
-
Remove the first occurence of an element in a list.
Parameters:
- t
table
the list
- x
the element to remove
- n
number
the number of expected elements in the list, including nil values
(default #t)
Returns:
x the removed element
-
extract (t, k[, n=#t])
-
Extract the list of elements with a specific key from a list of tables
Parameters:
- t
table
the list of tables
- k
the chosen key
- n
number
the number of expected elements in the list, including nil values
(default #t)
Returns:
table
the extracted table
-
ipairs (a, b[, ...])
-
Chainable ipairs.
Same as ipairs, but can take several tables, which will be chained, in order.
Parameters:
- a
table
first list to iterate over
- b
table
second list to iterate over after the first one
- ...
table,...
next tables to iterate over
(optional)
-
each (t, fn[, n=#t])
-
Applies a function to every item in list t.
The function receive two argument: the value and then the key.
Parameters:
- t
table
initial list
- fn
function
the function to apply
- n
number
the number of expected elements in the list, including nil values
(default #t)
Returns:
table
the initial list
-
map (t, fn[, n=#t])
-
Applies a function to every item in list t and returns the associated new list.
The function receive two argument: the value and then the key.
Parameters:
- t
table
initial list
- fn
function
the function to apply
- n
number
the number of expected elements in the list, including nil values
(default #t)
Returns:
table
the new list
-
all (t, fn[, n=#t])
-
Test if all the values in the list are true. Optionnaly applies a function to get the truthness.
The function receive two argument: the value and then the key.
Parameters:
- t
table
initial list
- fn
function
the function to apply
- n
number
the number of expected elements in the list, including nil values
(default #t)
Returns:
boolean
result
-
any (t, fn[, n=#t])
-
Test if at least one value in the list is true. Optionnaly applies a function to get the truthness.
The function receive two argument: the value and then the key.
Parameters:
- t
table
initial list
- fn
function
the function to apply
- n
number
the number of expected elements in the list, including nil values
(default #t)
Returns:
boolean
result
Dictionary operations
-
invert (t)
-
Returns a new table where the keys and values have been inverted.
Parameters:
Returns:
table
the inverted table
-
copy (t)
-
Perform a deep copy of a table.
The copied table will keep the share the same metatable as the original table.
If a key is a table, it will be reused and not copied.
Note this uses pairs() to perform the copy, which will honor the __pairs methamethod if present.
Parameters:
Returns:
table
the copied table
-
requirer ([prefix=""])
-
Returns a table which, when indexed, will require() the module with the index as a name (and a optional prefix).
Parameters:
- prefix
string
that will prefix modules names when calling require()
(default "")
Returns:
table
the requirer table
Random and UUID
-
uuid4 ()
-
Generate a UUID v4.
Returns:
string
the UUID in its canonical representation
Object grouping
-
group (list[, n=#t[, p=nil]])
-
Groups objects in a meta-object-proxy-thingy.
Works great with Lua 5.2+. LuaJit requires to be built with Lua 5.2 compatibility enabled to support group comparaison.
Parameters:
- list
table
of objects
- n
number
the number of expected elements in the list, including nil values
(default #t)
- p
table
list of parents. Used to find the first arguments of method calls.
(default nil)
Returns:
Group
object