Implementing Behaviour Tree Framework

I spent this week implementing the basic framework for my Behaviour Tree (BT) architecture, for this first iteration I only implemented the basic nodes that will be required within the tree, such as the Selector and Sequence Composite nodes.  I will be looking into including other nodes types as the project develops if I am able to find a use for them, such as decorator nodes or random sequence / selector nodes.

I also had to work towards creating a very basic blackboard system for the agent, this first iteration of the blackboard just contains a small selection of variables to help hold the information while the BT changes between the two leaf nodes currently implemented, however I will be building upon this blackboard and a global blackboard shared between all agents.  When I build upon the blackboard I will attempt to use a "Dictionary" as in a previous blackboard I had to create during my second year of studying I used "Map" in C++ and felt this worked in the most effective way.

I started by creating a script called BehaviourTree.cs this is where all of the BT nodes will be implemented in their basic form, this will be a "blueprint" of sorts for each node.  Within this file I create the composite nodes and an empty leaf node.  As mentioned earlier, if I require the other node types they will also be implemented here.

Now that the basic framework for the BT has been implemented I will need to create some behaviours that can be used by the artificial intelligence (AI) within the scene.  Since I had already created a Patrol.cs class to test the NavMesh within the scene I thought it would be good to start here and break the class down into different leaf nodes.  I decided to break the class down into a "Pick Location" and "Move Towards" behaviours.

To begin, I created a new scene that would show the agent moving around in a different manner, setting up patrol way points for three separate guards as indicated by the coloured diamonds. (For the purpose of this test I will be using just one AI agent).  

Once the agent was placed into the scene I then proceeded to create my MoveTowards.cs and PickLocation.cs scripts currently these are both very rudimentary scripts, where one picks a random location from an array, and the other takes that information and passes it into the NavMeshAgent which will find a path to navigate along.  Both of these scripts will take a reference to the blackboard and the NavMesh Agent.

The blackboard will store the values that need to be passed between each of the behaviours, in this instance the value that will be passed between is the destination.

Once I had finished implementing the framework for the BT and the basic patrol I then needed to test it out on one of the artificial intelligence (AI) agents within the scene, to do this I created another class called AI_Guard_BT.cs  This is the class that will be placed onto the AI Agent in the scene and will act as the "brain" of the agent.  Within this class the BT is initialised and then each frame the Run() function is called.  To initialise the BT when the class Awake() function is called it calls a method called InitializeBehaviourTree with a return type of Node.


This builds the tree from the bottom up (currently only the patrol behaviour is present) and then returns the root of the tree.  I had an issue when passing in the guardBlackboard and the agent, where they would be created within this script, but would not be assigned within the respective behaviours, eventually I found this to be a simple issue where I was creating the behaviour tree before assigning the variables to be passed in.  

As shown in the video below the AI agent will patrol around his set route randomly, next I will implement the seek behaviour when the AI can see the player.