BehaviourRunner

class in RenownedGames.AITree / Inherits from MonoBehaviour

The main component to run created behaviour tree assets. Use Behaviour Runner component to access associated behaviour tree and blackboard of related AI entity at the scene.

Public Methods

GetSharedBehaviourTree()

Description: Get shared reference of behaviour tree.

Returns: Shared reference of behaviour tree.

Example:

BehaviourRunner runner = ai.GetComponent<BehaviourRunner>();
BehaviourTree behaviourTree = runner.GetSharedBehaviourTree();

SetSharedBehaviourTree(BehaviourTree tree)

Description: Set new shared behaviour tree reference.

This action will stop the execution of the current behavior tree and terminate it. The work of the tree, its state and the values of the keys in the blackboard will be destroyed and overwritten by the new tree. This action is irreversible!

Parameters: Reference of behaviour tree.

Example:

BehaviourRunner runner = ai.GetComponent<BehaviourRunner>();
BehaviourTree behaviourTree = runner.SetSharedBehaviourTree(newTree);

GetBehaviourTree()

Description: Associated instance of behaviour tree.

Returns: Associated (local) instance of behaviour tree created by runner after loading.

Example:

BehaviourRunner runner = ai.GetComponent<BehaviourRunner>();
BehaviourTree behaviourTree = runner.GetBehaviourTree();
Node rootNode behaviourTree.GetRootNode();

GetSharedBlackboard()

Description: Get shared reference of blackboard

Returns: Shared reference of blackboard.

Example:

BehaviourRunner runner = ai.GetComponent<BehaviourRunner>();
Blackboard blackboard = runner.GetSharedBlackboard();

GetBlackboard()

Description: Associated instance of blackboard.

Returns: Associated (local) instance of blackboard created by runner after loading.

Example:

BehaviourRunner runner = ai.GetComponent<BehaviourRunner>();
Blackboard  blackboard = runner.GetBlackboard();
if(blackboard.TryGetKey<float>("speed", out FloatKey key))
{
    key.SetValue(10);
}

Protected Virtual Methods

Use the following methods only for overriding when inheritance, it is not recommended not to call these methods manually.

Awake()

Description: Called when an enabled script instance is being loaded.

Example:

protected override void Awake()
{
    base.Awake();
    Debug.Log("Runner fully loaded");
}

Start()

Description: Called on the frame when a script is enabled, just before any of the Update methods are called the first time.

Example:

protected override void Start()
{
    base.Start();
    Debug.Log("Runner started the tree");
}

Update()

Description: Called every frame, if the Behaviour is enabled.

Keep in mind it's Unity pipeline update call, not of selected behaviour tree update mode.

Example:

protected override void Update()
{
    base.Update();
    // Todo you code here...
}

FixedUpdate()

Description: Called every fixed frame-rate frame. 0.02 seconds (50 calls per second) is the default time between calls, if the Behaviour is enabled.

Keep in mind it's Unity pipeline fixed update call, not of selected behaviour tree update mode.

Example:

protected override void FixedUpdate()
{
    base.FixedUpdate();
    // Todo you code here...
}

LateUpdate()

Description: Called every frame, if the Behaviour is enabled after all Update calls.

Keep in mind it's Unity pipeline late update call, not of selected behaviour tree update mode.

Example:

protected override void LateUpdate()
{
    base.LateUpdate();
    // Todo you code here...
}

OnDestroy()

Description: Called when an enabled script instance is being destroyed.

At this point, the associated instance of the behavior tree and the blackboard is still present and can be safely invoked.

Example:

protected override void OnDestroy()
{
    base.OnDestroy();
    Debug.Log("Runner stopped the tree");
}

OnDrawGizmos()

Description: Implement if you want to draw gizmos that are also pickable and always drawn.

Example:

protected override void OnDrawGizmos()
{
    base.OnDrawGizmos();
    // Todo your code here...
}

OnDrawGizmosSelected()

Description: Implement to draw a gizmos if the object is selected.

Example:

protected override void OnDrawGizmosSelected()
{
    base.OnDrawGizmosSelected();
    // Todo your code here...
}

Events Callbacks

OnCloneTreeCompleted

Description: Called when behaviour runner complete cloning of shared beahviour tree.

This callback can be used for making some custom actions with tree before starting the tree of specified AI.

Example:

private void SomeMethod()
{
    // AI entity and runner component disabled (sleeping) at this point.
    BehaviourRunner runner = ai.GetComponent<BehaviourRunner>();
    runner.OnCloneTreeCompleted += OnCloneTreeCompleted;
}

private void OnCloneTreeCompleted(BehaviourTree tree)
{
    List<Node> nodes = tree.GetNodes();
    foreach(Node node in nodes)
    {
        // Just example...
    }
}

Last updated