Global Functions⚓︎
Global functions in lua are the built-in funcions which are pre declared and are very useful. There are many of them but in this guide we will discuss some common global functions.
tonumber()⚓︎
tonumber(value,base)
It converts the given argument in to a number
of desired base (between 2-36). If second argument is missing then by default it converts the given argument into a number of base 10. In case of failure will return nil
.
tostring()⚓︎
tostring(value)
It converts the given argument in to a string
. Just like tonumber()
, If it cannot convert it will return nil
.
print()⚓︎
print(value)
It takes multiple arguments and display them in the console/output. In most cases it is used for debugging.
assert()⚓︎
assert(statement,error message)
It stops the thread and returns the error message if the given statement is false
or nil
.
error()⚓︎
error(message)
It stops the curent thread and gives the error in the output/console.
type()⚓︎
type(value)
It returns the data type of the given argument.
pcall()⚓︎
pcall(function,...)
It calls the given function in protected mode. In case of any error the thread will not be terminated. The first returned value is a bool which is either true
or false
depends if the call succeeded or not. If the call succeed then first value will be true
and second value will be the value returned by the function called in the protected mode. If the call could not succeed then pcall will return false with the error message.
unpack()⚓︎
unpack(table,initial_index,final_index)
It returns elements of a table in the form of tuple
, according to the given arguments. By default: initial_index = 1
final_index = #table
select()⚓︎
select(n,args)
If n is positive, It returns all the given arguments after the index n
If n is negative, It returns all the given n number of arguments from the end
if n is #
operator, It returns the number of given arguments
rawequal()⚓︎
rawequal(value1,value2)
It returns a bool value. If given arguments are equal, it returns true
. If the arguments are not equal, it returns false
Thanks For Reading :)⚓︎
In case of any mistake feel free to report this article!