[HOWTO] How To: Start Coroutine From Lua

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
Tony Li
Posts: 21987
Joined: Thu Jul 18, 2013 1:27 pm

[HOWTO] How To: Start Coroutine From Lua

Post 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.
    }
}
Post Reply