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).
 

scornflake

New member
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;
        }
    }
 
Top