Game Development Community

dev|Pro Game Development Curriculum

More Behavior Tree Goodness

by Guy Allard · 07/16/2012 (7:44 am) · 8 comments

Following on from my previous blog, I've been working on some extra goodness for my BadBehavior behavior tree kit.

Behind the scenes, I've changed the way that the trees are handled internally by objects that are using them. Previously, each object using a tree was given its own copy of the tree to evaluate. As each node of the tree is a separate SimObject, that was getting quite expensive in terms of memory. Now when a unit is given a tree to run, it creates lightweight tasks for each node of the tree to be executed. Much more memory efficient.

The next upgrade was to modify the way that the condition and command tasks work. Previously they were running arbitrary script commands, and even though these were all being evaluated engine side, I wasn't happy with their performance.

i1271.photobucket.com/albums/jj639/guyallard01/TorqueStuff/condition.png
The above image shows the new condition node. It looks similar to the old one, but instead of running arbitrary torque script, the conditionFunc field now specifies a function to be run that returns true or false. Internally, it is evaluated using Con::execute, and I've gained a performance increase of at least 4x.


i1271.photobucket.com/albums/jj639/guyallard01/TorqueStuff/command.png
This image shows the new command node. The important part here is the func field, which specifies the name of a function in the namespace of the object running the tree. The four args fields allow arguments to be passed to that function. So the example command node when it is executed calls %obj.moveAlongPath(PatrolPath). Again, this uses Con::execute internally, and is also at least 4x faster than the old version.

As the new condition and command nodes are slightly less flexible than the old system, I've also added a new ScriptTask node (not shown) which is able to execute arbitrary torque script. This can be used when a simple condition or single command is insufficient (you could do container searches etc with it if you wanted).


i1271.photobucket.com/albums/jj639/guyallard01/TorqueStuff/ticker.png
The next new feature is the ticker decorator. This node acts like a timer, and controls how often the node can be executed. In the example, the ticker limits the branch that checks for a player to only execute once per second. By sprinking these tickers throughout the tree you gain fine grained control over how often expensive tasks are run.


i1271.photobucket.com/albums/jj639/guyallard01/TorqueStuff/event.png
Finally, the behavior tree system now has event handling. This has allowed for two new decorator node types - WaitForEvent and LoopUntilEvent. In the example, I'm using a WaitForEvent decorator to pause the execution of the movement branch until the bot has received an onReachDestination event. Before, we actively checked each tick if the player had reached its destination, now we can just do nothing until the event is received.
The LoopUntilEvent decorator (not shown) works like a normal loop decorator, but it stops running when it receives the event it is listening for.


I have a couple of jobs that are going to take me away from this over the next week or two, but when I am able to get back to it, my next plan is to get the behavior tree editor integrated within the T3D editor framework so that it seamlessly fits in with the other tools, and can take advantage of the undo/redo facility and the persistance manager.

After that, I want to put together some more example trees to demonstrate how quickly you can create a variety of interesting AI with just a few mouse clicks.

#1
07/16/2012 (8:19 am)
This looks great! You're leaps and bounds ahead of what I managed to do with BTs. Can't wait to see your example trees!
#2
07/16/2012 (9:25 am)
Oh pretty... this looks like it will be a big success... keep us posted!
#3
07/16/2012 (1:22 pm)
Damn, Guy! This has the level of elaboration and potential complexity of any professional middleware tool! Excellent work, hope you be able to complete it sooner rather than later, keep it up!
#4
07/16/2012 (2:33 pm)
I would consider this a potential buy aswell. Seems like a great tool if you want to develop your own AI (my AI code is a still a mess) it feels like this tool would make creating AI very easier and very intuitive!
#5
07/16/2012 (5:08 pm)
@Guy,
Excellent work.

One question: Can trees be dynamically constructed due to game events? This could make for some really awesome AI such as in an RPG. You could use it for companions that learn to fight along side you, for enemies that learn to fight better against you, and for NPCs learning to adapt to needs and wants. Like learning to find food or get resources, avoid danger, look for trouble, etc.

I am guessing this is driven by scripting beneath this?
#6
07/17/2012 (12:43 pm)
Thanks guys.

@Frank, I'm not sure how you would go about constructing fully dynamic trees. It's almost certainly possible, as the tree definitions are just an assembly of SimObjects.

One thing that could definitely be done is to modify trees made from different subtrees. In the 3rd video that I posted in by previous blog, the subtree nodes that I use to link to other trees could be made so that they can be changed on the fly. So an object could start with a 'dumb follow' subtree as its follow behavior, then after an upgrade, that could be replaced with a 'follow and defend' subtree. Alternatively, an object could start with an 'enemy' behavior that at some point gets replaced with a 'friendly' behavior. That kind of thing is possible with only small changes to where things are atm.

Another thing that I haven't mentioned yet is that absolutely any SimObject can use the trees. This opens up their use as decision makers for lots of different scenarios.

As for scripting, the tree definitions are scripted, and the leaves execute script statements. The rest of the execution logic - the sequences, loops, selectors etc are entirely c++. As those nodes effectively replace a lot of the control statements that you would normally script, that makes them pretty good in terms of performance.
#7
01/09/2014 (12:07 am)
could you send me some example code
#8
01/19/2014 (8:14 am)
Guy,
Not for sure if you are still around but if so please contact me via email:
jstanleynwo@yahoo.com...
I would love to talk to you about your kit.

Thanks in advanced