Skip to content

Base functions

IS4 edited this page Sep 28, 2021 · 9 revisions

print(...)

print behaves the same as in standard Lua, but uses the logprintf plugin API function.

require(modname)

This function is available without having to use the package package, to support safe loading of other packages. It tries to look into "scriptfiles/lua" for a Lua module, or "plugins" for a C module.

take(num, func, ...)

Calls func(...), expecting num results. Since some functions in the Lua API are optimized based on the number of return values, it is necessary to provide a method to specify this number explicitly. If the call returns less than num results, the rest is filled with nils. If num is -1, func will be called expecting all return values.

bind(func, ...)

Binds a function to a variable number of fixed arguments, and returns a new function which, when called, passes the original arguments as well as the new ones to func.

async(func, ...)

Creates a new coroutine and runs func within it, passing all arguments to the function. The function is expected to yield values reg, ..., making async call reg(cont, ...), where cont is a continuation function that resumes the coroutine.

import(...)

Traverses all available local variables in the currently executing function, and if some of them are nil, they are assigned based on their name from tables passed to import.

local print
import(_G)
assert(print == _G.print)

argcheck(arg, type [, pos])

Attempts to match arg against type type (or convert to if possible). type may be any Lua type string, as well as integer or float. The conversion follows standard Lua conversion rules (i.e. a numeric string can be converted to a number, and number can be converted to a string).

If the types don't match, an error is raised from the caller function. pos specifies the index of the argument in the caller function (if it is not specified, the function tries to find it based on the value).

optcheck(val, arg [, pos])

If val is nil, raise an "invalid option" error for the argument arg (on position pos in the caller function). Otherwise, returns val.

map(func, ...)

Calls func(x, i) for every argument x at position i in ..., and replaces x with the values returned from func (may be zero or multiple). func is allowed to yield. This function uses return value optimizaion, so func may not be called for all values if they are not used by the external code. Trailing nil values are not excluded when calling func.

apply(t, ...)

For every argument x at position i in ..., calls t[i](x) and replaces x with the results. Other behaviour is the same as for map.

concat(...)

Produces a function from the arguments that, when called, appends its arguments to the original ones and returns them in sequence.

exit()

Prevents any further code from executing in the Lua instance. On the next server tick, the Lua state is closed and all its resources freed. This function can be called only from the main thread.

array(...)

Like table.pack, but the resulting table is 0-based.

table.clear(t)

Removes all elements from t. Metamethods may be invoked.

table.copy(from, to)

Copies the table contents from from to to. Metamethods may be invoked.

debug.numresults([level])

Returns the number of return values expected by the caller of a function at level (default is 1).

coroutine.resumehooked(co, ...)

Analogous to coroutine.resume, but it also sets a hook that tries to invoke the hook in the caller's thread if it exists at the point of the hook being invoked.

Clone this wiki locally