I have a base class called Ability and then children classes for each ability like BasicMeleeStrike. I have a turn based game so the Do() method of each ability does it's logic sequentially. I'm trying to make coding a little nicer looking in each child ability class and I want to not have to yield out from in them but call base class functions that will do the operations over time but I want them to wait until they come back to the child class Do() function but what I have doesn't work. Is this possible?
class Ability{
public virtual IEnumerator Do(Actor target){}
protected IEnumerator JumpToTarget(Actor target){
yield return StartCoroutine(_JumpToTarget(target);
}
private IEnumerator _JumpToTarget(Actor target){
// inside here I tween to a location so it's yielding return null until it's done
}
}
class BasicMeleeAttack : Ability{
public override IEnumerator Do(Actor target){
JumpToTarget(target);
anim.Play("attack");
}
}
No jumping happens but the attack animation plays. When I put a breakpoint on anim.Play() it's hit right away. Is there a way to make it stay inside JumpToTarget(target) until it's finished without having to do a yield inside BasicMeleeAttack's Do() function? I just think it's much friendlier to not have to code that for all my actions.
return
,yield
,await
, or the end of the method (or anException
is thrown...). So if you want execution to pause somewhere mid-stream, you have to tell it so using theyield
orawait
keywords. Why is it that you don't want to put ayield
insideDo()
? It's no more lines than you have now, and it's more explicit about the desired behaviour, so it seems like a win-win. \$\endgroup\$yield
then you're making an explicit statement "DO NOT wait for this IEnumerator/coroutine to finish before proceeding." If that's not what you mean, then you need to tell the compiler what you want it to do instead. \$\endgroup\$