The Blackboard
Behavior tree nodes need to share data. A "find cover" node computes a position that "move to" needs later; a perception system finds a target that both "aim" and "shoot" use. Passing that data through constructor arguments quickly becomes a tangle. The standard answer is a blackboard: a small key-value store owned by the agent, which every node can read and write by key.
Perception ──writes──►┌──────────────── Blackboard ────────────────┐
│ TargetActor = Player │
│ LastKnownPos = (12, 4) │◄──reads── "move to LastKnownPos"
"find cover" ─writes─►│ CoverPos = (8, 6) │◄──reads── "move to CoverPos"
│ Alertness = 0.7 │◄──reads── decorator "Alertness > 0.5"
└────────────────────────────────────────────┘The name comes from the 1980s "blackboard architecture" of AI research, where independent experts post partial results on a shared board. In games it's simpler: a typed dictionary plus change notifications.
What a good blackboard gives you
| Feature | Why it matters |
|---|---|
| Named, typed keys declared up front | a typo or wrong type is an error at load time, not a silent null at runtime |
| Is-set state separate from the value | "no target" is different from "target at (0, 0)" |
| Change observers | decorators can abort a running branch the moment a key changes, instead of polling every tick |
| Scopes (agent, squad, global) | a squad can share "last seen player position" while each member keeps its own cover spot |
| Debug view | engines show the live blackboard of the selected agent; it's the first place to look when AI misbehaves |
A typed blackboard with observers
The keys are typed objects (Key<T>), so Get(Target) returns a string and Get(Alert) a double without casts. Observers fire only when a value really changes, which is how event-driven behavior trees avoid re-checking every condition every frame. A parent blackboard provides a squad-wide scope.
using System;
using System.Collections.Generic;
var Target = new Key<string>("Target");
var LastPos = new Key<(int x, int y)>("LastKnownPos");
var Alert = new Key<double>("Alertness");
var squad = new Blackboard("squad");
var guardA = new Blackboard("guardA", parent: squad);
var guardB = new Blackboard("guardB", parent: squad);
// A decorator "Alertness >= 0.5" observes the key instead of polling it.
guardA.Observe(Alert, (oldV, newV) =>
{
bool before = oldV >= 0.5, after = newV >= 0.5;
if (before != after) Console.WriteLine(quot; [guardA] condition 'alert' became {after}: re-evaluate the tree");
});
Console.WriteLine(quot;guardA has target? {guardA.IsSet(Target)}");
guardA.Set(Alert, 0.2);
guardA.Set(Alert, 0.2); // same value: no notification
guardA.Set(Alert, 0.6);
guardA.Set(Target, "Player");
Console.WriteLine(quot;guardA target = {guardA.Get(Target)}, alertness = {guardA.Get(Alert):F1}");
// Shared knowledge: guard A reports a sighting to the squad scope.
squad.Set(LastPos, (12, 4));
Console.WriteLine(quot;guardB reads LastKnownPos from {guardB.WhereIs(LastPos)}: {guardB.Get(LastPos)}");
guardB.Set(LastPos, (13, 4)); // guard B's own, more recent sighting shadows the squad value
Console.WriteLine(quot;guardB now reads it from {guardB.WhereIs(LastPos)}: {guardB.Get(LastPos)}");
Console.WriteLine(quot;guardA still reads it from {guardA.WhereIs(LastPos)}: {guardA.Get(LastPos)}");
guardA.Clear(Target);
Console.WriteLine(quot;after clearing: guardA has target? {guardA.IsSet(Target)}");
sealed class Key<T>
{
public readonly string Name;
public Key(string name) => Name = name;
}
class Blackboard
{
readonly string name; readonly Blackboard? parent;
readonly Dictionary<object, object?> values = new();
readonly Dictionary<object, List<Action<object?, object?>>> observers = new();
public Blackboard(string name, Blackboard? parent = null) { this.name = name; this.parent = parent; }
public bool IsSet<T>(Key<T> k) => values.ContainsKey(k) || (parent?.IsSet(k) ?? false);
public string WhereIs<T>(Key<T> k) => values.ContainsKey(k) ? name : parent?.WhereIs(k) ?? "nowhere";
public T Get<T>(Key<T> k)
{
if (values.TryGetValue(k, out var v)) return (T)v!;
if (parent != null) return parent.Get(k);
throw new KeyNotFoundException(quot;{k.Name} is not set");
}
public void Set<T>(Key<T> k, T value)
{
object? old = values.TryGetValue(k, out var v) ? v : default(T);
if (values.ContainsKey(k) && Equals(old, value)) return;
values[k] = value;
if (observers.TryGetValue(k, out var list))
foreach (var cb in list) cb(old, value);
}
public void Clear<T>(Key<T> k) => values.Remove(k);
public void Observe<T>(Key<T> k, Action<T, T> onChange)
{
if (!observers.TryGetValue(k, out var list)) observers[k] = list = new();
list.Add((o, n) => onChange((T)o!, (T)n!));
}
}guardA has target? False
[guardA] condition 'alert' became True: re-evaluate the tree
guardA target = Player, alertness = 0.6
guardB reads LastKnownPos from squad: (12, 4)
guardB now reads it from guardB: (13, 4)
guardA still reads it from squad: (12, 4)
after clearing: guardA has target? False
The observer printed exactly once: setting 0.2 twice didn't cross the 0.5 threshold (and the second write didn't even notify), while 0.6 did. Guard B saw the squad's sighting without anyone sending it a message, then shadowed it with its own fresher value.
Rules of thumb
- The blackboard holds the agent's beliefs, not the world's truth. "LastKnownPos" is where the agent thinks the player is. Perception writes it; the decision logic reads it. This keeps the sense, think, act separation.
- Few writers per key. If five nodes write "Target", you'll never know which one set it. Usually perception or a single "select target" service owns it.
- Don't store per-node state there. A node's own progress (the shot counter of a
Repeat) belongs inside the node instance. The blackboard is for data that several nodes share. - Names are an API. Designers bind keys by name in the editor, so renaming a key breaks trees. Keep names stable and descriptive.
- Clear what goes stale. A target that died or a cover point that was destroyed must be cleared, or the tree will chase ghosts.
Blackboards in engines
| Engine | Blackboard |
|---|---|
| Unreal Engine 5 | a Blackboard asset with typed keys (Bool, Int, Float, Vector, Object, Enum…), used by a Behavior Tree; decorators observe keys and can abort; keys can be marked Instance Synced to share one value across all agents using that blackboard. See Unreal: AIController, Behavior Trees & Blackboard. |
| Unity Behavior package | each behavior graph has a Blackboard of typed variables; graphs can also reference shared Blackboard assets. See Unity: AI Navigation & Behavior. |
| Godot 4 | no built-in BT; community behavior tree addons provide a blackboard (often a Dictionary wrapper), or use exported variables on the agent node |
Tip: Build a debug overlay that prints the blackboard above the selected agent's head. Most "the AI is stupid" bugs turn out to be a key that was never set, never cleared, or set by the wrong node; see Debugging AI.
Test yourself
What is the main job of a blackboard in a behavior tree?
- Drawing debug lines
- Sharing data (target, positions, flags) between nodes by key
- Storing the tree structure
- Scheduling ticks
Why do blackboards notify observers when a key changes?
- To save memory
- So decorators can react (and abort branches) immediately instead of polling every tick
- Because dictionaries require it
- To log changes to disk
In the example, why does guardA still read LastKnownPos (12, 4) after guardB set (13, 4)?
- Because of a bug
- guardB wrote to its own scope, which shadows the squad value only for guardB
- Because guardA's observer blocked it
- Because tuples can't change