Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
1  
Does the answer provided here help? gamedev.stackexchange.com/questions/51693/… –  Tetrad Mar 25 '13 at 21:36
    
That's also my question. I was going to post this question as a comment but thought it should be a separate question. –  Free Lancer Mar 25 '13 at 23:25

1 Answer 1

up vote 8 down vote accepted

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.

share|improve this answer
    
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. –  Free Lancer Mar 25 '13 at 23:34
1  
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. –  Byte56 Mar 25 '13 at 23:43
    
Are you resetting the Root Selector (0), to "READY" after it was set to "RUNNING" after the first iteration? –  Free Lancer Apr 1 '13 at 20:28
    
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. –  Byte56 Apr 1 '13 at 20:59
2  
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. –  Byte56 Jul 25 '13 at 1:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.