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?
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.
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.
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.
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())
:
Bar
) will be started before yieldingStartCoroutine
continues to run.yield return coroutineFunction()
:
Bar
) will be started after yieldingyield 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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With