Tutorial

Four Steps To Create and Run Your Behavior Trees

1. Design Behavior Trees and Forests In Bt Studio
2. Setup All Behavior Trees In Unity Editor
3. Customize Node Behaviors With Node Scripting API
4. Start To Execute Behavior Trees via Bt Engine Scripting API

1. Design Behavior Trees and Forests In Bt Studio

Design and create the behavior tree with the graphical user interface in the Main Editable Diagram Window in Bt Studio. Specify each node’s attributes in the Node Attribute Table Window when focused. Most of the attributes can be passed to Bt Engine in Unity Engine with a pair of strings. To see how a behavior tree works in the behavior tree engine system, check the behavior tree concept page.

create_trees

Save the behavior tree as an XML file. (For demonstration purpose, we use our demo behavior trees as examples.)

ATree00.xml
ATree10.xml
ATree11.xml
ATree20.xml
ATree21.xml
ATree22.xml

Design and create the behavior forest with the graphical user interface in the Main Editable Diagram Window in Bt Studio. Specify each node’s attributes in the Node Attribute Table Window when focused. Most of the attributes can be passed to Bt Engine in Unity with a pair of strings. To see how a behavior forest works in the behavior tree engine system, check the behavior forest concept page.

create_forest

Save the behavior forest as an XML file. (For demonstration purpose, we use our demo behavior forest as examples.)

AForest.xml

2. Setup All Behavior Trees In Unity Editor

Copy all the tree and forest XML files to the root directory of the Unity project. After copying all the files, the Unity project file structure should have these elements:

./AForest.xml
./ATree00.xml
./ATree10.xml
./ATree11.xml
./ATree20.xml
./ATree21.xml
./ATree22.xml
./Assets/BtEng/BtEngLib.dll
./Assets/BtEng/BtEngLib.XML
./Assets/BtEng/NewForestNode.cs
./Assets/BtEng/NewTreeRootNode.cs
./Assets/BtEng/NewDecoratorNode.cs
./Assets/BtEng/NewConditionNode.cs
./Assets/BtEng/NewActionNode.cs
./Assets/BtEng/BtEngineDemoGUI.cs
./Assets/Plugins/bteng.dll

Open Unity Editor and create a new project. Click Window->Bt Engine->Setup All Behavior Trees->1.Create All Node Game Objects and Node C# Scripts From Behavior Forest Xml.

1st_click

Select the node game object hierarchy level to create all essential node game objects. For demonstration purpose, we select the most detailed level, e.g., the “Node Level”, to create the full hierarchy of node game objects.

Select the node name option. Since our node names are already distinct when designed from Bt Studio, we don’t need to append redundant composite indices to make those names distinct in Unity. Select “No”.

Select the forest XML file.

select_AForest

Select a folder to save all new node scripts.

new_node_scripts_folder

It will take a couple seconds to generate and import all the required files.

After the task was done, the generated node game objects in Hierarchy tab and node C# scripts should look like this:

1st_click_res

Click Window->Bt Engine->Setup All Behavior Trees->2.Add All Node C# Script Components to Node Game Objects From Behavior Forest Xml.

2nd_click

Select the node game object hierarchy level to add node C# script components. Since we already created a full hierarchy of node game objects, we select the most detailed level, e.g., “Node Level”, to add each C# script component to the corresponding node game object that has identical name.

Select the node name option. Since we didn’t append composite indices to those names of node game objects and C# scripts, select “No” to not to append composite indices to node names to lookup and connect node game objects and C# script components.

Select the forest XML file.

select_AForest_2

After the task was done, each node game object in the hierarchy should have its corresponding node C# script attached.

2nd_click_res

3. Customize Node Behaviors With Node Scripting API

Open your IDE to see those generated node C# scripts. Each node operation was by default implemented with a debug logging function as well as a random node status generation function if applicable. To change their behaviors, in each node’s OnSomeOperation member functions, override their implementations by replacing the BtDebugger.PrintOnSomeOperation (and BtDebugger.GetRandomNodeStatus() if applicable) codes with your custom codes so that they can be invoked accordingly when running the behavior trees. To see when and which node operation gets invoked in the behavior tree engine system, check the behavior tree concept page.

open_ide

Those generated node scripts were copied from the NewForestNodeNewTreeRootNode, NewDecoratorNode, NewConditionNode and NewActionNode scripts with new node names. Each node script is printed an initialization function with its fixed composite indices as well as its fixed node name when creating from XML files which are used for node registration for Bt Engine to lookup and invoke.

4. Start To Execute Behavior Trees via Bt Engine Scripting API

Create a new UnityEngine.MonoBehavior C# script in Unity Editor. Invoke Bt Engine API like this demo:

Attach this script to an active game object in Unity Editor. Then run Unity. All the behavior trees from the forest xml file should get loaded and executed immediately. To see how the behavior tree engine works, check the behavior tree engine concept page.