Wait until coroutine is completed

lennon

New member
Hi, I want my tasks to return sucess only once a coroutine has actually ended (like a yield return).
Something like this:


public class MyAction : Action
{
[RequiredField]
public SharedMyClass myClass;

public override TaskStatus OnUpdate()
{
//My coroutine
yield return StartCoroutine(myClass.MyCoroutine());

//Return success ONLY once the coroutine has finished!
return TaskStatus.Success;

}
}



I'm looking for a solution that may work on my current coroutines without me needing to alter them or move them to the BT script.
 
OnUpdate doesn't return an IEnumerator so you'll need to create a new method that starts the coroutines and then return success as soon as that corotuine completes (by setting a flag).
 
For future reference, I want to say that I found a pretty cool coroutine wrapper already scripted that allows you to check if a coroutine is finished, if it's running, etc. You can also pause the execution.
It's in this thread, you just need to create a script and add it to your project:

Example of use in a BT task (you just need to put change your coroutine's name):

[RequiredField]
public SharedMyCharacter myCharacterScript;
private Task t;

public override void OnStart()
{
t = new Task(
myCharacterScript.Value.myCoroutine());
}

public override TaskStatus OnUpdate()
{

if (t.Running) { return TaskStatus.Running; }
else
{
return TaskStatus.Success;
}



This is pretty good for me, as I believe that it's better to just code coroutines and have my BT Task's calling those... also, I already have many coroutines that I want to use inside my tree without having to re-write them. Coroutines are a bit more intuitive to me, and I can make a snippet to write the BT tasks in two clicks just changing the coroutine's name. That way I seldom have to worry about creating shared variables, and recoverying a character's script value and some other stuff. It just helps with the learning curve. It seems a bit more compatible too as my actions are coroutines accesible to any script outside of the BT scope.
I specially like this for bigger blocks of actions that you know are solid. Actions like say "enter cover" that require a bunch of steps like walk to a point, wait till the guy reaches, then crouch, wait for the animation and then set a bool in the animator and wait till the cover pose has been reached. Better to have this blocks that won't change and don't need visual debugging than a million nodes that will convolute the BT. And still you can visually see what's happening and leave the BT for what it should be.
Justin, maybe you should consider adding this code to your package, I think we can all benefit from it.
Anyway, thanks.
 
Last edited:
Top