In this lesson, you will learn how to use a centralized Blackboard to share data between Behavior Tree nodes, allowing your AI to "remember" information across complex decision paths.
Why Do We Need a Blackboard?
As your Behavior Tree grows, individual nodes often need to share information—like the "last seen position" of a player or whether the AI is currently "low on health." If you pass this data manually through every node, your tree becomes tightly coupled and difficult to maintain. A Blackboard acts as a shared memory container (a simple object or dictionary) that all nodes in a tree can read from or write to.
Implementing the Shared Data Store
Think of the Blackboard as a specialized script that acts as the "brain's notepad." Instead of nodes storing local variables, they reference this central object to coordinate their behavior.
public class AIBlackboard : MonoBehaviour
{
// Shared state variables
public Transform targetPlayer;
public Vector3 lastKnownPosition;
public bool isAggressive = false;
}
Connecting Nodes to the Blackboard
When your Behavior Tree processes a branch, you inject this AIBlackboard reference into each node during initialization. A "Sense Player" node might write to lastKnownPosition, while a "Move To Target" node reads that same value to update the NavMeshAgent destination.
public class MoveToTargetNode : ActionNode
{
private AIBlackboard _blackboard;
public MoveToTargetNode(AIBlackboard blackboard) => _blackboard = blackboard;
public override NodeState Evaluate()
{
if (_blackboard.targetPlayer != null)
{
agent.SetDestination(_blackboard.targetPlayer.position);
return NodeState.Success;
}
return NodeState.Failure;
}
}
By decoupling the data from the node logic, you create a system where nodes can be swapped or reordered without breaking the flow of information. If the player goes behind a wall, your "Sensor" node simply updates the lastKnownPosition in the Blackboard, and the "Movement" node automatically reacts during the next tick without needing a direct link to the sensor itself.
Key Takeaway: A Blackboard system decouples AI logic from data storage, allowing nodes to act as independent modules that communicate through a shared, centralized state container.