- Home /
Wait bunch of coroutines inside of coroutine
Okay, in a simple scenario when you need to wait coroutine inside coroutine you just call this:
yield return StartCoroutine(obj.MyAction());
// do something
But when I need to start MyAction for a bunch of objects and wait until them all will be finished? Is there a simple way, or I need to use additional variables and check it in loop within parent coroutine? I mean, do something like this:
foreach (MyScript s in scriptsToRun)
{
StartCoroutine(s.MyAction());
}
bool isActionsProcessed = false;
while (!isActionsProcessed)
{
isActionsProcessed = true;
foreach (MyScript s in scriptsToRun)
{
if (!s.IsActionProcessed)
isActionsProcessed = false;
}
if (!isActionsProcessed) yield return null;
}
// do something
Answer by whydoidoit · Mar 12, 2014 at 07:26 AM
You could pass a class parameter to the coroutine that contains a count of currently running routines or you could make a generic coroutine runner that does that if you prefer.
ParallelSupport.cs
using System.Collections;
using UnityEngine;
public class RunInfo {
public int count;
public static Dictionary<string, RunInfo> runners = new Dictionary<string, RunInfo>();
}
public class ParallelSupport : MonoBehaviour {
public static ParallelSupport instance;
void Awake() { instance = this; }
}
public static class ParallelCoroutineExt {
public static RunInfo ParallelCoroutine(this IEnumerator coroutine, string group = "default") {
if(!RunInfo.runners.ContainsKey(group)) {
RunInfo.runners[group] = new RunInfo();
}
var ri = RunInfo.runners[group];
ri.count++;
ParallelSupport.instance.StartCoroutine(DoParallel(coroutine, ri));
return ri;
}
static IEnumerator DoParallel(IEnumerator coroutine, RunInfo ri) {
yield return ParallelSupport.instance.StartCoroutine(coroutine);
ri.count--;
}
}
You then attach ParallelSupport to anything in your scene that will hang around and then you can do this:
RunInfo info;
foreach (MyScript s in scriptsToRun)
{
info = s.MyAction().ParallelCoroutine(); //Optionally specify a group here
}
while(info.count > 0) yield return null;
Note that this coroutine is running based on the lifetime and activation of what you attached ParallelSupport to - it would be easy to adapt to pass a behaviour on which it should be run ins$$anonymous$$d.
Answer by beck · Mar 12, 2014 at 06:04 AM
If you are not using the Unity's special yields (WaitForSeconds, StartCoroutine) from the coroutines you're waiting for, you can use the IEnumerator object itself:
IEnumerator routine1 = SomeCoroutine();
IEnumerator routine2 = SomeOtherCoroutine();
while(routine1.MoveNext() | routine2.MoveNext())
{
yield return null;
}
The above code reads "while routine1 or routine2 are not finished, wait for 1 frame"
Wow, manually iterate coroutine is gotta be obvious since they returned IEnumerator, but I've never thought of them that way. I changed code a bit to work with collection of coroutines, but it looks like this code not work properly, maybe you can help me to figure out why.
// Collection of coroutines
IEnumerator[] waitToDestroy = GetBunchofCoroutines();
bool destroyFinished = false;
while (!destroyFinished)
{
destroyFinished = true;
Array.ForEach(waitToDestroy, cor => { if (cor.$$anonymous$$oveNext()) destroyFinished = false; });
if (!destroyFinished) yield return null;
}
// do something
(btw now I see that I can use array ins$$anonymous$$d of list)
Coroutines work fine, but this last "do something" part starts right after calling coroutines, not after finishing them.
Actually @beck's code does not iterate the two routines in parallel it does one then the other due to the way that if evaluates its terms.
Your code looks like it just does the whole thing at once.
Yes sorry, my while should have read:
while(routine1.$$anonymous$$oveNext() | routine2.$$anonymous$$oveNext())
Corrected my post.
Okay, I'd cleaned my code a bit but still can't understand why it not working properly. @whydoidoit, why it is does whole thing at once? As for me this scenario looks proper, seems like I missed something. It calls $$anonymous$$oveNext on the each coroutine and waits till the next frame if there is any coroutines that can iterate. I beleive I missed something obvious, but I cannot figure out what
So it would depend on whether those coroutines did anything apart from yield return null; I would seriously consider using my answer below...
Answer by Ash-Blue · Mar 23, 2019 at 07:39 PM
Actually you can do this with just a few simple lines. You need to return each coroutine in a loop and it will automatically complete when it has nothing left to do. Here is an example that will wait for a list of coroutines to complete.
private IEnumerator WaitForMultipleCoroutines (List<Coroutine> coroutines) {
var animations = new Stack(coroutines);
while (animations.Count > 0) {
var animation = animations.Pop();
yield return animation ;
}
// Logic to trigger after all coroutines complete goes here
}