Miscellaneous Utilities
Et Cetera contains a number of general-purpose helper functions that don't fit in a specific category; these are detailed here.
TABLE HELPERS
These functions are useful for manipulation of tables without regard to their actual contents.
etc.array_find
Usage:etc.array_find(table, value)
If the
array
contains value
as a numerical index, return the index that it is located at; otherwise return nil
.
etc.flatten
Usage:etc.flatten(table)
Converts a table with multiple dimensions into a flat (one-dimensional) array while preserving the order of elements at the bottom level. The dimensions of the table do not need to be the same size. A caveat is that bottom-level elements cannot be tables, or they will be treated as if they were another dimension of the overall tree.
Example:
etc.flatten {a, {b, c}, {d, {e}}}
returns {a, b, c, d, e}
etc.merge
Usage:etc.merge(first_table, ...)
Assuming all the arguments supplied as varargs (
...
) are tables, return a new table by merging all the supplied tables together with keys from latter-specified tables taking priority over those specified first. If first_table
is the only argument, returns a copy of it. This function is not recursive. Sub-table elements of the tables being merged will not be merged.
etc.merge_recursive
Usage:etc.merge_recursive(first_table, ...)
Identical to
etc.merge
except that it will merge subtables up to any order. This function does NOT check for recursive definitions!
PLACEHOLDER FUNCTIONS
These functions are useful when calling a method or procedure that takes a function as an argument, but you don't want to pass any particular function to it.
etc.NOP
Usage:etc.NOP()
Does nothing; returns nothing.
etc.ID
Usage:etc.ID(...)
Does nothing; returns
...
.
etc.INV
Usage:etc.INV(a, b)
Does nothing; returns
b, a
.
LOGGING FUNCTIONS
This set of functions essentially act as a wrapper aroundminetest.log
with the added capability of throwing fatal errors as part of logging procedure.
etc.log.fatal
Usage:etc.log.fatal(msg[, level])
Logs
msg
as-is accompanied by the modname, then raises an error with the same message. If level
is set, adds it to the error level. Default error level is 2 (the function calling etc.log.fatal
).
etc.log.assert
Usage:etc.log.assert(cond, msg)
If
cond
is not true, call etc.log.fatal(msg, 1)
.
etc.log.error
Usage:etc.log.error(...)
Logs the result of concatenating
...
to a string with the log level error
, along with the currently loading modname or @active
if modloading has already finished.
etc.log.warn
Usage:etc.log.warn(...)
Logs the result of concatenating
...
to a string with the log level warning
, along with the currently loading modname or @active
if modloading has already finished.
etc.log.info
Usage:etc.log.info(...)
Logs the result of concatenating
...
to a string with the log level info
, along with the currently loading modname or @active
if modloading has already finished.
etc.log.action
Usage:etc.log.action(...)
Logs the result of concatenating
...
to a string with the log level action
, along with the currently loading modname or @active
if modloading has already finished.
etc.log.verbose
Usage:etc.log.verbose(...)
Logs the result of concatenating
...
to a string with the log level verbose
, along with the currently loading modname or @active
if modloading has already finished.
etc.log
Usage:etc.log(...)
Alias of
etc.log.info(...)
.
COLOR HELPERS
etc.rgb_to_hex
Usage:etc.rgb_to_hex(r, g, b[, a])
Returns a hex string of the specified color. Uses 0-1 channels, so if your numbers are 0-255 you should divide by 255 first. The alpha channel is optional, and if specified will result in an
#RRGGBBAA
formatted string (the kind Minetest uses).
etc.hex_to_rgb
Usage:etc.hex_to_rgb(hex)
Returns red, green, blue, and optionally alpha channels in 0-1 space from the hexadecimal string provided. Only the
RRGGBB[AA]
format is supported. The hex code does not need to be preceded by a #
, but is allowed to. 0x
notation is not supported.