# Using one instance of Behaviour Tree for many agents?

## Recommended Posts

bonta-kun    104

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...?

##### Share on other sites
apatriarca    2365
I think there was a video tutorial on AiGameDev.com discussing these topics. Probably this video.

##### Share on other sites
Juliean    7068

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.

##### Share on other sites
avision    208

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 its 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 cant cause the other entities dont have this kind of ability.

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

##### Share on other sites
bonta-kun    104

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.

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

Edited by bonta-kun

##### Share on other sites
Waterlimon    4398
I guess you could make the tree contain multiple states if it gives performance benefits.

##### Share on other sites
avision    208

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 wont 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 wont 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.

##### Share on other sites
snowmanZOMG    1205

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.