Blackboard
Overview
The Blackboard system is a flexible and powerful framework designed for Unity, enabling developers to create and manage game data dynamically. It leverages the concept of a "blackboard" as a central data repository where different components of a game can read and write data, facilitating communication and data sharing among disparate systems without direct references.
Key Concepts
BlackboardBehaviour
BlackboardBehaviour
is the base class for any blackboard. It allows the creation of custom blackboards by extending this class. Custom blackboards can contain any number of data fields, known as signals, which are automatically managed by the system.
Signals
Signals are the primary means of communication within the blackboard system. They represent data points on the blackboard, such as health, ammo, or player state. Signals are dynamically generated and managed, allowing for a flexible and scalable approach to data management.
Debugging
The DebugBlackboard
attribute can be applied to a BlackboardBehaviour
class to enable viewing the generated source for the blackboard in the Unity inspector. This is particularly useful for debugging and understanding the auto-generated aspects of your blackboard.
Creating a Simple Blackboard
Below is an example of how to create a simple blackboard with a single signal representing health.
A source generator will generate the following code:
The source generators are automatically run by the Unity compiler!
Interacting with the Blackboard
Components can interact with the blackboard by referencing the BlackboardBehaviour and using the generated signals. Here's an example of a component that modifies the health signal on a blackboard:
Viewing Generated Code
The source generators automatically run by Unity's compilation process add signals and other necessary code to the blackboard. To view the generated code:
In Rider: Ctrl + click on the classname.
In Visual Studio: Place your cursor on the classname and press F12. The declarations window should show, and you can select the generated partial. The file is named
<FileName>_<lite|pro>_generated.cs
.Other editors: Unity doesn't support any other editor officially and as such are incapable of viewing the generated code added by source generators. By adding the
[DebugBlackboard]
attribute to your blackboard the inspector will show two buttons which will allow you to Debug.Log the generated code or to copy it.
IAllAreSignals and ISomeAreSignals Interfaces
In the Blackboard framework, signals are a fundamental concept used to represent data that can change over time, triggering updates or actions when these changes occur. The IAllAreSignals and ISomeAreSignals interfaces play a crucial role in defining how signals are treated within classes that represent data in the Blackboard.
Using these interfaces allows you to add signals to nested classes in a blackboard, or to add signals to any other class!
IAllAreSignals Interface
The IAllAreSignals interface is used to mark a class as a container where all valid fields are considered signals. This means that every field in a class implementing this interface is automatically treated as a signal without the need for explicit marking. This interface is particularly useful when you want to ensure that all properties of an object are reactive and can trigger updates or actions upon changes.
Example Usage
In the example above, the Gun
class is marked with the IAllAreSignals
interface, indicating that all of its fields, such as _type
, are treated as signals within the Blackboard system.
ISomeAreSignals Interface
Contrary to IAllAreSignals
, the ISomeAreSignals
interface is used when only some fields within a class should be treated as signals. This interface requires explicit marking of fields that should be considered signals, offering more granular control over which properties are reactive.
Example Usage
In the Bullets class example, the ISomeAreSignals
interface is implemented, and only the inGun
field is explicitly marked as a signal using the [Signal]
attribute. This allows for specific fields to be reactive, while others, like clipSize
, remain as regular fields.
Last updated