Sign in to follow this  
SiS-Shadowman

Implementing a hierarchical behaviour tree

Recommended Posts

SiS-Shadowman    359
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 this post


Link to post
Share on other sites
alexjc    457
This one gets discussed a lot at AiGameDev.com, both on the blog and in the forums.

A good place to start is this article and its comments.

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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this