@gwigz/slua

Global Functions

Lua and SLua global functions

Lua standard globals

Standard Lua functions available in the SLua runtime:

assert

Checks if the value is truthy; if not, raises an error with the optional message. Returns the value upon success.

function assert<T>(value?: T, errorMessage?: string): T

collectgarbage

Alias for gcinfo

function collectgarbage(option: "count"): number

collectgarbage

Run the garbage collector

function collectgarbage(option?: "collect"): void

error

Raises an error with the specified object. The optional level determines which call stack level is blamed for the error.

function error<T>(message: T, level?: number): never

gcinfo

Returns the total heap size in kilobytes.

function gcinfo(): number

getfenv

Get the scoped environment for the given function.

function getfenv(target: ((this: void, ...args: any[]) => any[]) | number): Record<string, any>

getmetatable

Returns the metatable for the specified object. If the metatable has a '__metatable' field, returns its value instead.

function getmetatable<T>(obj: T): Record<string, any> | undefined

graphheap

Writes the contents of heap to the given file in JSON format. Intended to be used with tools/graphanalyze.py

function graphheap(path: string): void

graphuserheap

Writes the contents of user heap to the given file in JSON format. Intended to be used with tools/graphanalyze.py

function graphuserheap(path: string): void

ipairs

Returns an iterator for sequential integer key-value pairs in the table. Iteration starts at 1 and halts at the first nil value.

function ipairs<V>(tab: V[]): LuaMultiReturn<
  [(this: void, arg0: V[], arg1: number) => LuaMultiReturn<[number | undefined, V]>, V[], number]
>

loadstring

Compile Luau code into a function.

function loadstring(src: string, chunkname?: string): any

newproxy

Creates a new untyped userdata object with an optional metatable.

function newproxy(mt?: boolean): any

next

Returns the next key-value pair in the table traversal order. If the index is nil or omitted, returns the first pair.

function next<K, V>(t: Record<K, V>, i?: K): LuaMultiReturn<[K | undefined, V]>

pairs

Returns an iterator for all key-value pairs in the table.

function pairs<K, V>(t: Record<K, V>): LuaMultiReturn<
  [
    (this: void, arg0: Record<K, V>, arg1: K | undefined) => LuaMultiReturn<[K | undefined, V]>,
    Record<K, V>,
    K,
  ]
>

pcall

Executes function f in protected mode, calling it with the provided arguments. Returns a boolean indicating success. If true, it also returns the function's results. If false, it returns the error message.

function pcall(f: (...args: any[]) => any, args: any[]): any

print

Sends all arguments as text privately to the object owner, separated by 4 spaces. The owner must be currently in the same region for the message to be received.

function print(args: any[]): void

rawequal

Returns true if a and b have the same type and value or reference, bypassing the __eq metamethod..

function rawequal<T1, T2>(a: T1, b: T2): boolean

rawget

Performs a table lookup, bypassing the __index metamethod..

function rawget<K, V>(t: Record<K, V>, k: K): V | undefined

rawlen

Returns the length of a table or string, bypassing the __len metamethod..

function rawlen<K, V>(t: Record<K, V> | string): number

rawset

Assigns a value to a table field, bypassing the __newindex metamethod..

function rawset<K, V>(t: Record<K, V>, k: K, v: V): Record<K, V>

require

Execute the named external module.

function require(target: any): any

select

Returns a subset of arguments starting from the specified index. Supports negative indexing. If index is '#', returns the number of arguments.

function select(i: string | number, args: any[]): any[]

setfenv

Set the scoped environment for the given function.

function setfenv(target: number | ((...args: any[]) => any), env: Record<string, any>): any

setmetatable

Changes the metatable for the given table. Raises an error if the table already has a protected metatable (it has a '__metatable' field).

function setmetatable<T, MT>(t: T, mt: MT): T

tonumber

Converts the input string to a number in the specified base. Returns nil if invalid.

function tonumber(value: string | undefined | number, base?: number): number | undefined

tostring

Converts the input object to a string. Calls the metatable's '__tostring' metamethod if present.

function tostring<T>(value: T): string

type

Returns the type of the object as a string.

function type<T>(obj: T): string

unpack

Returns values from an array in the specified index range.

function unpack<V>(tab: V[], i?: number, j?: number): V[]

xpcall

Calls function f with the provided arguments, handling errors with err if they occur.

function xpcall<E>(f: (...args: any[]) => any, err: (...args: any[]) => any, args: any[]): any

SLua-specific globals

toquaternion

Converts a string to a quaternion. Returns nil if invalid.

function toquaternion(value: string | undefined | Quaternion): Quaternion | undefined

torotation

Converts a string to a rotation (quaternion). Returns nil if invalid.

function torotation(value: string | undefined | Quaternion): Quaternion | undefined

touuid

Creates a new uuid from a string, buffer, or existing uuid. Returns nil if the string is not a valid UUID, or the buffer is shorter than 16 bytes.

function touuid(val: string | undefined | buffer | UUID): UUID | undefined

tovector

Converts a string to a vector. Returns nil if invalid.

function tovector(val: string | undefined | Vector): Vector | undefined

Plugin globals

These globals come from the transpiler plugin, not the SLua runtime.

$castRay

function $castRay<const Opts extends CastRayParamOptions>(start: Vector, end: Vector, options: Opts): CastRayResult<Opts>

$createCharacter

function $createCharacter(): CharacterParamBuilder

$httpRequest

function $httpRequest(url: string, options: HttpParamOptions): UUID

$linkParticleSystem

function $linkParticleSystem(linkNumber: number): ParticleSystemParamBuilder

$particleSystem

function $particleSystem(): ParticleSystemParamBuilder

$rezObjectWithParams

function $rezObjectWithParams(inventoryItem: string): RezParamBuilder

$setCameraParams

function $setCameraParams(): CameraParamBuilder

$setGltfOverrides

function $setGltfOverrides(link: number, face: number): GltfOverrideParamBuilder

$setPrimParams

function $setPrimParams(linkNumber: number): PrimParamBuilder

$updateCharacter

function $updateCharacter(): CharacterParamBuilder

On this page