From what I understand on Behavior Trees, each Behavior should be a short goal oriented Action that could be done in a few iterations.

So for example, below is an image of a Behavior Tree:

enter image description here

Now let us assume that the Drive To Enemy behavior takes more than a few iterations in the tree. So on each pass Drive To Enemy is called because it is now in the running state.

The problem is I want to call Evade Enemy if an Enemy is nearby. And considering that Drive To Enemy is always called I never get a chance to call Evade Enemy (Should probably be called Avoid Enemy).

  • Should I traverse the Tree EACH pass no matter what Action is currently running?
  • Am I going about this the right way?
  • What is the proper way of handling such a behavior?

Originally asked on Stackoverflow. Thought here would be a more appropriate place to ask this question.

  • 1
    \$\begingroup\$ Does the answer provided here help? gamedev.stackexchange.com/questions/51693/… \$\endgroup\$
    – Tetrad
    Mar 25, 2013 at 21:36
  • \$\begingroup\$ That's also my question. I was going to post this question as a comment but thought it should be a separate question. \$\endgroup\$ Mar 25, 2013 at 23:25

1 Answer 1


See the image I provided in my previous answer:

enter image description here

If you imagine that node 1 is 'Evade Enemy' and node 2 is 'Chase Enemy', you'll see that even though in the second iteration (when everything is green except for '2' and 'B' is when the second iteration starts), 'Evade Enemy' still gets checked first. Only when 'Evade Enemy' fails, because there are no enemies near by, is 'Chase Enemy' activated again. When 'Chase Enemy' is visited again, it notices that it's in the 'running' state and skips directly to 'B'.

This means that every time the tree is checked, it will always traverse left to right. Even when a node is marked as running, the higher priority nodes are still checked first.

I'm not sure if you're meaning to process your nodes from right to left, but that's how you appear to have them arranged (i.e. under evade enemy, locate enemy is on the right of drive in opposite direction). If you need further explanation, you should ask in chat or in one of your existing questions on the topic.

  • 2
    \$\begingroup\$ Thanks for another great explanation. I was having a hard time understanding the recursive nature of Behavior Trees. I was going to ask you in the comment of the last question but didn't want to turn the comments into a long QA. One last question though, this is all making sense now. Is there a difference between Selectors and Sequences in handling Running states? It seems that the Selector (Node 0) checked Node 1 first, while the "Node 2" sequence didn't check Node A, on the second iteration. \$\endgroup\$ Mar 25, 2013 at 23:34
  • 4
    \$\begingroup\$ Good question, I think you're getting it. Node 2 didn't check node A because it node A was completed. When node 2 is marked as 'Running', it notes that node B is the node that's currently running. You can assume that if a node is running, it means the previous nodes do not need to be checked again. \$\endgroup\$
    – House
    Mar 25, 2013 at 23:43
  • \$\begingroup\$ Are you resetting the Root Selector (0), to "READY" after it was set to "RUNNING" after the first iteration? \$\endgroup\$ Apr 1, 2013 at 20:28
  • \$\begingroup\$ I believe that only the parent of the running node is set to running. The root needs to be set to ready instead of running because we need to parse node (1) again. \$\endgroup\$
    – House
    Apr 1, 2013 at 20:59
  • 3
    \$\begingroup\$ That's one option. You can actually update it as frequently or infrequently as you like. Every 300 ms if you wanted to, or every frame. Or you can have a fixed update as well as a triggered update to deal with any events. Behavior trees, as with most structures like this, are not strictly defined. They should be used in the best way possible for your game. If evaluating it every frame is too wasteful, then don't do that. You can also limit the amount of time spent evaluating a tree, and pick up on the next frame if you wanted to. There's lots of options. \$\endgroup\$
    – House
    Jul 25, 2013 at 1:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .