Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to have a CoRoutine declared as virtual in parent class and then overridden in child classes.

protected override IEnumerator f(Vector3 to)
{
...
    yield return base.f(to);
...
}

What happens (or doesn't happen) is that the base code doesn't get executed. Debuggin it I step into the base.f(to) and the cursor just stand on function name. If I press step next control returns to the child.

I really have no clue how to handle this situation. Any clue?

share|improve this question

IEnumerators (more specifically, yield return) are special and magic pieces of syntax sugar.

What is happening is that the compiler creates (at compile time) a class that packages all the state of the function you wrote. And then substitutes the function with a stub that creates an instance of the class and returns it.

So you're not running your code in yield return base.f(to), you're just creating an object and returning it.

CoRoutines are also special and magic pieces of Unity, they iterate over what is returned and if given some special type of value it does something else special.

These things include:

  • Scheduling the CoRoutine to be continued at some later point in time
  • I have no idea I don't use that much Unity to be honest :)

So what you're returning from your CoRoutine (the override) is an IEnumerator, Unity doesn't know what to do with the coroutine so it doesn't do anything special and asks for the next value. Which runs the bit after your yield return and... stops, because there's nothing more to be done.

What you should do is this:

protected override IEnumerator f(Vector3 to)
{
...
    var baseVal = base.f(to);
    while ( true ) {
       yield return baseVal.Current;
       if ( baseVal.MoveNext() == False ) break;
    }
...
}

I don't have access to an Unity setup at this time so this is untested, I also don't know what happens with an IEnumerator that never returns a single value (or if that is even legal).

share|improve this answer
    
Here's a question about what values you can (usefully) return from coroutines, gamedev.stackexchange.com/questions/81377/… – Kevin van der Velden Apr 18 at 9:56

Try this instead:

  protected override IEnumerator f(Vector3 to)
  {
  ...
      yield return StartCoroutine(base.f(to));
  ...
  }

Coroutine inherits from YieldInstruction, and yielding on one will cause your coroutine to pause until the other coroutine completes.

share|improve this answer
    
Does this actually work? From what I've read the only return values understood to do anything but delay a frame are YieldInstructions, I would think a coroutine wouldn't fit. – Kevin van der Velden 1 hour ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.