Blackboards
A Blackboard is a data structure which allows you to share data between different components of your AI without having direct dependencies between them.
A blackboard is a Dictionary
like data structure which allows you to add data by defining it as a set of key value pairs.
Let's say you want to know what is the safest location to go to, you can define a safe location key in the blackboard and store the Vector3
of the location in it.
Our implementation of blackboards is more strongly typed than a Dictionary<string,object>
and you should define all keys with the data type of their values before being able to set/get them.
Getting Started
To define a blackboard you first need to create a blackboard definition.
To do that right click in your project and click on Create>NoOpArmy>Wise Feline>BlackBoard Definition.
Then you need to assign that definition to a BlackBoard
component.
Then you can set or get the value of the keys you defined in the definition.
You have multiple methods in the BlackBoard
component like GetFloat(key)
, SetFloat()
, GetVector3(key)
and SetVector3(key,value)
.
You have these pairs for all data types and you can set and get the values in your code as you need.
How can I use this?
Your AI can use this in considerations and actions and other parts of your game. Let's say you have an action like, GoToLocation, you can take a blackboard key which you know contains a Vector3
.
A small tutorial
- Create a definition and call it b.
- Add a float key called speed.
- Create a new script called
TestBlackBoard
and attach it with aBlackBoard
component to a GameObject. - Set the definition in the blackboard to the one you made above.
- Open the
TestBlackBoard
script you made and add some code like this.
public class TestBlackBoard : MonoBehaviour
{
private BlackBoard board;
void Start()
{
board = GetComponent<BlackBoard>();
board.SetFloat("speed", 3);
var speed = board.GetFloat("speed");
print(speed);
}
}
The code should print 3.