Hello, I am trying to learn the behaviour tree pattern and I have some points where I’m stuck. Actually, I understand the general concept. First, I created a node as the root and then added a leaf to it. This leaf takes an IStrategy as a parameter, and I added my PatrolStrategy class as the IStrategy. This class selects a point and moves my game object there. What I want is for this leaf to stop if it detects an enemy and for another Strategy to take over. The first method that came to my mind was to have another behaviour tree within the PatrolStrategy and have one of its leaves be a condition node. If it returns success, it would execute the next leaf, which would be AttackStrategy.
I hope I was able to explain what I mean. is this method logical? Should it be done this way, or is there a more logical approach? So far, I have learned about condition, Selector, Sequence, and PrioritySelector nodes, and I still need to learn about the loop node. If anyone has a good resource recommendation, I would greatly appreciate it.
I found the The Behavior Tree Starter Kit useful when researching behavior trees (although the source code does have a few bugs in the parallel code).
I’d say there are two main types of behavior trees. You have data driven where you normally have a visual editor for the graph. And code driven where you need to recompile the changes but it’s a lot easier to create. There are also different generations of behavior trees. A lot of the tutorials I see online are about first generation trees which aren’t really used much in games. Unreal for example uses event driven behavior trees.
Building your own behavior tree can be quite complex. I’d recommend trying a third party solution first.
Also, probably the main purpose of a behavior tree is interrupting the current action for a higher priority action. As an example, the activeSelector
node below would keep checking the other states. So if I was patrolling but I see an enemy it would break out of patrol and start attacking.
.activeSelector()
.sequence()
.condition(hp <= 0)
.play(deathAnimation)
.sequence()
.condition(canSeeEnemy)
.moveTo(enemy)
.attack(enemy)
.sequence()
.pickPatrolPoint()
.moveTo(patrolPoint)
.build()