Where to put actor variables

I am trying to make some “custom” tutorial with Behavior Trees / AIController to check if i correctly learned how to handle the basics.

Lets say that i have 100 actors, each with a “leadership” value, randomly generated.

Now, where is the correct place to place the variable ?
For what i can see, i could place it:

  • on the actor itself
  • on the aicontroller the actor is using
  • on the blackboard the BT the aicontroller is using.

Is there some guideline on the topic? something i am missing?

thank you for your help

No there’s nothing you are missing, this is just referring to best practices, and with all best practices it is a matter of opinion.

There are a few things to consider first however - if you want to be able to drop the blueprint in the scene and set this variable from the editor, you will want to keep the variable in the character blueprint so it can be exposed. You can’t expose variables that live in the behavior tree to the editor, because before the AIController runs the selected tree at runtime, the code has no idea which class it will be using.

If on the other hand you want your behavior tree to constantly reference this variable and alter the way it runs, it should live in the blackboard.

Now of course there are a few edge cases, the agent may need to access the variable but you may still want to set it from the editor, in which case I would use two variables, one in the blackboard and one exposed version in the character blueprint. Use the event begin play node to initialise the blackoard variable from the character bluieprint. Of course this is really up to you, you may simply want to keep these variables in the character blueprint for simplicity and access them from each task node like this: (either of these graphs will work, but I recommend the AI version for your case)

Coding style in these cases never has a clear cut answer and just comes down to what you think is cleanest.

Thank you for your help. you talked about order of execution (AIController / Tree at runtime) , do you have some link about this topic?

I don’t though I’m sure some exist. This is just based on the fact that the behaviour tree only begins to run when the run behaviour tree node is pinged, so anything you do before that is guaranteed to happen first. Is there something specific you are thinking of?

Not something in particular, but in other dev contexts i have always found useful understand how things get called and in which order and why. This had always granted me more knowledge on the subject and therefore more versatility if something has to be done. for example in another question i’m trying to debug a problem with actor->aicontroller->bt ,but i can’t understand who is called and when (which event fires? on who ?), so debugging is more trial and error than a “solid” aprroach

Just for future reference, maybe it is useful for someone: Actor Lifecycle | Unreal Engine Documentation