LuaCoroutine
Description
The LuaCoroutine class is used to create and manage Lua coroutines. Lua coroutines bound to the same Lua object share the same global table.
In contrast to the LuaAPI class, any file or string you load into the state will not be immediately executed. Everytime the resume method is called, the Lua code will execute until its end is reached or until yield
is called from Lua.
Lua code running inside a LuaCoroutine has access to the yield
function which can accept and return any number of arguments to GDScript through the resume function of the coroutine. Typically, you will use this function to pause the Lua code for as long as the user specifies in its arguments (see the example below).
Methods
bind void
Binds the coroutine to a LuaAPI object. All coroutines attached to the same object share the same resources.
Parameters
Parameters | Description |
---|---|
lua: LuaAPI |
The LuaAPI object to bind to. |
Returns
void
load_file LuaError?
Loads a file by its absolute path into the coroutine's state.
Warning
This method simply loads the contents of the file into the Lua state. It does not execute it.
You must use the resume method to execute the code that was loaded.
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 load_string method instead.
Parameters
Parameters | Description |
---|---|
filePath: String |
Path to the file. |
Returns
LuaError or null
depending on whether there is an error or not.
load_string void
Loads the content of a string into the Lua state.
Warning
This method simply loads a string of Lua code into the Lua state. It does not execute it.
You must use the resume method to execute the code that was loaded.
Parameters
Parameters | Description |
---|---|
content: String |
A string containing Lua code to be loaded. |
Returns
void
resume Variant
Resumes or starts the coroutine. Will either return an Array of arguments passed by Lua through the yield() function or a LuaError.
Parameters
Parameters | Description |
---|---|
args: Array |
A array of arguments to be passed to yield() in lua (Or await in gdscript if yield_await was used). |
Returns
Variant
is_done bool
Returns true
if the coroutine has finished executing.
Parameters
None
Returns
bool
yield_await Signal
Warning
This method is still experimental and may cause issues.
This method attempts to allow you to hault execution in a GDScript method called by a lua coroutine.
It takes an array as an argument, which is then passed to LuaCoroutine.resume()
.
Parameters
Parameters | Description |
---|---|
args: Array |
Array of arguments to be passed to LuaCoroutine.resume(). |
Returns
Signal
Example
yield_state LuaError
Warning
This method is still experimental and may cause issues.
Parameters
Parameters | Description |
---|---|
args: Array |
Arguments to be passed to resume. |
Must be called from a lua hook. Will yield the lua state and pass arguments to resume. Can be destructive.
Returns
call_function Variant
Note
This method is identical to LuaAPI's method with the same name.
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
function_exists bool
Note
This method is identical to LuaAPI's method with the same name.
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
Note
This method is identical to LuaAPI's method with the same name.
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?
Note
This method is identical to LuaAPI's method with the same name.
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.
Example
Example
set_hook void
Sets the hook for the state. The hook will be called on the events specified by the mask. The count specifies how many instructions should be executed before the hook is called. If count is 0, the hook will be called on every instruction. The hook will be called with the following arguments: hook(parent, event, line)
. The parent is the LuaAPI object that owns the current state.
This is useful for preventing infinite loops, but it should be kept in mind there will be a large performance tax.
Parameters
Parameters | Description |
---|---|
hook: Callable |
The function to be called as a hook. |
mask: int |
The hook even mask. |
count: int |
Specifies how many instructions should be executed before the hook. |
Returns
void
Example
Hello world!
Good Bye World!
Good Bye World!
Good Bye World!
Good Bye World!
Good Bye World!
Hello world!
Good Bye World!
Good Bye World!
Good Bye World!
Good Bye World!
Good Bye World!