Pooled BehaviorTree reference issues

almostchris

New member
Hey there, so I've got a BehaviotTree pool set up in order to prevent deserialization at runtime, but I'm having issues reassigning a previously used BT.

The pooled BT's retain component references of the first character they've been assigned to. I've tried restarting the tree as well as resetting values but none of it seems to force the BT call it's Tasks' OnAwake methods the second time around.

Specifically, I'm having issues with the Movement Pack's NavMeshMovement script, which retains the reference to the first NavMeshAgent. Any ideas what I'm doing wrong here or how to reset the BT to a fresh state?
 

Justin

Administrator
Staff member
What does your pooling code look like? When you call EnableBehavior on the behavior tree component that has the external tree it will call Awake on all of the tasks, which will cache the component references.
 

almostchris

New member
Basically, I've got two pools one for characters and the other for external BT's. I use the following code when my character gets spawned to pull the BT from the pool:

C#:
public void OnSpawned()
        {

            behaviorTree.DisableBehavior();
            behaviorTree.ExternalBehavior = AIPool.Instance.GetBehavior(externalBehavior);
            BehaviorManager.instance.RestartBehavior(behaviorTree);
            behaviorTree.EnableBehavior();
        }
When my character gets despawned I release the BT back to the pool:
C#:
public void OnDespawned()
        {
            
            behaviorTree.DisableBehavior();
            AIPool.Instance.ReturnBehavior(behaviorTree.ExternalBehavior);
            behaviorTree.ExternalBehavior = null;
        }
The AIPool class just instantiates and initializes a predefined number of external BTs and adds them to a list, nothing fancy.
The first time the BT is assigned to a character everything works as expected, however the second time the same external BT gets assigned to a different character, it still references the old NavMeshAgent of a character that's been despawned and starts throwing the usual errors when NavMeshMovement.SetDestination() gets called.
 

Justin

Administrator
Staff member
You don't need to call restart within OnSpawned but that shouldn't affect things. Can you set a breakpoint within NavMeshMovement.Awake to see if it is being called?
 

almostchris

New member
Hey Justin, so after adding the breakpoint I noticed that the OnAwake method was being called on EnableBehavior() as well as on DisableBehavior().
Is that the intended behavior?
Anyway, it seems like when the behavior tree was being despawned and respawned instantly, it would retain references to the old gameObject. I ended up rewriting my pool class to use Stacks instead of Lists and everything seems to be ok now. Thanks!
 
Top