# Recursive Unity Coroutine

I have a co-routine which is heavily reliant on calling itself to get the task done. I have tested it and the code works fine, the problem is that the co-routine will return every time that it enters itself recursively.

IEnumerator test(arg1, arg2)
{
// I want to pause here
yield return null;

// I don't want to pause on any of these
yield return test(arg1, arg2);
yield return test(arg1, arg2);
yield return test(arg1, arg2);
yield return test(arg1, arg2);
}


In the example above I have a yield return at the top that is hit and pauses the co-routine, thats fine. Then there are the calls to itself below which also pause the co-routine. How do I avoid pausing the co-routine on the recurcive calls?

• Since the recursive calls are themselves just IEnumerators, you can step them manually in a loop as shown here, ignoring any unwanted pauses. This looks a bit like an XY problem though, and there might be a more elegant solution to accomplish your original goal. Can you tell us a bit more about the feature this coroutine is serving? We might be able to support that feature in a different way. – DMGregory Nov 15 '17 at 18:12
• it has to search a grid area to mark enclosed spaces. I had a few other solutions but the problem is that the spaces are irregular. So this essentially propagates itself across all grid tiles that could be linked together, because of the recursion I know that no tiles will be missed. – user3797758 Nov 15 '17 at 18:18
• That's a start. Please edit your question to include this information, and explain at what points in the graph search you do and don't want it to pause. – DMGregory Nov 15 '17 at 18:23
• Ahh, yeah that works. Though it does make the code kinda clunky – user3797758 Nov 15 '17 at 19:08
• It does, that's why I suspect a more elegant solution might be possible if you edit your question to describe the root graph search problem you're trying to solve and how you want that search to behave. – DMGregory Nov 15 '17 at 20:25

Though the comments on this question I was able to find a solution to this. If I transfer it over to the example I posted above it would look like this:

//global valiables
private const int Speed = 200; //amount of iterations per frame
private int skipCount = 0; //counter for the iterations

private IEnumerator PropagateSearch(arg0, arg1)
{
if ((skipCount = ++skipCount % Speed) == 0)
yield return null;

//work here

{
IEnumerator count = PropagateSearch(arg0, arg1);
while (count.MoveNext()) if (skipCount % Speed == 0) yield return null;
}

{
IEnumerator count = PropagateSearch(arg0, arg1);
while (count.MoveNext()) if (skipCount % Speed == 0) yield return null;
}

{
IEnumerator count = PropagateSearch(arg0, arg1);
while (count.MoveNext()) if (skipCount % Speed == 0) yield return null;
}

{
IEnumerator count = PropagateSearch(arg0, arg1);
while (count.MoveNext()) if (skipCount % Speed == 0) yield return null;
}
}


This will go through the co-routine and will only break when there it iterates Speed amount of times. With that you can fine tune the amount of work that happens per frame.