This page will give an overview of how to utilize the Dynamic Subtrees in our standard behavior trees to modify an NPCs behavior. This page will not go into depth on the function and use of Behavior Trees in general, nor will it go over how to create your own behavior trees.
In a Behavior Tree you are able to run another tree as part of your behavior. This can be done by either using a
RunBehavior
task or a
RunBehaviorDynamic
task. Functionally they are both the same under the hood during a trees execution, the difference is that the
RunBehaviorDynamic
task allows you to modify what tree you target at runtime.
Each
RunBehaviorDynamic
task has an
Injection Tag
field and a
Default Behavior Asset
field. The Example from the above selected task:
The
InjectionTag
field is used to denote tasks that should use the same subtree when modified at runtime. For example if you have multiple locations in your tree where you want to enter a “Passive” subtree, you would tag each of those with the same
InjectionTag
. These tags are defined as Gameplay Tags and the default ones can be found in the
NPCGameplayTagTable
.
The
DefaultBehaviorAsset
field is used to describe the default tree that should be run, if the tree is not modified at runtime. This default will apply to any Controller which does not modify them.
Our existing AI Controllers have a
DefaultDynamicBehaviorTrees
which is used when an NPC begins to run its behavior to set their default dynamic subtrees. The
DefaultDynamicBehaviorTrees
field is a map pairing
FGameplayTag
keys to
UBehaviorTree
values. Here you can define any Injection Tags you want to modify upon startup for instances of this controller, with the associated
UBehaviorTree
value being the new subtree.
When you are creating NPCs that you want to behave in their own specific way, creating a custom AI Controller based off of an existing one is the recommended approach. This lets you define the
DefaultDynamicBehaviorTrees
field while maintaining existing functionality.
The following is an example using our
HumanAIController
blueprint:
In the case our NPC runs the
BT_SimpleAttack
Behavior Tree the Highlighted nodes will be modified, as their injection tags match those found in our Controller’s
DefaultDynamicBehaviorTrees
map.
We do not only have to operate by modifying the defaults found in the fields of our Controller or Behavior Tree, we are also able to change the subtrees at runtime. All that is needed to do this is a reference to our NPC’s Controller.
Will utilize our Controller’s
DefaultDynamicBehaviorTrees
to set any subtrees in our table to their defaults
Will consume a list of Injection Tags which it will search for in our Controller’s
DefaultDynamicBehaviorTrees
and set any matching tags to their defaults
Will take in one Injection Tag and one Behavior Tree asset, and will set any subtrees matching the Injection Tag to the given Behavior Tree
The given Injection Tag does not need to exist in our Controller’s
DefaultDynamicBehaviorTrees
map
Consumes a map of Injection Tag keys and Behavior Tree values in the same format of the Controller’s
DefaultDynamicBehaviorTrees
field. Loops through these pairs and will call
SetBehaviorSubtree
for each one
When you are setting multiple Subtrees it is important to note: the tasks associated with the injection tag you set will not be proactively set in the future. Should another subtree be injected that itself contains a task using the same tag as one you previously set, it will not automatically update the that task. The new dynamic task will run whatever it’s default set in the Behavior Tree task is unless otherwise overridden.