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
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.
Save the behavior tree as an XML file. (For demonstration purpose, we use our demo behavior trees as examples.)
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.
Save the behavior forest as an XML file. (For demonstration purpose, we use our demo behavior forest as examples.)
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:
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.
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 a folder to save all new node scripts.
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:
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.
After the task was done, each node game object in the hierarchy should have its corresponding node C# script attached.
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.
Those generated node scripts were copied from the NewForestNode, NewTreeRootNode, 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.
Create a new UnityEngine.MonoBehavior C# script in Unity Editor. Invoke Bt Engine API like this demo:
public class NewBehaviourScript : MonoBehaviour
// Use this for initialization
BtEngine.SetForestFile("AForest.xml");// Sets the forest xml file before starting Bt Engine
BtEngine.Start();// Starts Bt Engine
BtEngine.LoadAll();// Loads all forests and trees from the forest xml file
BtEngine.PlayAll();// Asynchronously starts to execute all behavior trees in each forest
BtEngine.Shutdown();// Shuts down Bt Engine before quitting the Unity application
// Update is called once per frame
BtEngine.Update();// Updates Bt Engine in each frame
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.
Copyright © 2016-2020 Qualgame, LLC