Can't change variables at runtime of task in external behavior task of behavior tree reference.

kblack0610

New member
Hello, I have my behavior tree and tasks referenced in a controller script that I'm using to tie to persistent data I have saved (it loads during runtime when the enemy is instantiated, i.e distance to target, etc). When I access the behavior tree references I have in my behavior tree and try to change the variable, the variable values don't change for the task on the tree of the object.

What's weird is after I've made the change I log it and it shows the changed value, but looking at it from the behavior tree inspector nothing has changed. I've tried with both a shared variable and variable.

Does the variable need to be exposed in some way in order for the value to be changed? Or is it not possible to change these values at runtime within a behaviour tree. Was going to attach the logs of the value being changed and the unchanged behavior trees, but the image files are "too large". Any help would be appreciated! (not sure if this is why external pools or used, or if I need to expose the variable differently, or something else)

Here's how I'm changing the value:
Code:
            seekIsDistanceReachableTask.Distance = AIStats.minWalkToDistance; //variable
            seekIsDistanceReachableTask.testDistance= AIStats.minWalkToDistance; //shared variable
 
Last edited:
Ah interesting, I noticed it changed the var on the actual external behaviour tree in my assets, but not the one on the character. That's a lead though. I must have to reference the task that gets created within the tree from the external behavour (a new task gets created right) than the actual behavour tree reference? Going to try it! This was how I was previously doing it:
Code:
            behaviorTree = this.gameObject.GetComponent<BehaviorTree>();
          
            //Get references to external trees
            List<BehaviorTreeReference> btr = behaviorTree.FindTasks<BehaviorTreeReference>();

            //Add Tasks, todo probably want a find method here to get the specific behaviour trees
            seekIsDistanceReachableTask = btr[0].externalBehaviors[0].FindTask<IsDistanceToTargetReachable>();
            seekAimWeaponAtTargetTask = btr[0].externalBehaviors[0].FindTask<AimWeaponAtTarget>();

            destroyIsDistanceReachableTask = btr[1].externalBehaviors[0].FindTask<IsDistanceToTargetReachable>();
            destroyShootTask = btr[1].externalBehaviors[0].FindTask<ShootTarget>();
 
Last edited:
The main reason I did this is to access the variables entirely through the code. I'd prefer not to have to edit shared variables in the inspector and use the variable synchronizer, but if that's the only way then I guess I'll have to go that route.
 
Last edited:
Looks like I was able to get it to work by changing the variables after the subscribed event "OnBehaviorComplete", nice! If there's an easier way to do this, would appreciate any guidance. Thanks for your awesome asset and sorry about the long thread of talking to myself! haha
 
Top