Jump to content

  • Log In with Google      Sign In   
  • Create Account


Using one instance of Behaviour Tree for many agents?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 bonta-kun   Members   -  Reputation: 104

Like
0Likes
Like

Posted 04 December 2013 - 06:49 AM

Hi, after reading almost everything related to Behaviour Trees on aigamedev site I started to think how to implement it.

It looks like each agent in the game can have its own BT which it updates every frame. But what if many agents share the same behaviour (so they have their own copy of the same tree)? Isn't it a good option to have only one instance of the tree and each agent passes its pointer to the tree during update? This way decision/behaviour logic will be the same, but it will also depend on current owner state because each node will have access to "owner->blackboard->some_data" etc.

The same way I could use small behaviour subtrees (e.g. "attack_with_machine_gun") in different trees. So there would be one instance of "attack_with_machine_gun" but each main tree could use it during update by passing its current owner pointer.

Or this idea is completly wrong...?



Sponsor:

#2 apatriarca   Crossbones+   -  Reputation: 1476

Like
1Likes
Like

Posted 04 December 2013 - 07:07 AM

I think there was a video tutorial on AiGameDev.com discussing these topics. Probably this video.

#3 Juliean   GDNet+   -  Reputation: 1913

Like
1Likes
Like

Posted 04 December 2013 - 07:32 AM


It looks like each agent in the game can have its own BT which it updates every frame. But what if many agents share the same behaviour (so they have their own copy of the same tree)? Isn't it a good option to have only one instance of the tree and each agent passes its pointer to the tree during update? This way decision/behaviour logic will be the same, but it will also depend on current owner state because each node will have access to "owner->blackboard->some_data" etc.

 

Just to add from a general code point of view, this is called Flyweight-pattern, and makes most sense when there is a huge memory overhead, and sharing the instances helps to reduce this. I don't have much experience with AI, but generally this seems applyable.



#4 avision   Members   -  Reputation: 153

Like
1Likes
Like

Posted 04 December 2013 - 07:35 AM

On aigamedev you will also find the behaviour tree starter kit. It is fine to learn how they actually work. But basically each entity/agent has it`s own copy of a tree. Basically you want to be able to copy each node in a tree with childs. Then you can always use a subtree in different places. So you can make different trees who share same subtrees. You could then at runtime change a subtree for one agent but not for the others who use the same big root-tree.

 

Having one instance of a tree working with multiple entities is a bad idea IMO. A BT represents the ai state of one entity. How would you store the state of all the enitites currently using a tree inside the tree? One would be executing a seletor "find cover" while another would be "shoot with machine gun", every node would have to store the state for each entity. The next problem would be that you could not change a subtree for one agent only. Imagine a agent gets a new kind of attack, you would inset a special subtree in an attack subtree, but you can`t cause the other entities don`t have this kind of ability.

 

Think of the BT as the brain of an entity. You don`t want to share a brain :)



#5 bonta-kun   Members   -  Reputation: 104

Like
0Likes
Like

Posted 04 December 2013 - 08:07 AM

Thank you all for the replies.

 

Yes, I saw the "2nd gen BTs" video but this was the first thing I did and later after reading many articles my mind couldn't digest anything more.

 

@avision: This was exactly the thing I forgot - the per-agent state of the tree. I understood it at the beginning of studying BTs but later I forgot it.

 

So, thanks for your help

BTSK is available here: http://github.com/aigamedev/btsk

I also asked this question here: http://forums.aigamedev.com/showthread.php?6166-Using-one-instance-of-Behaviour-Tree-for-many-agents


Edited by bonta-kun, 04 December 2013 - 08:12 AM.


#6 Waterlimon   Crossbones+   -  Reputation: 2108

Like
0Likes
Like

Posted 04 December 2013 - 09:50 AM

I guess you could make the tree contain multiple states if it gives performance benefits.
The lack of awesome free resource gathering building sandbox games capable of running an user made 8 bit computer in the world disturbs me.

#7 avision   Members   -  Reputation: 153

Like
0Likes
Like

Posted 04 December 2013 - 11:37 AM

I guess you could make the tree contain multiple states if it gives performance benefits.

 

You probably could but i would not recommend it.
If you would have many many entities which always use the same never changing tree it could be an option.

 

Most likely you will have entites with different and most important of all _changing_ trees, like replacable subtrees.. Then this approach won`t work.
But the more i think of it the more problems i see. You probably want parallel/concurrency nodes or the like, this will be a pain to implement this way.
 

And most likely your BT won`t cause any performance problems if you implement them like shown on aigamedev. Atleast not compared to things like LOS-Test or the like. I would just start working with them as they are proven to work fine and wait if you really find a reason to change the paradigm.

 

I can also recommend "Artificial Intelligence for games", it has good chapters about BTs, Goal driven AI and other solutions.



#8 snowmanZOMG   Members   -  Reputation: 729

Like
0Likes
Like

Posted 05 December 2013 - 02:31 AM

It's possible to take certain portions of the behavior tree and make it shared amongst all agents to help reduce memory overhead and potentially improve performance.  But the cost of this I find is usually higher than it's worth.  The cost you usually incur is separating the core structure of the tree with the runtime state that's necessary to allow for proper initializations and restarts of the tree of a behavior that was previously running.  This, in turn, can make maintaining the code a bit more of a hassle.

 

In my own personal projects, I don't see that cost being worth it.  It may just be I've never found a particularly elegant way to make the core tree structure shared but the tree traversal state on a per agent basis, but I've also never needed to optimize that portion of my code.  I have relatively simple behaviors in my game but I still tick all the agents every frame and I may spend at most 0.5% total frame time in the behavior tree?

 

I really don't see the point of trying to do this aside from educational purposes.  Chances are you'll gain very little from attempting to make this optimization.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS