Collapsable decorator

I was trying to create a decorator that would collapse branches when entering/exiting, but I guess it break some stuff if used on a sequence with a conditional abort. Is there some way to use decorators like this that get triggered when a branch is started/ended? I've had that need several times now since I use a lot of external trees, for example if current branch is a profession that runs a production, stop production when branch ends, I do this now using my own repeater that triggers this logic OnEnd, but would be tidier with a decorator.

Code:
using BehaviorDesigner.Runtime.Tasks;

[TaskDescription(@"Will collpase children when not active")]
public class Collapsable : Decorator
{
    public override void OnAwake()
    {
        base.OnAwake();

        NodeData.Collapsed = true;
    }

    public override void OnStart()
    {
        base.OnStart();

        NodeData.Collapsed = false;
    }

    public override void OnEnd()
    {
        base.OnEnd();

        NodeData.Collapsed = true;
    }
}

I tried putting it on top of each external behaviour here (each one is its own profession) since at play this becomes a huge mess of nodes, and it does collapse everything, but it breaks the logic somewhere.

1634499347778.png
 

Justin

Administrator
Staff member
What does it break with conditional aborts? The Collapsed property is used only by the editor so it shouldn't cause any changes in execution based on it being true/false.
 
What does it break with conditional aborts? The Collapsed property is used only by the editor so it shouldn't cause any changes in execution based on it being true/false.

Sorry for delay, basically any branch with the decorator gets stuck before exiting, the below tree works fine normally, but if I add the Collapsable decorator it gets stuck when it is supposed to leave this branch and just keep executing first node (has received event):

1634701273148.png
 
I realised now its because I didnt track executionStatus. I do have one question tho, is "Decorate" always called OnEnd?

Code:
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;

namespace Nielsen
{
    [TaskCategory("Custom/Other")]
    [TaskDescription("Will reset the assigned variables on end")]
    public class ResetVariablesOnEnd : Decorator
    {
        public SharedVariable[] variables;

        // The status of the child after it has finished running.
        private TaskStatus executionStatus = TaskStatus.Inactive;

        public override bool CanExecute()
        {
            // Continue executing until the child task returns success or failure.
            return executionStatus == TaskStatus.Inactive || executionStatus == TaskStatus.Running;
        }

        public override void OnChildExecuted(TaskStatus childStatus)
        {
            // Update the execution status after a child has finished running.
            executionStatus = childStatus;
        }

        public override TaskStatus Decorate(TaskStatus status)
        {
            foreach (SharedVariable variable in variables)
            {
                variable.SetValue(null);
            }

            return status;
        }

        public override void OnEnd()
        {
            // Reset the execution status back to its starting values.
            executionStatus = TaskStatus.Inactive;
        }
    }
}
 
Top