Behaviour Trees are used to create Artificial Intelligence for any board game made with the “Abstract Strategy” plugin.
Create a Behaviour Tree file asset: Click on the Gamelogic menú and choose New Behaviour Tree.
Edit a created Behaviour Tree: Click on inspector and click Edit on the Asset Inspector:
Clicking Edit will open the following window:
Behaviour Trees work as tree where the node Behaviour Tree showed on the previous image is the root of the tree. As you can see there is a column at the left side with different options, these options represent nodes with different behaviours. Every node has a Script representation that you can check on the following folder: Gamelogic/Plugins/AbstractStrategy/AI/BehaviourTree/Nodes/
Also be careful when adding, removing or changing nodes on the BT window as all changes are immediately saved and there is no ctrl+z to back you up.
Structure of a Behaviour Tree: Let’s take a look at the Reversi AI Behaviour Tree found on the Gamelogic/Example folder, if you open it you will find the following tree:
This BT is read as follow: The Behaviour Tree node is the root node that call a child to execute his logic, in this case a Sequence Node is called. Sequence Nodes call are his childs from left to right executing all their logics, if any node fail the sequence is break and a Failure is send to the root node to let it know that the AI couldn’t calculate the next movement, but if all sequences work it will return success letting know that the AI did his moved.
The sequence node call two nodes, Find best position and place piece, as their name suggest the first one find the best position on the board and the second one place the piece on the position found.
The way these two nodes communicate is by a blackboard dictionary object on the code, so the “Find best position” node place the found position on the “pos” variable of the dictionary and so the “Place Piece” node use the same variable position to reach it.
As we said the Sequence node call nodes from left to right, so if you want to change the order of nodes on a sequence you can use the arrows that are at the right of the Remove button.
Also, as you can see, there are some nodes that seems way too specific and other are are of general use, here is a guide so you can know how to start your own BT:
General use nodes:
- Selector: Execute every child till one returns Success, Running or Error. If none then it return Failure. In other words, it returns the value of the first child that complete his logic.
- Sequence: Execute every child unless one of them return failure.
- Memory Selector; Same as Selector, but if one child return Running then at the next AI turn the BT will resume its execution on the node that returned Running.
- Memory Sequence: Same logic as Memory Selector but applying the Sequence logic.
- Empty Cell Condition: Test if positions on the board are empty or not saving the result on the blackboard specify position.
- Test Cell Condition: Test positions on the board saving the result on the blackboard specify position.
Inverter: Invert the result of his child.Specific uses: - Find best position: Find best position on the board for a Reversi Game.
- Place piece: Place a piece on the board for games that add pieces.
- Place piece randomly: Place a piece on the board for games that add pieces.
- Can Make Line Condition: Check if it is possible to make a line on the next move, used to games with this logic in mind.Also there are three options that are not nodes:
- Clear: delete every node. Be careful since there is no restore option.
- Reassign Parents: Reassign parents when links has changed.
- DoLayout: If nodes are moved and in no clear order you can click this and the full Tree will be order.You may be thinking that these are not enough, we will be adding more nodes to the BT window, but you can also add your own, this, however, will be cover in other tutorial so for now we invite you to try out this options and create simple AI so you can fully understand how Behaviour Tree operates.