A BehaviorTree
chooses a behavior to be exhibited by an AI-controlled unit at any given moment in time. Behavior Trees are relatively simple to construct, but there is a lot of setting up to do to get one running. You also have to be familiar with the components available for constructing your Behavior Tree to do so effectively.
A Behavior Tree is extremely useful for defining NPC behavior that is more varied than simply moving towards an opponent (as shown in the previous recipe with AIMoveTo
).
The process of setting up a Behavior Tree to control a character is fairly complicated. The first thing we need is a Blueprint of a Character
class derivative to control. We then need to create a custom AI Controller object that will run our Behavior Tree to control our Melee attacker character. The AIController
class inside our Blueprint will run our Behavior Tree.
The Behavior Tree itself contains a very important data structure called a Blackboard. The Blackboard is like a chalkboard for containing variable values for the Behavior Tree.
A Behavior Tree hosts six different types of node, which are as follows:
true
or false
, depending on whether the task succeeded or not (by providing a FinishExecution()
node at the end).FinishExecute()
call at the end. There is an example Service node in the Sequence node in the preceding diagram.Selector nodes attempt to execute nodes until success (after which it returns), while Sequence nodes execute all until a failure is encountered (after which it returns).
Keep in mind that if your Tasks do not call FinishExecute()
, neither Selectors nor Sequences will be able to run more than one of them in succession.
Character
. To do so, go to the Class Viewer, type Character
, and right-click. Select Create Blueprint… from the context menu that appears and name your Blueprint class BP_MeleeCharacter
.Character
class derivative. Go to Content Browser and derive a Blueprint from the AIController
class—be sure to turn off Filters | Actors only first!BP_AIMeleeController
class derivative and go to the Event Graph. Under Event BeginPlay, select and add a Run Behavior Tree node to the graph. Under BTAsset
, be sure to select your BehaviorTree_FFA_MeleeAttacker
asset.18.216.124.145