Identifying a wander point with a custom task

Once you've opened EnemyBehavior, click on the Blackboard tab. We need to create a key that will store the location of the next destination that the enemy should wander to. Unlike the PatrolPoint key, our destination won't be represented by an in-game Actor, but rather, by vector coordinates. Now, create a new key in the Blackboard panel and call this key WanderPoint. Change Key Type to Vector. Next, click on the Behavior Tree tab to return to the Behavior Tree.

In the Behavior Tree, we can remove two of the sequences we have already established to handle moving between patrol points and idling. Select the Move to Patrol and Idle sequence nodes, along with their attached task nodes, and delete them. Now, drag a wire out from the Selector node and attach it to a new Sequence node. Rename this node to Wander, and then move this node to the right of both the Attack Player and Investigate Sound sequences.

The first task of our Wander sequence will be to determine where in the Level the enemy should be wandering. For this, we will need to create another custom task. Click on the New Task button and select the BTTask_BlueprintBase option from the drop-down menu. Return to Content Browser and find the new task object called BTTask_BlueprintBase_New in the Enemy folder. Rename this task object to FindWanderPointTask. Double-click on FindWanderPointTask to open the Event Graph Editor for the new task.

We will set up nodes that will grab the location of the enemy Actor and generate a random point within a radius around that location. This point will then be stored as our WanderPoint. The nodes used to accomplish this can be seen in the following screenshot:

First, we need to create a variable within this task that will allow us to establish a reference to Blackboard Key. Add a new variable and call it WanderKey. Set the type to BlackboardKeySelector and make sure that the Instance Editable checkbox is selected.

Now, add an Event Receive Execute node to the Event Graph. Drag a wire from the Owner Actor output pin and attach it to a Cast To AIController node. Then, attach the two nodes' execution pins. Now that we have access to the AI controller, we can access its controlled Actor's location. Drag a wire out from the As AIController output pin of the casting node, and then attach it to a Get Controlled Pawn node. Next, drag a wire out from the Return Value output pin of this node, and then attach it to a GetActorLocation node.

With the enemy Actor's location in hand, we can now generate the random location that will serve as our WanderPoint. Drag a wire out from the Return Value output pin of Get Actor Location, and then attach it to a Get Random Point in Radius node. Set the Radius value of this node to a large number that should cover most or all of your Level. I set mine to 10000.

Next, we need to store this vector in the Blackboard. Drag a wire out from the Random Location output pin of Get Random Point in Radius, and then attach it to a Set Blackboard Value as Vector node. Drag the WanderKey variable onto the Key input of this node, and then attach the input execution pin to the output execution pin of the Cast To AIController node. Finally, drag a wire out from the output execution pin of Set Blackboard Value as Vector, attach it to a Finish Execute Node, and check the Success input box.

Add a descriptive comment around these nodes. Then, compile and save this Blueprint. Click on the EnemyBehavior tab to return to the Behavior Tree.

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

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