Making an attack task

To create an attack task that does damage, we will extend the Attack Player sequence we created in the enemy Behavior Tree. Open EnemyBehavior from Content Browser. From the Behavior Tree view, click on the New Task button and select the BTTask_BlueprintBase option from the drop-down menu that appears. As we did with the custom task to reset key values in Chapter 9, Building Smart Enemies with Artificial Intelligence, we will navigate to the Enemy folder in Content Browser and rename the newly created BTTask_BlueprintBase_New object to DoAttackTask. Double-click on DoAttackTask to return to Event Graph for the new task.

We will need to create two variables within the task, one to store the target of the damage, and one to store the amount of damage to be applied. From the My Blueprint panel, use the + sign button next to Variables to create two variables. Call the first variable TargetActor, set its type to Blackboard Key Selector, and check the box next to Instance Editable in the Details panel. Now, rename the second variable as Damage, set its type to Float, and ensure that it is set to be editable. Finally, set the Damage variable's default value to 0.3:

With the variables created, look at EventGraph of the task. We first want to grab the Target Actor variable, where we will later store a reference to the player. To begin, place an Event Receive Execute node in an empty grid space. Now, drag a wire from the output execution pin of the Event node to the graph and, in the search box, search for IsValid under the Utilities category. Attach the IsValid node to the Event node.

Add a Get Blackboard Value as Actor node to the graph. Drag the Target Actor variable to the Key input pin of Get Blackboard Value as Actor. Then, connect the Return Value output pin to the Input Object input pin of the IsValid node. Attach an Apply Damage node to the Is Valid output execution pin of the IsValid node. Next, establish how much damage is done on each attack by dragging the Damage variable onto the Base Damage input pin of the Apply Damage node. Connect the Return Value output pin to the Damaged Actor input pin of Apply Damage to establish the target of the damage.

Finally, conclude the task by attaching a Finish Execute node to the Apply Damage node and checking the box next to the Success input. After applying a descriptive comment around this group of nodes, your final result should look like this:

With the custom attack task created, return to Behavior Tree. Because we are creating this attack as a melee attack, we want the enemy to only perform the attack after reaching the player. Find the Attack Player sequence node in Behavior Tree, drag a wire down from the bottom of the node, and add a new DoAttackTask task node between the Move To and Wait task nodes, which already exist, as shown here:

Click on the DoAttackTask node and change the Target Actor selection in the Details panel to PlayerActor. You can also change Node Name to Damage Player to add some descriptive details to your use of the task. With our Behavior Tree set up to use our custom task that applies damage, we should be in a good position to test our work. However, if you compile, save, and test, you will find that the health meter does not appear to be affected when the enemy closes in on you, despite the damage being applied. To fix this, we must add an Event to change the health meter when damage is dealt. Go to Content Browser, navigate to the Blueprints folder, and open FirstPersonCharacter.

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

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