Custom composite task always return failure when recover from a conditional abort

YnS

New member
I have a custom composite task named ExecuteInOrder which controls the execution order of my behavior tree. It uses a combat controller which generates the orders. Outside the composite task I have a custom conditional task to interrupt ExecuteInOrder and perform some other game logic. But whenever the conditional task is triggered and executed successfully, the ExecuteInOrder will always return failure because of the (childStatus == failure), I am thinking that I should reset the child status in the OnConditionalAbort method? If so, how should I reset the child status correctly?

Code:
public class ExecuteInOrder : Composite
{
        [RequiredField]
        public CombatController combatController;
        
        public int currentChildIndex;

        public SharedBool stageChanged;

        private bool firstStart;
        
        #region Override

        public override void OnStart()
        {
            if (stageChanged.Value || !firstStart)
            {
                currentChildIndex = combatController.skillOrder != null ? combatController.skillOrder[combatController.orderIndex] : 0;
                stageChanged.Value = false;
                if (!firstStart)
                {
                    firstStart = true;
                }
            }
        }
        
        public override int CurrentChildIndex()
        {
            return currentChildIndex;
        }

        public override bool CanRunParallelChildren()
        {
            return false;
        }

        public override bool CanExecute()
        {
            var value = currentChildIndex < combatController.skillOrder.Count;
            return combatController != null && value;
        }

        public override void OnChildStarted()
        {
            combatController.SetSkillStatus();
        }

        public override void OnChildExecuted(TaskStatus childStatus)
        {
            //This part failed everytime get back from conditional abort
            if (childStatus == TaskStatus.Failure)
            {
                Debug.Log($"Execute Failed!!!: {currentChildIndex}");
                currentChildIndex = combatController.skillOrder[combatController.orderIndex];
            }
            else
            {
                combatController.orderIndex++;
                currentChildIndex = combatController.skillOrder[combatController.orderIndex];
            }
        }

        public override void OnConditionalAbort(int childIndex)
        {
            currentChildIndex = 0;
        }

        public override void OnEnd()
        {
            if (stageChanged.Value)
            {
                currentChildIndex = 0;
            }
        }

        public override void OnReset()
        {
            currentChildIndex = 0;
        }
    
        #endregion
        

        public void ResetData()
        {
            currentChildIndex = 0;
        }

}
 
I am thinking that I should reset the child status in the OnConditionalAbort method?
Yes, that sounds like the correct approach. You could do something similar to what the Sequence task does:
Code:
        public override void OnConditionalAbort(int childIndex)
        {
            // Set the current child index to the index that caused the abort
            currentChildIndex = childIndex;
            executionStatus = TaskStatus.Inactive;
        }
 
Top