Behaviour Trees in AI Development
Many games that include AI will use Behaviour Trees to design and develop them, the system was popularized by the Halo series though it has been used in other games such as the Far Cry series and Alien: Isolation, which used Behaviour Trees to design and program the AI enemies that the player encounters throughout the game. They are typically used over finite state machines and planning due to their simplicity for the designers and programmers. They are also easier to follow and debug than finite state machines and easy to build functions for as they can be added onto other composite nodes and even other agents. Finally, they are more optimised for AI development which gives their usage the favour in larger, triple-A projects.
Image from Ubisoft.
What are Behaviour Trees?
Behaviour Trees follow the traditional tree data structure commonly used in computer science. They always start with a root node, which has no children. It is where the execution of the tree starts. A behaviour tree typically includes composite nodes (also known as control flow nodes), decorator nodes and leaf nodes. Behaviour Tree in Unreal Engine,
Composite Nodes
Composite Nodes control when their child nodes are run. They can have parents and children. There are two types of composite nodes, each one controls what order their children are run:
· Sequence Nodes make sure each child is run in sequence. They typically have leaf offspring since they represent actions the AI will take.
· Selector Nodes will choose one of its leaves to be run; this is normally based on a condition within the game world or, in some cases, random chance. Because of this, their offspring are typically composite and decorator nodes.
Decorator Nodes
Decorator Nodes are like composite nodes in that they have parents and children, however, unlike the composite node, they can only branch off to one child node. They are normally used to change the output of a child node or re-run a child node.
Leaf Nodes
Leaf nodes are functions which are run by an agent, they have parent composite and decorator nodes, however, they can’t have child nodes. Leaf nodes run instructions based on a sequence or a selective condition depending on their parent. If an agent was being shot at by the player in a game, the agent would call functions which would be run in sequence depending on the situation, the agent would look for cover, run over to cover & hide. Each action would be split into different leaf nodes under a sequence node.
Image from Unreal Documentation.
Blackboards
Trees use, what is known as, a blackboard. The blackboard stores different variables and conditions which the agent uses, some examples could be the distance to the player or if they are under attack etc. Blackboards are used to make sure the agent doesn’t have to check for the same condition each frame, blackboards also allow multiple agents to refer to the same set of values, for example, a group of enemies in the same area.