Setting External Behavior Tree at Runtime and Task problem

Nenad

New member
Hi,

I have problem with setting External Behavior Tree at Runtime. I am using pool for my Enemies prefabs. Also I use pool for External trees. The problem I have is that my Tasks / Actions reference to old game object from pool.

This is how I set up External tree:

C#:
        BehaviorTree.DisableBehavior(true);

        BehaviorTree.ExternalBehavior = PreloadedBehaviourTrees[behaviourType].GetExternalBehavior(actor);

        BehaviorTree.EnableBehavior();


The problem is that in my action:

C#:
[TaskCategory("Actions - Enemies")]

[TaskDescription("Text")]

public class EnemyAi_Shoot : Action

{

    EnemyAi_Container _enemy;


    [BehaviorDesigner.Runtime.Tasks.Tooltip("Instant Fire")]

    public SharedBool Instant= false;


    public override void OnAwake()

    {

        _enemy = gameObject.GetComponent<EnemyAi_Container>(); // gameObject is reference of old enemy gameObject after respawn from pool

    }

}


Is there any way to force change gameObject in tasks of entire tree?
 
When the tree is enabled again it will point to the current GameObject. Are you sure that you are looking at the correct agent? I just did a test and the GameObject was updated.
 
yes, I am pretty sure that I debugged correct agent. I am manually ticking tree. I had only one agent on scene that is turned on. But when I debug code it reference the previous agent that is turned off. Also I am sure that enabled is called.

So when we call BehaviorTree.EnableBehavior(); it gets new copy of gameObject?

I am kind stuck with this.
 
So when we call BehaviorTree.EnableBehavior(); it gets new copy of gameObject?
Yes - you could import the runtime source to step through it:


When EnableBehavior is called it will assign the GameObject property within BeahviorManager.AddToTaskList. This is before OnAwake is called.
 
Top