In this specific case, the solution is relatively simple: make Wait()
return just the integer:
public IEnumerable Update()
{
while (true)
{
ShootAtPlayer();
yield return Wait(30);
}
}
private int Wait(int x)
{
return removeScript ? -1 : x;
}
In more complicated cases, you can use foreach
, though this makes the syntax much more verbose:
public IEnumerable Update()
{
while (true)
{
ShootAtPlayer();
foreach (var x in Wait(30))
yield return x;
}
}
private IEnumerable Wait(int x)
{
yield return removeScript ? -1 : x;
}
As you can see, you can (ab)use yield return
to implement fibers, but yield return
was never meant for this, so it won't work that well.
What was made for this kind of asynchronous continuations is the new async
-await
. With that, your code could look for example something like this:
public async Task Update()
{
while (true)
{
ShootAtPlayer();
await Wait(30);
}
}
private async Task Wait(int x)
{
await fiber.Wait(removeScript ? -1 : x);
}
As a final note, I think the way you're using removeScript
is not a good idea. End of a script should be signified by the Update()
method actually completing (the enumerable doesn't have any more items, or the Task
completes), not by returning a magic value.