gale.ai.blackboard module

This file contains the implementation of the class Blackboard, a shared key-value store to pass data between an agent’s behavior tree/decision tree nodes and any other system, the same role a Blackboard plays alongside Behavior Trees in engines such as Unreal Engine.

Author: Alejandro Mujica (aledrums@gmail.com)

class gale.ai.blackboard.Blackboard(values=None)[source]

Bases: object

A shared key-value store meant to be attached to an Agent (see gale.ai.agent) so its behavior tree/decision tree nodes, its steering behaviors, and any external system (a perception system, a quest system, another agent) can read and write data about it without having to wire up direct references to one another.

For instance, a perception system can post the last known position of a player under the “last_seen_player_position” key, a Condition node can check whether “is_alerted” is set, and a Task node can set “patrol_point” for a Seek steering behavior to read on its next update — none of them need to know about each other, only about the blackboard and the keys they agree on.

Usage example:

blackboard = Blackboard({“is_alerted”: False}) blackboard.set(“last_seen_player_position”, (120, 80)) blackboard.get(“is_alerted”) # False blackboard.get(“patrol_point”, default=(0, 0))

def on_alert_changed(key, old_value, new_value):

print(f”{key} changed from {old_value} to {new_value}”)

blackboard.observe(“is_alerted”, on_alert_changed) blackboard.set(“is_alerted”, True) # triggers on_alert_changed

Parameters:

values (Dict[str, Any] | None)

get(key, default=None)[source]
Parameters:
  • key (str) – The key to look up.

  • default (Any) – Value returned if key has not been set. The default value is None.

Returns:

The value stored under key, or default if it has not been set.

Return type:

Any

has(key)[source]
Parameters:

key (str) – The key to look for.

Returns:

Whether key currently has a value set.

Return type:

bool

set(key, value)[source]

Set key to value. If the stored value actually changes (or key did not have a value before), every observer registered for key via observe is notified.

Parameters:
  • key (str) – The key to set.

  • value (Any) – The new value.

Return type:

None

erase(key)[source]

Remove key and its value. Does nothing if key has no value set. Registered observers for key are kept.

Parameters:

key (str) – The key to remove.

Return type:

None

clear()[source]

Remove every key and its value. Registered observers are kept.

Return type:

None

observe(key, observer)[source]

Register observer to be called whenever key’s stored value changes through set().

Parameters:
  • key (str) – The key to watch.

  • observer (Callable[[str, Any, Any], None]) – Callable invoked as observer(key, old_value, new_value). old_value is None if key had no value set before.

Return type:

None

stop_observing(key, observer)[source]

Unregister a callable previously registered with observe.

Parameters:
  • key (str) – The key that was being watched.

  • observer (Callable[[str, Any, Any], None]) – The exact callable passed to observe.

Raises:

ValueError – If observer was not registered for key.

Return type:

None