Idiomatic statemachine with none looping nodes

I’m very new to Behavior and learning how to use it for maybe unintended purposes, in my case specifically UI management. I’m sorry if this question has been asked before.

I’m trying to make a simple state machine. The state will be changed from outside the behavior Graph. In a webinar I’ve found to use a combination of Restart If and Switch nodes. However the following (Test A) doesn’t work because the console message is spammed continuously.

Turning Repeat off on the On Start node doesn’t work either, because it would seem the Restart If is no longer listening if the branch is finished. In the example in the webinar the branch was kept alive because the actions after the switch were looping. For me this is not the case.

I did try to get to working in the ‘programmer’ way (Test B), but this doesn’t feel very idiomatic to me (besides the fact that the Console gets spammed with warnings because nothing is attached to the True node).

Does anyone have advice for a good way to make a Behavior state machine when the actions after the switch are not looping?

While awaiting a response I’ve come up with something simple that seems to work. Note that it doesn’t do any safety checks like check if the branch is still running. I will delete this if a better solution comes up.

OnVariableChangeEvent.cs

using System;
using Unity.Behavior;
using UnityEngine;
using Modifier = Unity.Behavior.Modifier;
using Unity.Properties;

[Serializable, GeneratePropertyBag]
[NodeDescription(
    name: "Variable Changed",
    story: "[Variable] changed",
    category: "Events",
    id: "eca36a9b7e3d9074ccae000eed6c2f8c"
)]
public partial class OnVariableChangeEvent : Modifier
{
    [SerializeReference] public BlackboardVariable Variable;

    protected override Status OnStart()
    {
        if (Variable == null)
        {
            return Status.Failure;
        }

        Variable.OnValueChanged += OnValueChanged;

        return Status.Waiting;
    }

    protected override Status OnUpdate()
    {
        return Status.Waiting;
    }

    protected override void OnEnd()
    {
        if (Variable != null)
        {
            Variable.OnValueChanged += OnValueChanged;
        }
    }

    private void OnValueChanged()
    {
        StartNode(Child);
    }
}

Hi @Ghanzka,

Have you already looked at the Behavior Demo on the store?

I believe there are some examples there that could help you.

Although, what you have done is a smart way of doing things also :slight_smile: