Subtrees
Subtrees store the behavior tree data within a ScriptableObject allowing you to swap out behaviors on the same GameObject. Subtrees can also be nested within the behavior tree using the Subtree Reference task.
Choose how to create the Subtree
Behavior Designer Pro exposes two different editor workflows:
| Workflow | Result |
|---|---|
| Export in the operations toolbar | Copies the entire current graph into a new Subtree asset. The source graph remains unchanged. Use this when the whole graph should become a reusable asset or be assigned to a Behavior Tree component. |
| Right-click a task selection and choose Save as Subtree | Extracts a branch into a new Subtree asset and replaces that branch with a Subtree Reference. Use this while refactoring part of an existing tree. |
Save a selected branch as a Subtree
- Select one or more logic tasks in the branch that should become reusable. Event nodes cannot be included.
- Right-click the selection and choose Save as Subtree.
- Choose the asset path and name.
- Inspect the new Subtree Reference and its variable overrides, then open the Subtree asset to confirm the exported branch.
The selected tasks identify the branch root; they are not necessarily the exact set that will be exported. With multiple tasks selected, Behavior Designer finds their lowest common ancestor and saves that task plus all of its descendants. The command is available only when the selection has a common branch and contains at least one logic task.
After the save, the source branch is replaced by a Subtree Reference. Shared Variables that are externally configurable can be exposed as overrides on the reference. A Graph variable marked Internal remains usable inside the Subtree but is intentionally omitted from those override controls. See Variables for the Internal command.
Checkpoint: the parent tree contains one Subtree Reference where the branch used to be, opening the asset shows the expected ancestor and descendants, and the parent tree still reaches the same behavior through that reference.
Subtree Reference Task
The Subtree Reference task loads the assets in its Subtrees field when the graph initializes. To select them dynamically, derive from SubtreeReference or implement ISubtreeReferenceNode. This example chooses one asset at random:
using Opsive.BehaviorDesigner.Runtime.Tasks.Actions;
using Opsive.BehaviorDesigner.Runtime;
using Opsive.GraphDesigner.Runtime;
using UnityEngine;
public class RandomSubtreeReference : SubtreeReference
{
private Subtree[] m_Selection;
public override Subtree[] Subtrees => m_Selection != null ? m_Selection : m_Subtrees;
/// <summary>
/// Performs any runtime operations to evaluate the array of subtrees that should be returned.
/// </summary>
public override void EvaluateSubgraphs(IGraphComponent graphComponent)
{
m_Selection = null;
if (m_Subtrees == null || m_Subtrees.Length == 0) {
return;
}
m_Selection = new Subtree[] { m_Subtrees[Random.Range(0, m_Subtrees.Length)] };
}
}
Before reading Subtrees, the graph calls EvaluateSubgraphs(IGraphComponent) with the owning component. The override chooses an asset and Subtrees returns that selection. The older EvaluateSubtrees(IGraphComponent) name is deprecated; a parameterless override does not match the API.
Variable Overrides
When a subtree is assigned to the Subgraph field of the Behavior Tree component the graph variables added to the subtree will automatically be added to your Behavior Tree component. This allows you to override the subtree variable with a value local to that graph. The variable name and type are not editable because it needs to match the same variable on the subtree.

Variables can also be overridden using the Subtree Reference task. Within the Subtree Reference task inspector you’ll have a similar override interface:

Reevaluation
When the tree loads, each Subtree Reference is replaced by its selected subtree nodes. Call BehaviorTree.ReevaluateSubtreeReferences() to select again at runtime: it invokes EvaluateSubgraphs(IGraphComponent), reads Subtrees, rebuilds the references, and restarts the tree.
Pooling
Subtrees can be pooled for better performance when switching between many subtrees. When the subtree is instantiated the Deserialize method should be called to deserialize the tree. Ensure the Pooled property is also set to true after the tree is deserialized. This will ensure Behavior Designer knows that it should not duplicate any of the instantiated tasks. Pooled subtrees can then be assigned to the behavior tree component just like regular subtrees. For example:
Pooling is not required when several Behavior Tree components reuse the same authored Subtree asset. Assign that asset directly and each component receives its own runtime Task and Graph-variable state. Use the pooling API only when the project explicitly manages instantiated runtime Subtrees, and never give one pooled instance to two concurrently running Behavior Tree components.
using Opsive.BehaviorDesigner.Runtime;
using UnityEngine;
public class SubtreePooler : MonoBehaviour
{
[Tooltip("A reference to the behavior tree that the subtree should be assigned to.")]
public BehaviorTree m_BehaviorTree;
[Tooltip("The subtree that should be pooled.")]
public Subtree m_Subtree;
[Tooltip("Specifies how large the pool should be.")]
public int m_PoolSize = 5;
private Subtree[] m_SubtreePool;
private int m_Index;
/// <summary>
/// Pools the subtrees.
/// </summary>
private void Awake()
{
m_SubtreePool = new Subtree[m_PoolSize];
for (int i = 0; i < m_PoolSize; ++i) {
m_SubtreePool[i] = Object.Instantiate(m_Subtree);
m_SubtreePool[i].Deserialize();
m_SubtreePool[i].Pooled = true;
}
}
/// <summary>
/// Assigns a new pooled subtree to the behavior tree.
/// </summary>
public void Assign()
{
m_BehaviorTree.Subgraph = m_SubtreePool[m_Index];
m_Index = (m_Index + 1) % m_PoolSize;
}
}
Related:
Switch Subtrees compares a fixed Subtree Reference with choosing or replacing a subtree at runtime.
For graph navigation, connection layout, panels, and shortcuts, see the Overview. Task-specific row and Inspector controls are documented with Tasks.
When a tree does not start or chooses the wrong branch, use Debugging, which begins with the Error Window and continues through Play Mode status and variable inspection.