Creating your own behaviour tree


For the next tutorial we will guide you on how to make your own Behaviour Tree Node so you can use it to make your custom Artificial Intelligence.

We will be using the PlacePiece.cs file for reference so you can understand how it is made:

Extending from BTNode class: The first thing to do when creating a Behaviour Tree Node (from now on referred as BT Node) is to extend your class from the BTNode class.

This class will give you access to all the main functionalities that a BT Node should have and set also some defaults properties like:

  1. It max children capacity is 0, to change this you should have to override the MaxChildren property.
  2. Graphic appearance on the Behaviour Tree.
  3. Variable of the parent BT Node.
  4. Variable of the left and right sibling.
  5. Index of the node as a child.
  6. Methods to handle the BT Node on the Behaviour Tree Window.

Override the main methods: The main methods that control the logic and behaviour of a BT Node is controlled by the following virtual methods:

  1. OnEnter: Special behaviour for when the Behaviour Tree enter this BT Node.
  2. OnOpen: Called when the BT Node is not opened.
  3. DoLogic: Called to execute the logic of the node, if you want this node to do something you must implement at least this method.
  4. OnClosed: Called when closing the node and the result of the DoLogic method is not Running (we will go in more detail for this later)
  5. OnExit Called just before returning the result of the node.

This methods are called on the order presented here. All of them, for the exception of DoLogic, have some basic behaviours so is recommended to call the base method on the overrides if used.

Normally you would only want to override the DoLogic to implement behaviours on a new BT Node. In fact, as you can see on the PlacePiece class all it does is create some variables and override the DoLogic method to make its logic.

Implementing the DoLogic method: The DoLogic method has three parameters, the blackboard that has the information of the behaviour three over the board, the agent player owning the Behaviour Tree and the game manager of the game.

You can use the blackboard to pass information between nodes or to read information from the previous node, so with the manager you can access the state of the actual game and create, move, delete pieces as needed. Of course if these move are valid or not will be validated on the move and capture rules set on the game.

Understanding the BTNodeState: The DoLogic method must return a BTNodeState that has the following options:

  • Success: You should return this state when the logic of your BT Node has ended in a successful way. This will make the parent know that this child made his labor in a good manner and can continue with the following nodes.
  • Failure: You should return this state when the logic of your BT Node has failed on it execution by normal and logical reason, like: it can make a move do to rule constrains, does not have pieces to move, it can’t move for special reasons , etc. This Failure state doesn’t mean the code has find a technical error or a programmatical mistake.
  • Running: You should return this under the same circumstances as a Success state, the difference is that the Behaviour Tree will know that on the next iteration of the Tree it should resume it’s logic from this node instead of move through it from the beginning.
  • Error: You must return this state whatever the code find technical errors o programmatical mistakes so it does not freeze your game. This will make know the Behaviour Tree that this particular node does not work.

As you can see on the PlacePiece BT Node code the Success state is returned if committing the movement of a piece is possible, otherwise it return Failure.

Making visible the node on the Behaviour Tree Window: To make visible the script of a BT Node on the Behaviour Tree Window you need do a very simple step: Add the GraphEditor attribute to the class.

This attribute has a string parameter that determine the name of this BT Node on the Behaviour Tree Window, so you can give it a more appropriate name, different from the class name, if you want.

When all of this is done you will be able to see the BT Node on the Behaviour Tree Window and you will be able to add it, remove it and link it with other nodes.

We invite you to look at the code of other BT Node to see how logic has been managed for each cases, in particular we invite you to check all the Specific uses BT Node that we listed on the previous tutorial.

Scroll to Top