Page 1 of 1

[HOWTO] How To: Start Coroutine From Lua

Posted: Sat Jul 31, 2021 8:06 am
by Tony Li
To start a C# coroutine from Lua, register a non-coroutine method with Lua. Example:

Code: Select all

Lua.RegisterFunction("StartMyCoroutine", this, SymbolExtensions.GetMethodInfo(() => StartMyCoroutine()));
...
public void StartMyCoroutine()
{
    StartCoroutine(MyCoroutine());
}

IEnumerator MyCoroutine()
{
    ...
}
Note: Using this technique, Lua doesn't report back the status of the coroutine. If you need to wait for the coroutine to finish, consider using a custom sequencer command instead. Example:

Code: Select all

public class SequencerCommandMyCoroutine : SequencerCommand
{
    IEnumerator Start()
    {
        ... // (your coroutine code here)
        Stop(); // Always call stop when command is done.
    }
}