Recommended way to code up async tasks

scornflake

New member
What's the recommended way to code up an action, such that it'll return TaskStatus.Running until its completed? I'm thinking here of a long running coroutine (over a number of seconds).
 
I'm wanting to make a general 'wait for coroutine to finish' style task wrapper.
If there's a more elegant way than below, love to hear it (ofc, the below coroutine itself it just for testing)


Code:
    public class CoroutineAction : Action
    {
        public bool IsRunning = false;

        public override void OnStart()
        {
            StartCoroutine(CoroutineWithEndingBit());
        }

        private IEnumerator CoroutineWithEndingBit()
        {
            IsRunning = true;
            yield return TasksCoroutine();
            IsRunning = false;
        }

        protected IEnumerator TasksCoroutine()
        {
            for (int i = 0; i < 50; i++)
            {
                yield return new WaitForSeconds(1);
                Debug.Log($"Waiting.... {i}");
            }

            yield return null;
        }

        public override TaskStatus OnUpdate()
        {
            return IsRunning ? TaskStatus.Running : TaskStatus.Success;
        }
    }
 
The behavior tree task is not able to return a status from a couroutine. You'll need to go the flag route for this use case.
 
Hope this is not a too stupid question, but what do you mean with set a flag? If i google it it's more confuse me how to use them in a BT
 
Top