Connecting a Behavior Tree to a Character

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).

Getting ready

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.

Getting ready

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:

  1. Task: Task nodes are the purple nodes in the Behavior Tree that contain Blueprint code to run. It's something that the AI-controlled unit has to do (code-wise). Tasks must return either true or false, depending on whether the task succeeded or not (by providing a FinishExecution() node at the end).
    Getting ready
  2. Decorator: A decorator is just a Boolean condition for the execution of a node. It checks a condition, and is typically used within a Selector or Sequence block.
    Getting ready
  3. Service: Runs some Blueprint code when it ticks. The tick interval for these nodes is adjustable (can run slower than a per-frame tick, for example, every 10 seconds). You can use these to query the scene for updates, or a new opponent to chase, or things like that. The Blackboard can be used to store queried information. Service nodes do not have a FinishExecute() call at the end. There is an example Service node in the Sequence node in the preceding diagram.
  4. Selector: Runs all subtrees from left to right until it encounters a success. When it encounters a success, execution returns back up the tree.
  5. Sequence: Runs subtrees from left to right until it encounters a failure. When a failure is encountered, execution goes back up the tree.
    Getting ready

    Note

    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.

  6. Simple Parallel: Runs a single task (purple) in parallel with a subtree (gray).
    Getting ready

How to do it...

  1. Begin by creating a Blueprint for your Melee unit inside UE4. You should do so by deriving a custom Blueprint from 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.
  2. To use a Behavior Tree, we need to start by setting up a custom AI Controller for our Character class derivative. Go to Content Browser and derive a Blueprint from the AIController class—be sure to turn off Filters | Actors only first!

    Note

    Non-actor class derivatives are not shown by default in the Class Viewer! To make the AIController class show, you need to go to the Filters menu and uncheck the Actors only menu option.

  3. Create your Behavior Tree and Blackboard objects by right-clicking in Content Browser and selecting Artificial Intelligence | Behavior Tree and Artificial Intelligence | Blackboard.
  4. Open the Behavior Tree object, and under Blackboard Asset in the Details panel, select the Blackboard that you've created. Blackboards contain keys and values (named variables) for your Behavior Tree to use.
    How to do it...
  5. Open your 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.
    How to do it...

How it works…

A Behavior Tree is connected to an AI Controller, which in turn is connected to a Blueprint of a Character. We will control the behavior of Character through the Behavior Tree by entering Task and Service nodes to the diagram.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.124.145