Instant coroutine ?

Hello!

I try to start an empty coroutine for a design reason. I can’t find a way to execute that coroutine without wait until the next frame to execute the next part of my code.

There is a sample code (the Execute() method is started with the StartCoroutine() method) :

public override IEnumerator Execute() {

            Debug.Log("test2");

            // Node controllable
            INodeControllable nodeControllable = item.GetComponent<INodeControllable>();
            if (nodeControllable != null) {
                yield return StartCoroutine(Execute(nodeControllable));
            }

            Debug.Log("test3");

}

protected virtual IEnumerator Execute(INodeControllable node) {
            yield break;
}

What I want is when a class doesn’t override the Execute method, “test2” and “test3” are logs in the same frame.

Is there something i’m missing here ?

Thanks!

You’ll need to manually run your inner coroutine:

if (nodeControllable != null) {
    var innerRoutine = Execute(nodeControllable());
    while(innerRotuine.MoveNext())
        yield return innerRotuine.Current;
}

An IEnumerator that hits yield break on it’s first MoveNext will return false, so your while-loop will never run and nothing will be yielded by it.

3 Likes

Hi Baste!

Thanks for the tip it’s working really great.