Failing to understand something simple about setting shared variables in a script

smogger914

New member
Super new to behavior trees and trying to figure out how this should work.

I am trying to set a sharedVariable for a tree when I initialize it like this:

C#:
GameObject characterGameObject = new GameObject();
var behaviorTree = characterGameObject.AddComponent<BehaviorTree>();
behaviorTree.ExternalBehavior = characterBehaviorTree;
behaviorTree.SetVariable("myFloat", (SharedFloat)26f);

I have a simple task that prints out the myFloat variable.

C#:
using UnityEngine;
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;

[TaskCategory("Common")]
public class CharacterHasJob : Conditional
{
  public SharedFloat myFloat;

  public override void OnAwake()
  {
    Debug.Log("CharacterHasJob myFloat: " + myFloat.ToString());
  }

  public override TaskStatus OnUpdate()
  {
    return TaskStatus.Success;
  }
}

The value that is printed out is always zero, i.e. what is set in the variables tab.

Screen Shot 2022-10-08 at 2.21.22 PM.png

I have also mapped the variable to the Task in the UI.

Screen Shot 2022-10-08 at 2.22.01 PM.png

I feel like I am missing something super simple and it should work.

Any help is greatly appreciated!
 
Hi I just made a quick test, my code worked so my assumption is that for ExternalBehavior your declaring characterBehaviorTree as an ExternalBehavior rather than ExternalBehaviorTree, and you should be using SetVariableValue.

C#:
    public ExternalBehaviorTree characterBehaviorTree;
    void Start()
    {
        GameObject characterGameObject = new GameObject();
        var behaviorTree = characterGameObject.AddComponent<BehaviorTree>();
        behaviorTree.ExternalBehavior = characterBehaviorTree;
        behaviorTree.SetVariableValue("Search Distance", (SharedFloat)26f);
    }
 
Top