Skip to content

Blackboard API

CoffeeVampir3 edited this page Apr 11, 2021 · 4 revisions
[CreateAssetMenu(menuName = "Graphify/Blackboard")]
public class DataBlackboard : ScriptableObject

Data Blackboards can be created as a regular scriptable object and applied to any Graph Blueprint. Blueprints have three blackboard scopes, you can set blackboard references in the inspector. A blackboard is not created for a graph by default.

internal DataBlackboard localBlackboard;
internal List<DataBlackboard> pooledBlackboards;
internal DataBlackboard globalBlackboard;

The local blackboard is copied for each instantiated virtual graph. That means each one can play with it's values and not have to worry about corrupting a different graphs data. No other scope is copied, so if you want to share data between graphs you would use either the global scope or utilize the blackboard pool.

You can just reference a DataBlackboard in code if you want to modify it's values outside the graph, modifying local blackboard values will not propogate to already existing copies. (So use a different scope if you want this behaviour)

You can reference the blackboards at any time in a runtime node using the Blackboards static class:

public static class Blackboards {
public static Dictionary<string, object> Local => ...
public static Dictionary<string, object> Global => ...
public static bool Query(string queryKey, out object item) 
public static bool Query<T>(string queryKey, out T item)

For example:

protected override RuntimeNode OnEvaluate(int graphId)
{
    if (!Blackboards.Local.TryGetValue(CommandBlueprint.targetId, out var target))
    {
        Debug.LogError("Attempted to run command + " + this.name + " but had no target!");
        return null;
    }
    rootOut.LocalValue = target as GameObject;
    return rootOut.FirstNode();
}

Query will query every blackboard in the pool and will return the first matching key.

You can initialize any graph's local blackboard in the Graph Evaluator initializer. This will configure that Virtual Graph's copy of the local blackboard.

public void Start()
{
    evaluator.Initialize(() =>
    {
        Blackboards.Local[CommandBlueprint.targetId] = target;
    });
}

Calling the static Blackboards outside of this initializer or a runtime node's OnEvaluate will produce undefined results.