# Implementing a hierarchical behaviour tree

## Recommended Posts

I've decided to implement a hierarchical behaviour tree for the AI in my game. But I've got some question about how to do specific things. Recently, I read an article about the AI of Halo2, and I stumbled across a section that explained impulses. Currently, my system works like this: I've got a Root node, that can travel the tree down- and upwards. This root node calls an Update() function of the current leaf/branch wich returns a state. This state tells the root node, weather it has finished or needs some more time executing. When it's finished, the node tells the root node where it needs to go next (maybe the node's parent or a child of the current node). The root node then does this in an infinite loop, until it stumples upon a "need more time to execute" state. This would be returned by a leaf, that's supposed to move the AI to a certain position. It will return this state every update call, until the AI is at the desired position, then it will return the finished state.
void BTRoot::Update()
{
BTBase::t_finish_state current_state = BTBase::WAIT_FOR_FINISH;

while( true )
{
if( !m_pCurrent )
return;

BTBase::t_finish_state last_state = current_state;
BTBase *pNext = m_pCurrent->Update( last_state, current_state );

if( pNext )
{
m_pCurrent = pNext;
} else { // NOT_FINISHED
// da nicht jeder knoten beim ersten Update seine Arbeit erledigen kann
// (MoveTo dauert eine Weile), signalisiert das jeder Knoten durch das setzen
// der Wait Variable
break;
}
}
}


This system (I haven't got the time to test it yet) should work fine for sequences and selections, timers etc... But when I read this article, I figured, that I also need some sort of interrupt routine, that will force the root node to abandon it's current work and continue at another node. This interrupt could be caused by an "enemy sighted" state. However, I've got some problems to figure out a system, that implements this behaviour. Can anybode give me some advice how I could integrate this into my current system?

##### Share on other sites
This one gets discussed a lot at AiGameDev.com, both on the blog and in the forums.

You basically need a "parallel" node somewhere in the tree that checks for these events. You can even build a custom "selector" (like mentioned in the Halo 2 article) which dynamically checks the better options in the tree every time it is executed.

Does that help a bit?

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628336
• Total Posts
2982158

• 9
• 24
• 9
• 9
• 13