Lua Global¶
Functions¶
boolean rawequal(table v1, table v2)¶
v1The first argument.v2The second argument.
string type(table o)¶
oThe argument.
1 2 3 4 5 6 | |
The data returned by APIs is usually a userdata.
string tostring(table o)¶
oThe 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)¶
oThe string to be converted to a number.
1 2 3 | |
number tonumber(number o)¶
oThe argument.
table assert(... arguments)¶
arguments as is if the first argument is not false nor nil, or raises an error.
argumentsArguments 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)¶
msgThe 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.
funcThe function to call.argumentsThe 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.
funcThe function called in protected mode.errThe error handler.
1 2 3 4 5 6 7 | |
table next(table table, table index)¶
tableThe table to traverse.indexAn index of the table.
1 2 3 4 5 6 7 8 9 10 11 | |
table ipairs(table table)¶
1 | |
tableThe 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 | |
tableThe 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.
tableA table.indexAny 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.
tableA table.indexAny value.valueAny value.
table unpack(... list)¶
1 | |
listThe table to unpack.
1 2 3 | |
table select(number index, ... arguments)¶
index.
indexThe index of the first argument to return.argumentsSome 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.
indexThe string "#".argumentsSome values.
1 | |
void print(... arguments)¶
arguments, and prints their values to the output.
This can be used to log game information.
argumentsSome values.
1 | |