BehaviourTree
class in RenownedGames.AITree / Inherits from ScriptableObject
The BehaviourTree class is responsible for defining, managing, and controlling the behavior trees used in AI logic.
Public Methods
Clone()
Description: Clones the entire behavior tree, including all nodes and settings.
Returns:
BehaviourTree — A new cloned copy of the behavior tree.
Example:
BehaviourTree clonedTree = originalTree.Clone();GetBlackboard()
Description: Shared Blackboard reference associated with this behaviour tree reference
Returns:
Blackboard — The blackboard used by this behavior tree.
Always return shared reference of blackboard. If you need blackboard instance see BehaviourRunner
Example:
Blackboard blackboard = behaviourTree.GetBlackboard();GetUpdateMode()
Description: Retrieves the current update mode for the behavior tree.
Returns:
UpdateMode — The current update mode (e.g., Update, FixedUpdate, LateUpdate, Custom).
Example:
UpdateMode mode = behaviourTree.GetUpdateMode();SetUpdateMode(UpdateMode mode)
Description: Sets the update mode for the behavior tree.
Parameters:
UpdateMode mode — The desired update mode.
Example:
behaviourTree.SetUpdateMode(UpdateMode.Update);GetTickRate()
Description: Retrieves the tick rate, which defines how often the behavior tree updates.
Returns:
int — The number of ticks per second.
Example:
int tickRate = behaviourTree.GetTickRate();SetTickRate(int count)
Description: Sets the tick rate for how often the behavior tree should update.
Parameters:
int count — The number of ticks per second.
Example:
behaviourTree.SetTickRate(5);IsRunning()
Description: Checks whether the behavior tree is currently running.
Returns:
bool — true if the tree is running, false otherwise.
Example:
bool isRunning = behaviourTree.IsRunning();GetRootNode()
Description: Retrieves the root node of the behavior tree.
Returns:
Node — The root node of the behavior tree.
Example:
Node rootNode = behaviourTree.GetRootNode();GetNodes()
Description: Returns the list of all nodes in the behavior tree.
Returns:
List<Node> — The list of all nodes.
Example:
List<Node> nodes = behaviourTree.GetNodes();SetNodes(List<Node> value)
Description: Sets the list of nodes in the behavior tree.
Parameters:
List<Node> value — The list of nodes to assign.
Example:
behaviourTree.SetNodes(new List<Node> { node1, node2 });GetCurrentNode()
Description: Returns the currently executing node.
Returns:
Node — The node currently being executed.
Example:
Node currentNode = behaviourTree.GetCurrentNode();Static
TraversePath(Node node, Node searchingNode)
Description: Traverses the path from a specified node to a target node.
Parameters:
Node node — The starting node.
Node searchingNode — The target node to find.
Returns:
LinkedList<Node> — The path of nodes leading to the target.
Example:
LinkedList<Node> path = BehaviourTree.TraversePath(startNode, targetNode);Traverse(Node node, Action<Node> visiter)
Not recommended. This method is obsolete.
Use Node.Traverse() implementation.
Description:
Traverses all nodes from the target node with a visitor callback. Note: This method is obsolete; use Node.Traverse() instead.
Parameters:
Node node — The starting node.
Action<Node> visiter — The callback to apply to each node.
Example:
BehaviourTree.Traverse(rootNode, node => Debug.Log(node.Name));GetChildren(Node node)
Description: Returns all child nodes of a given node.
Parameters:
Node node — The parent node.
Returns:
List<Node> — The list of child nodes.
Example:
List<Node> children = BehaviourTree.GetChildren(parentNode);IterateChildren(Node node)
Description: Iterates through all child nodes of a given node.
Parameters:
Node node — The parent node.
Returns:
IEnumerable<Node> — An enumerable collection of child nodes.
Example:
foreach (var child in BehaviourTree.IterateChildren(parentNode))
{
Debug.Log(child.Name);
}IsSubNode(Node node, Node checkNode)
Description: Checks if a node is a sub-node of another node.
Parameters:
Node node — The parent node.
Node checkNode — The potential sub-node.
Returns:
bool — true if checkNode is a sub-node of node, false otherwise.
Example:
bool isSubNode = BehaviourTree.IsSubNode(parentNode, childNode);Create(string name)
Editor only!
If you call this method in build code block, wrap it in UNITY_EDITOR define.
Description: Creates a new behavior tree asset in the project.
Parameters:
string name — The name of the new behavior tree asset.
Example:
#if UNITY_EDITOR
BehaviourTree newTree = BehaviourTree.Create("New AI Tree");
#endifProtected Virtual Methods
Use the following methods only for overriding when inheritance, it is not recommended not to call these methods manually.
OnEnable()
Description: Called when the behavior tree is loaded.
Example:
protected override void OnEnable()
{
base.OnEnable();
Debug.Log("Behaviour Tree Loaded");
}Reset()
Description: Called when the user presses the Reset button in the Inspector, or when the component is first added.
Example:
protected override void Reset()
{
base.Reset();
Debug.Log("Behaviour Tree Reset");
}Last updated