External Behavior Trees

In some cases you may have a behavior tree that you want to run from multiple objects. For example, you could have a behavior tree that patrols a room. Instead of creating a separate behavior tree for each unit you can instead use an external behavior tree. An external behavior tree is referenced using the Behavior Tree Reference task. When the original behavior tree starts running it will load all of the tasks within the external behavior tree and act like they are its own. Any SharedVariable within the external tree of the same name and type of its parent tree will automatically be overridden. For example, if the parent tree has a SharedInt named “MyInt” with a value of 7, and the External Tree has a SharedInt named “MyInt” with a value of 0, when the tree runs MyInt will have a value of 7 within the External Tree.

Behavior Tree Reference

The Behavior Tree Reference task allows you to run another behavior tree within the current behavior tree. You can create this behavior tree by saving the tree as an external behavior tree. One use for this is that if you have an unit that plays a series of tasks to attack. You may want the unit to attack at different points within the behavior tree, and you want that attack to always be the same. Instead of copying and pasting the same tasks over and over you can just use an external behavior and then the tasks are always guaranteed to be the same. This example is demonstrated in the RTS sample project located on the samples page.

The GetExternalBehaviors method allows you to override it so you can provide an external behavior tree array that is determined at runtime.

The Behavior Tree Reference task allows you to specify variables per-reference task. For most cases this is not needed because variables will automatically be transferring from the parent tree to the External Tree. However, if you have multiple Behavior Tree Reference tasks in the same tree all pointing to the same External Tree, you may want to be able to specify different variable values per tree. In this situation you can specify the variable value on the Behavior Tree Reference task and that variable value will be transferred to the External Tree.

Pooling

External behavior trees can be pooled for better performance when switching between many external trees. When the external behavior tree is instantiated the Init method should be called to deserialize the external behavior tree. Pooled external behavior trees can then be assigned to the behavior tree component just like non pooled external behavior trees. For example:

using UnityEngine;
using BehaviorDesigner.Runtime;

public class ExternalPoolExample : MonoBehaviour {
    public BehaviorTree behaviorTree;
    public ExternalBehavior externalBehaviorTree;

    private ExternalBehavior[] externalPool;
    private int index;

    public void Awake()
    {
        // Instantiate five External Behavior Tree which will be reused. The Init method will deserialize the External Behavior Tree.
        externalPool = new ExternalBehavior[5];
        for (int i = 0; i < externalPool.Length; ++i) {
            externalPool[i] = Object.Instantiate(externalBehaviorTree);
            externalPool[i].Init();
        }
    }

    public void OnGUI()
    {
        // Assign the next External Behavior Tree within the simplified pool.
        if (GUILayout.Button("Assign")) {
            behaviorTree.DisableBehavior();
            behaviorTree.ExternalBehavior = externalPool[index];
            behaviorTree.EnableBehavior();
            index = (index + 1) % externalPool.Length;
        }
    }
}