1
\$\begingroup\$

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?

\$\endgroup\$
7
  • \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Commented Nov 15, 2017 at 18:12
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Nov 15, 2017 at 18:18
  • \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Commented Nov 15, 2017 at 18:23
  • \$\begingroup\$ Ahh, yeah that works. Though it does make the code kinda clunky \$\endgroup\$ Commented Nov 15, 2017 at 19:08
  • \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Commented Nov 15, 2017 at 20:25

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .