- Home /
Is it possible to write a yield statement that only yields on a conditional?
Weird question on the surface, I know. Basically, I want to do the following:
if (condition) {
yield return new WaitForSeconds(waitTime);
}
But I want to write a method of some kind to make it easier to write, since I'm doing it a lot.
For context, I'm using WaitForSeconds()
in a bunch of places, but I want to be able to set a static flag that will turn them off so I can more quickly test the outcomes of some automatic processes under different settings.
I tried making a custom yield instruction WaitForSecondsIfCondition()
with a conditional inside it, but I realized that even waiting for zero seconds is still like a yield return null
, which waits for a frame, and even this slows things down more than I'd like because I have the yield instructions in some loops (to create a cascading effect with some animations).
Anyway, while it's annoying, it's not all that bad to wrap the if statement around the WaitForSeconds()
instruction when testing, so I'll do that for now.
Answer by Bunny83 · Jul 16, 2020 at 12:50 AM
No, this is not really possible, not without some kind of wrapper when you start the coroutine. With a wrapper it's kinda easy. If you just want to get rid of all yield instructions all together, you can simply iterate the IEnumerator manually in a loop. If you just want to get rid of "certain" yield instructions you could create a specialized "yield instruction" / class that your wrapper will filter out and act accordingly.
static bool shouldIYield = true;
IEnumerator Wrapper(IEnumerator aCoroutine)
{
while (aCoroutine.MoveNext())
{
var c = aCoroutine.Current;
if (shouldIYield)
yield return c;
}
}
With this wrapper you would need to start your coroutine like this:
StartCoroutine( Wrapper( YourCoroutine() ) );
Of course this could be packed into an extension method
public static class CoroutineWrapperStart
{
public static Coroutine SCoroutine(this MonoBehaviour aMB, IEnumerator aCoroutine)
{
return aMB.StartCoroutine(Wrapper(aCoroutine));
}
IEnumerator Wrapper(IEnumerator aCoroutine)
{
// [ ... ]
}
}
With that little class you could simply use:
this.SCoroutine(YourCoroutine());
instead of
StartCoroutine(YourCoroutine());
To handle a custom yield instruction in the wrapper you could use a wrapper for the actual yield value ^^
public class ConditionalYield
{
public object yieldValue;
public ConditionalYield(object aYieldValue)
{
yieldValue = aYieldValue;
}
}
In the wrapper you could simply do this
var conditional = c as ConditionalYield;
if (conditional != null && shouldIYield)
yield return conditional.yieldValue;
else if (conditional == null)
yield return c;
instead of
if (shouldIYield)
yield return c;
Now with the "ConditionalYield" class you could do this inside your coroutine:
// [ ... ]
yield return new ConditionalYield(new WaitForSeconds(xyz));
// [ ... ]
This should be caught by our wrapper and the WaitForSeconds should only be used when our global flag is set to true. In this case we can use "normal" yield statements which would still work as before but only those which are wrapped in our ConditionalYield class would be ignored.
Thanks, this appears to work.
I have a (hopefully) quick followup question. I tried making a subclass of IEnumerator ConditionallyYieldingIEnumerator
that would always execute the Wrapper's functionality inside $$anonymous$$oveNext()
. $$anonymous$$y hope was to be able to use StartCoroutine(myConditionallyYieldingCoroutine)
as normal and also be able to start the coroutine from a yield statement inside another coroutine. When trying to define a coroutine with ConditionallyYieldingIEnumerator
as the return type, I got the error message "cannot be an iterator block because ConditionallyYieldingIEnumerator is not an iterator interface type". This surprised me, because it's a subclass of IEnumerator. From what I could find in existing forums and wading in the documentation, it seems like what I'm trying to do isn't allowed. Do you know whether that's true?
In the end, I just made Wrapper
public to use it from other yield statements, so it does everything I want it to. But since I only want the conditional yielding in a specific set of coroutines, I thought it would be nice to set it up when those coroutines are defined. Any insights appreciated, however brief.
Answer by Addyarb · Jul 16, 2020 at 01:10 AM
You may consider using the newly-implemented async/await pattern, which will get you a lot closer to what you want. The equivalent of WaitForSeconds(0) will become completely synchronous, rather than necessarily waiting for a single frame due to it not being based off of the C# generator system. Writing your own tasks (similar to YieldInstruction in this context) is also a lot easier and more intuitive. There's a free asset on the Unity Asset Store I like to use which gives useful extensions. Check out their documentation.
Good luck!
This does seem quite useful. I'm hesitant to rework my existing code just for testing purposes, but I'll try setting things up this way in future projects. Thanks for sharing!
Answer by tuinal · Jul 16, 2020 at 02:35 AM
If I understand the question correctly this is possible & straightforward:
bool continue
...
yield return new WaitUntil(()=>continue)
If I understand this answer correctly, it will always yield for one frame when continue
is false (not desired). Is that right?
I also want each conditional yield statement to be equivalent to WaitForSeconds(waitTime)
when continue
is true (waitTime varies), but I don't see a way to do that with a lambda expression that would be easier than wrapping a WaitForSeconds()
line in an if. Could be that I'm misunderstanding.
Ah, you want to break out of the WaitForSeconds? You can modify a variable in a lambda expression; the below should do this whilst still keeping a timer:
bool cont;
...
float timer = 0f;
float waitTime = 5f;
yield return new WaitUntil(() => (timer += Time.deltaTime) > waitTime || cont);
For my specific purpose, it still seems simpler to just use the if statement to skip the yield altogether rather than creating a new timer variable each time I want to do this. This does seem like a useful trick, though, which I'll keep in $$anonymous$$d. Appreciate you sharing it.