Syncing variables across behavior trees


New member

I'm trying to get 2 identical agents to sometimes talk when they are in range.

This requires the synchronizing variables across behavior trees, which is made slightly more complicated by the fact that it is only a 10% chance an agent will decide to try and talk to another. If one of the agents in range is successful in attempting to talk, I would like them both to look at eachother. I'm wondering what the best way to do this is.

It needs to be able to scale up to a crowd simulation, so it does not seem like Global Variables is a good option.

This is what my attempt looks like:

The Within Distance Action, is identical to the one that comes with the Movement Pack, however I have added an additional check so that the agent doesn't find itself.

The Talk Action is based on the Wait action, although it tells the agents to look at the 'Returned Object' from the Within Distance function. It also sets variables on the 'Return Object' so that the target of the conversation will know who to look at when they are being talked to.

In the Task Action Setup function:

// This lets the target know that it is being spoken to
_partnerBehaviorTree = conversationPartner.Value.GetComponent<BehaviorTree>();
_partnerIsBeingSpokenTo = (SharedBool)_partnerBehaviorTree.GetVariable("IsBeingSpokenTo");
_partnerConversationPartner = (SharedGameObject)_partnerBehaviorTree.GetVariable("ConversationPartner");
_partnerIsBeingSpokenTo.Value = true;
_partnerConversationPartner.Value = gameObject;

The problem with this is, that if the other agent is wandering/waiting; the Selector isn't evaluated until the left side is finished. My instinct is to copy the Selector and the Compare Shared Bool, and place somewhere to the left where it will have priority.

So I have 2 questions:

1. (Simple) OK I can't figure this out. How would I move the Selector/Compare Shared Bool such that it could interrupt the wander/wait sequence?
2. (Generally) My sense is that there must be a much better way to do this, than to have 2x Selector/Compare Shared Bools across the tree. Does anyone have any ideas about how to do this more cleanly?

Sorry if these are stupid questions! Growing pains of trying to move my AI stuff over to BD.



Staff member
It's tough to answer without debugging and seeing it in action but in general to sync variables with other trees you can do one of the following:

1. Use global variables
2. Use a variable that is mapped to a property (variable mapping in the docs). This will allow you to define the variable within a C# script and access that same property across multiple trees.

You can interrupt branches using conditional aborts and it looks like you started to use them but the lower priority portion of that abort won't work because there are no branches to the right of the current branch.