LuaAPI
Description
The LuaAPI class is used to interact with Lua from GDScript.
This class provides the methods to execute Lua code; to call Lua functions from GDScript; to read and write the value of global Lua variables; to create a Lua constructor for a GDScript class and more.
Methods
bind_libraries void
Takes an array of strings and binds the Lua state to each library. It is case insensitive.
Danger
The IO, Package and OS libraries have some dangerous methods such as os.execute
, require
or io.write
. Make these libraries available to the end user at your own risk.
Info
You can learn more about the available libraries and their methods in the official Lua manual.
Parameters
Parameters | Description |
---|---|
libraries: Array |
Array of all Lua libraries you would like to make available. The libraries are "base", "coroutine", "debug", "table", "string", "math", "io", "os", "utf8" and "package". |
Returns
void
Example
call_function Variant
Calls a global Lua function from GDscript.
You can pass any number of arguments to the Lua function by adding them to the array parameter.
Parameters
Parameters | Description |
---|---|
luaFunctionName: String |
Name of the Lua function to be invoked. |
args: Array |
Array of arguments to be passed to the Lua function. |
Returns
Variant
Example
Example: user://file.lua
do_file LuaError?
Loads a file by its absolute path into the Lua state.
Warning
You might experience issues when loading a file in the "res://" directory after the game has been exported. In this case, it is recommended to load the file's contents as a string and use the do_string method instead.
Parameters
Parameters | Description |
---|---|
filePath: String |
Absolute path to the file. |
Returns
LuaError or null
depending on whether there is an error or not.
Example
do_string LuaError?
Loads the content of a string into the Lua state, executing it.
Parameters
Parameters | Description |
---|---|
content: String |
A string containing Lua code to be executed. |
Returns
LuaError or null
depending on whether there is an error or not.
Examples
expose_constructor LuaError?
Exposes the constructor of a class to Lua as a global method.
Parameters
Parameters | Description |
---|---|
constructorName: String |
The name of the constructor in Lua. |
gdscriptClass: Object |
A GDScript class or any Object that can be created with the .new method. |
Returns
LuaError or null
depending on whether there is an error or not.
Examples
The string passed to lua.do_string
is the following Lua code:
function_exists bool
Returns true
if there is a Lua function with the provided name.
Parameters
Parameters | Description |
---|---|
functionName: String |
The name of the Lua function to check. |
Returns
bool
Example
Example: user://file.lua
pull_variant Variant
Reads the value of a variable by its name from the Lua state.
Parameters
Parameters | Description |
---|---|
variableName: String |
The name of the Lua variable to read. |
Returns
Variant
Example
push_variant LuaError?
Pushes a copy of a variant (value
) to the Lua stack as the global variable variableName
.
Parameters
Parameters | Description |
---|---|
variableName: String |
The name of the Lua variable to write to. |
value: Variant |
The value to be written. |
Returns
LuaError or null
depending on whether there is an error or not.