Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a StartCoroutine needed for a call from inside one co-routine to another co-routine?

When you have nested co-routines like

void Update()
{
    if(someTest)
    {
        StartCoroutine(Foo());
    }
}

IEnumerator Foo()
{
    doStuff = true;
    yield return StartCoroutine(Bar());
    doStuff = false;
}

IEnumerator Bar()
{
    //Very important things!
}

Is the StartCoroutine in yield return StartCoroutine(Bar()); necessary?

Are we allowed to just do

void Update()
{
    if(someTest)
    {
        StartCoroutine(Foo());
    }
}

IEnumerator Foo()
{
    doStuff = true;
    yield return Bar();
    doStuff = false;
}

IEnumerator Bar()
{
    //Very important things!
}

If we are allowed, does this have any impact on the program behavior/performance?

like image 783
Scott Chamberlain Avatar asked Jan 18 '17 17:01

Scott Chamberlain


People also ask

What is StartCoroutine in Unity?

A coroutine is a function that allows pausing its execution and resuming from the same point after a condition is met. We can say, a coroutine is a special type of function used in unity to stop the execution until some certain condition is met and continues from where it had left off.

Can you call coroutine within a coroutine?

Coroutines are great, and nested coroutines are just fine. But I've found them to be pain in situations where things can change while they're running. For example, maybe you're doing a WaitForSeconds(2.0) during a sleeper hold, but then a counter reduces the remaining time by 0.5 seconds.

How do you keep coroutines from going inside?

To stop a coroutine from "inside" the coroutine, you cannot simply "return" as you would to leave early from an ordinary function. Instead, you use yield break . You can also force all coroutines launched by the script to halt before finishing.


1 Answers

Is the StartCoroutine in yield return StartCoroutine(Bar()); necessary?

No, you are allowed to use yield return Bar();.

If we are allowed, does this have any impact on the program behavior/performance?

Yes to both behavior and performance question.

The Difference:


yield return StartCoroutine(coroutineFunction()):

  • Inner coroutine(Bar) will be started before yielding
  • Memory Allocation: 56 bytes
  • Calls: 2
  • When a parent coroutine is killed, the child coroutine that is started with StartCoroutine continues to run.

yield return coroutineFunction():

  • Inner coroutine(Bar) will be started after yielding
  • Memory Allocation: 32 bytes
  • Calls: 3
  • When a parent coroutine is killed, the child coroutine that is started with yield return coroutineFunction() is killed as well. This is every important to know especially when you need to stop a parent coroutine with its children.
  • Faster:

    Probably because it allocates less memory. When used in a for loop it is faster than yield return StartCoroutine(coroutineFunction()). This is even true even though it has more calls. Also, Time and Self ms from the Profiler shows that its values are less than the ones from yield return StartCoroutine(coroutineFunction()).

In conclusion:

The yielding difference is almost like the i++ vs ++i (post and pre increment). If you care about memory management then go with the second method yield return coroutineFunction() as it allocates less memory. Also if you want to be able to stop all the inner or child coroutines when the parent one is stopped then also use yield return coroutineFunction().

like image 66
Programmer Avatar answered Oct 05 '22 22:10

Programmer