🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Reactive Behavior Trees

Started by
4 comments, last by Ender1618 11 years, 9 months ago
So I am back to trying to understand how to use behavior trees for my AI decision making. So right now I am looking at BTs from the perspective of Alex's presentation on second generation behavior trees on AiGameDev. Although i am not going to implement a second generation tree just yet. From my understanding a second gen tree implementation is just an optimization of a first gen tree, where the node layout is identical just traversal is optimized.

So lets start simple with a first gen tree, from what I understand the tree is ticked from the root every think cycle. Sequences and selectors maintain a memory of the current child they left off on since the last tick, and when hitting that sequence or selector again we just continue with the evaluation of that child subtree until it succeeds or fails. After something like a sequence or selector has succeeded or failed the current child node indicator is reset to point at the first child again.

So a tree like this is problematic:
selector
--sequence
----Cond(Health low)
----SubTree(Flee)
--sequence
----Cond(Has target)
----SubTree(Attack)
--Subtree(Idle)
Here the conditions would be checked only once when entering the sequence, so we could not react to changes unless the subtree failed (e.g. attack). For example if we entered the first sequence, checked the condition (which at the time was true), the entered the Flee subtree, that condition would not be checked again until we either succeeded or failed that subtree (which would only have the condition checked once we re-entered the parent sequence later on).

Another way to approach this would be something like this?:
selector
--parallel
----[Not]Cond(Health low)
----[Not]Cond(Has Target)
----SubTree(Idle)
--parallel
----Cond(Health low)
----[Not]Cond(Has Target)
----SubTree(Flee)
--parallel
----[Not]Cond(Health low)
----Cond(Has Target)
----SubTree(Attack)

This seems horribly convoluted, and would get even more complicated for more subtrees i might need to bail out of in this subtree selection.

I heard something about ActiveSelectors being able to always take the highest priority subtree that continually checked conditions will allow, but I am a bit confused on how this wold be implemented (sequence and selectors seem pretty straight forward in their implementations).

Anyone have any insights?
Advertisement
Yes, ActiveSelectors are a nice solution to this particular example. You'll first need to figure out your "specification" for how that should happen, e.g. every frame check all nodes of higher priority than the currently executing one and if one can execute simply deactivate the current one.

Once you have that specification, the code is relatively straightforward. I've done this differently a bunch of times, but most recently managed to get it working by mostly reusing the passive selector logic. Just write unit tests and it'll work out!

Alex

Join us in Vienna for the nucl.ai Conference 2015, on July 20-22... Don't miss it!

Hmm but how is this implemented?

Does each subtree of the selector have associated with it a condition (or condition subtree) that is ticked every frame (where the actual subtrees that we want to choose from do not necessarily get ticked)? Then the left most (highest priority) subtree whose associated condition has proved true is then run (meaning its subtree is traversed). So its like a parallel that only runs conditions in parallel?

Thx
You could create a selector as you describe it, with a separate condition tree (read-only) and then a behavior tree (read-write). However, you'll find those are a pain to maintain in a similar way than your previous example.

The way I use active selectors, I basically want the exact same as a passive selector, but instead running those higher priority branches every frame and aborting the lower-priority ones if those succeed. If that's the specification you want, then the implementation of it should be straight forward. (If not, just ask! :-)

Alex

Join us in Vienna for the nucl.ai Conference 2015, on July 20-22... Don't miss it!

I am a bit confused as to your explanation of the ActiveSelector implementation.

In your description how does an active selector differ than a normal selector. A normal selector will check the highest priority child (left most subtree) first, and then move on to the next child (on the next tick) if the previous child returned failure. How does the active selector differ? Does the child pointer just not increment on the next tick?

What do you mean by aborting the lower priority subtrees? Does that mean just not traversing them?

Thx
Thought about it a bit more, by ActiveSelector do you mean a selector that always evaluates from highest priority (left most) to lowest when it is visited, instead of (like a normal selector) continuing with the last visited subtree during the next tick. You have to keep track of what subtree you where in before so that if you need to abandon that subtree on the next tick, you can reset all of that subtree's child iterators. Does that make sense?

This topic is closed to new replies.

Advertisement