Variables
One of the advantages of behavior trees are that they are very flexible in that all of the tasks are loosely coupled – meaning one task doesn’t depend on another task to operate. Being loosely coupled is an advantage but there are times when you also need tasks to share information with each other. For example, you may have one task that determines if a target is Within Sight. If the target is within sight you might have another task which seeks towards the target. In this case the two tasks need to communicate with each other so the Seek task actually moves towards the same object that Within Sight found. For this example we have two tasks: one that determined if the target is within sight and then the other task moves towards the target.
In order to share the variables our tasks need to declare a the SharedVariable of the specified type:
public SharedVariable<GameObject> m_Target;
With this defined we can now create a new variable within Behavior Designer. For this example we should create the variable within the “Graph” tab of the Shared Variables pane. See the Scope page for information on the other tabs.
After the variable is created it can be assigned to within the task. Select the task and click on the “.” to the right of the variable value.
Do the same assignment with the Seek task and now both tasks can use the same value. The Accessing Variables page has information on how to get and set the variable value both within and outside of a task. The Property Bindings feature then makes it extremely easy to use the same value within your own components.