Behaviour Tree Editor - Promethaes/Game-Programming-Compendium GitHub Wiki
Foreword
I've looked at a couple of different ideas of behaviour trees, and so, I'm trying my hand at making my own behaviour tree editor. This is going to be an interesting challenge for me because I've never made editor scripts to this degree before, let alone made any formal behaviour tree. Sure, my previous implementation shares the same concepts in general of having a tree structure that takes in input, evaluates it, and then executes actions based on that input. But there are problems with it:
- All actions attached to a decision are executed at the same time. There is no option to have a sequence of actions play in order.
- The use of Unity Events helps understand the flow of control, but a visualization would be a much greater representation.
- I found myself editing the Blackboard script directly for the behaviour I wanted. I realize now that I was essentially combining the idea of a Blackboard and a Root-Node into one script, which should be separate.
Behaviour Trees
A great video explaining what BT's are and how one might implement them. Another one
Behaviour Trees, or BT's, are similar to a finite state machine or FSM. What makes them special varies from one implementation to the next, but in general most contain:
- Nodes, which generally have failed, succeeded, and running states, and a virtual Evaluate function
- A Blackboard, which stores the state of variables that are to be known about across the entire tree (see Basics of AI
- Flow control Nodes similar to Deciders
- Leaf nodes similar to Actions that do the exact same thing but also have their own checks before they run their behaviour There are a few different types of flow control nodes:
- Composites, which are flow control nodes that have several leaf nodes as children and can serve many purposes. They can provide an additional state check before choosing a leaf node to navigate to and then having that leaf node perform its own check, essentially forming a logical AND which each child. Composites can also choose to run each leaf node in sequence, in parallel, randomly select one, etc. Also known as Sequencers or Selectors respectively.
- Decorators, which are flow control nodes that have only one child and may serve to change the output of the node's state or delay its execution, etc. I think its called a decorator because it can do a little bit of modification on the output as a sprinkle of interesting functionality...? yeah why not.
In general, flow control nodes can be very interesting and I will definitely be thinking of different ideas for decorators and composites.
Nodes can have priority, which dips into Utility-Based design, opening up even more natural looking results. This concept also opens up the possibility of interrupting functionality, or falling back to other functionality, for example if the character gets hit, they could navigate to the stagger state by interrupting their current state.
A technique used for optimization is to store the currently active node in the Blackboard and have that node process the update rather than going through each node to see if it should be triggered at a given point, and doing the same for all of its children. One problem I'm going to have to figure out is how to allow for interrupts with this optimization in place, I'm thinking I have certain criteria that qualify as a legal interrupt for a given leaf node, the state of said interrupt being some sort of flag in the Blackboard or something. Definitely something with event systems involved.
Common implementations of these systems include having a global Blackboard to store state that should be known by many AI at once, such as the player's position. I think a combination of a global and BT local Blackboard is the best option to encapsulate where needed yet save me some hassle.
Fuzzy Logic and Randomness
Something mentioned in much of AI is the idea of probabilistic outcomes. This is, in accordance with other practices, all in favour for adding more life to the AI. Some nodes of the behaviour tree may use animation curves in order to map their priority or weighted inputs to 0 or 1. It is important to note that a balance should be found between realistic behaviour and predictability, which can probably be achieved with a combination of fuzzy logic gates and sequencers.
Editor Windows
As I've never made any editor windows before, I've looked in to some resources: Brackey's Intro to Editor Windows
Apparently there's a UI Builder similar to Scene Builder from JavaFX and Windows Forms. I just went down a rabbit hole learning how to use it, and I'm unsure if its too much for my needs but I'm running with it.
I've managed to find a tutorial on exactly what I was looking for. I'm going to follow it and make changes where I see fit. This implementation uses UIBuilder as well, so its good to know that I was on the right track.