Lua Global¶
Functions¶
boolean rawequal(table v1, table v2)¶
v1
The first argument.v2
The second argument.
string type(table o)¶
o
The argument.
1 2 3 4 5 6 |
|
The data returned by APIs is usually a userdata.
string tostring(table o)¶
o
The only argument.
1 2 |
|
float3 implements __tostring in its metatable, and float3 can be converted to a string in the format [x,y,z].
number tonumber(string o)¶
o
The string to be converted to a number.
1 2 3 |
|
number tonumber(number o)¶
o
The argument.
table assert(... arguments)¶
arguments
as is if the first argument is not false nor nil, or raises an error.
arguments
Arguments to check.
1 2 3 4 5 6 |
|
In this example, the div
function takes two arguments and returns their quotient, which contains an assert statement to ensure the second argument is not zero.
If the assertion fails, the program execution will be stopped with an error raised.
void error(table msg)¶
msg
The error message.
1 2 3 4 5 6 |
|
In this example, the error statement in the wont_print function will raise an error with a message "terminated" and the following print statement will never execute because function error never returns.
boolean pcall(function func, ... arguments)¶
func
is not propagated; instead, pcall catches the error and returns the error message.
Its first result is the status code (a boolean), which is true if the call succeeds without errors.
In such case, pcall also returns all results from the call, after this first result.
In case of any error, pcall returns false plus the error message.
func
The function to call.arguments
The arguments to pass.
1 2 3 4 5 6 7 8 |
|
In this example, we define a div
function that throws an error if the second argument is zero.
In the first print statement, pcall returns true with the quotient.
In the second print statement, div
function raises an error since the second argument b is zero,
then pcall catches this error and returns false with an error message.
boolean xpcall(function func, function err)¶
func
in protected mode, using err as
the error handler.
Any error inside func
is not propagated; instead, xpcall catches the error, calls the err
function with the original error object, and returns a status code.
Its first result is the status code (a boolean), which is true if the call succeeds without errors.
In this case, xpcall also returns all results from the call, after this first result.
In case of any error, xpcall returns false plus the result from err.
func
The function called in protected mode.err
The error handler.
1 2 3 4 5 6 7 |
|
table next(table table, table index)¶
table
The table to traverse.index
An index of the table.
1 2 3 4 5 6 7 8 9 10 11 |
|
table ipairs(table table)¶
1 |
|
table
The table to iterate.
1 2 3 4 5 6 7 8 |
|
In this example, ipairs is used in the for loop to iterate over the table t from the index 1 to the index 3. The loop variable i holds the value of the current index, and v holds the value of the current element.
table pairs(table table)¶
1 |
|
table
The table to iterate.
1 2 3 4 5 6 7 8 |
|
In this example, pairs is used in the for loop to iterate over the table t in any order. The loop variable k holds the value of the current index, and v holds the value of the current element.
table rawget(table table, table index)¶
table[index]
, without invoking any metamethod. table
must be a table; index
may be any value except nil.
table
A table.index
Any value.
void rawset(table table, table index, table value)¶
table[index]
to value
, without invoking any metamethod. table
must be a table, index
any value different from nil, and value
any Lua value.
table
A table.index
Any value.value
Any value.
table unpack(... list)¶
1 |
|
list
The table to unpack.
1 2 3 |
|
table select(number index, ... arguments)¶
index
.
index
The index of the first argument to return.arguments
Some values.
1 2 |
|
In the second print statement, the "#" argument following the select call removes all return values except the first of the select call, and only 2 and "#" self are printed.
number select(string index, ... arguments)¶
arguments
.
index
The string "#".arguments
Some values.
1 |
|
void print(... arguments)¶
arguments
, and prints their values to the output.
This can be used to log game information.
arguments
Some values.
1 |
|