One of the advantages of behavior trees are that they are very flexible in that all of the tasks are loosely coupled – meaning one task doesn’t depend on another task to operate. The drawback of this is that sometimes you need tasks to share information with each other. For example, you may have one task that is determine if a target is Within Sight. If the target is within sight you might have another task Move Towards the target. In this case the two tasks need to communicate with each other so the Move Towards task actually moves in the direction of the same object that the Within Sight task found. In traditional behavior tree implementations this is solved by coding a blackboard. With Behavior Designer it is a lot easier in that you can use variables.

In our previous example we had two tasks: one that determined if the target is within sight and then the other task moves towards the target. This tree looks like:  

The code for both of these tasks is discussed in the Writing a New Task topic, but the part that deals with variables is in this variable declaration:

public SharedTransform target;

With the SharedTransform variable created, we can now create a new variable within Behavior Designer and assign that variable to the two tasks:

Switch to the task inspector and assign that variable to the two tasks:

And with that the two tasks can start to share information! You can get/set the value of the shared variable by accessing the Value property. For example, target.Value will return the transform object. When Within Sight runs it will assign the transform of the object that comes within sight to the Target variable. When Move Towards runs it will use that Target variable to determine what position to move towards.

Looking at the variable within the inspector, if you look to the left of the delete button you’ll see a triangle pointing to the right. This is the button for Variable Mappings.

Variable Mappings allow your SharedVariable to map to a property of the same type. This allows you to quickly get or set a value on a MonoBehaviour component. As an example, lets say that we want to get the position of the agent. It is possible to use the Get Position task with a non-mapped variable, but that adds unnecessary tasks to your behavior tree. Instead, if you map the variable to the Transform.position property, whenever the value of the variable is accessed, it will instead use the property that it is mapped to. This allows you to get or set the position of the Transform without any extra tasks.

Note: Variable Mappings require a C# property. It does not work on C# fields. With the above Vector3 example, you can setup a property mapping with the following:

public class MyMonoBehaviour : MonoBehaviour
   private Vector3 m_MyVector3;
   public Vector3 MyVector3 { get { return m_MyVector3; } set { m_MyVector3 = value; } } // Required for property mappings.

Behavior Designer supports both local and global variables. Global Variables are similar to local variables except any tree can reference the same variable. Variables can be referenced by non-Task derived classes by getting a reference to from the behavior tree.

The following shared variable types are included in the default Behavior Designer installation. If none of these types are suitable for your situation then you can create your own shared variable:

  • SharedAnimationCurve
  • SharedBool
  • SharedColor
  • SharedFloat
  • SharedGameObject
  • SharedGameObjectList
  • SharedInt
  • SharedMaterial
  • SharedObject
  • SharedObjectList
  • SharedQuaternion
  • SharedRect
  • SharedString
  • SharedTransform
  • SharedTransformList
  • SharedVector2
  • SharedVector3Int
  • SharedVector3
  • SharedVector3Int
  • SharedVector4