Behavior Tree Task Variable Initialization

How do you initialize variables in a Behavior Tree Task? I can’t seem to find an event to call that is suited for this. I have some variables that remain constant and seems wasteful to set them every time the task is executed.

You store variables in blackboard.
Then you make blackboard key (and name it so it is not confusing).

For eg:

  • integer var that is “cookie counter” this in blackboard
  • blackboard key that is “cookie counter key” this you make in every blueprint that needs to use “cookie counter”

You make “cookie counter key” visible (editable) to other blueprints. Then it will show up in properties of your task blueprin. There you connect that “cookie counter key” variable with “cookie counter” variable in blackboard. It is how blueprints inside behavioral tree know whic h variables need to be used.

Also remembver that those behavior tree variables clear after every tick of behavior tree and also between instances, so everything you want to survive outside or between ticks you should store in blackboard.

If I understand right you can’t store any persistent variables in a Task, so they have to be in the Blackboard?
In that case I’ll follow your advice, thanks.

But won’t the blackboard fill up with a lot of clutter if all task variables has to be stored there?

Yup you cannot store data inside any of task services etc blueprints in behavior tree. Everything you want to survive must be stored in blackboard.
It is matter of optimization, those blueprints are reused every tick (ai tick) for every ai pawn you have out there. So only blackboard data survives.

And that is the tricky part to design blackboard to be not cluttered. Also no data structures or arrays there only simple data.

But you can make service that for eg. loads all data from player controller. At least data that should be common for all AI instances.

Hold on.

I added an integer count variable to the task that incremented every time the task was executed. And the variable kept increasing. So it appears this variable was not reset but persistent.

Hi Nawrot,
I understand recommending using the blackboard, but right now is extremely tedious to use in blueprints, so much that I also uses “tasks scope” variables since they are persistent, as CryptoSeven discovered, or add variables to my AI controller.

The official documentation describes setting a simple vector a “more involved step”, so imagine setting lots of value in a blackboard.

In AI controller, we can’t use the “Blackboard Key Selectors” or have variables set/get helpers like structs have greatly benefits in blueprints. Right now I can’t recommend using the blackboard to anyone.

Where could I open an issue to recommend some blackboard usability features?

Bump. No-one answered this question. If you have an integer value that is LOCAL to a behaviour tree task and you increment it, the next time the task runs the variable is still incremented. I am not using any blackboard keys for this task, so all variables should be local and reset upon each call, To avoid this I have had to add a node at the start of execution that resets the integer back to 0, but this shouldn’t have to be the case. Seems like an engine bug