A Top-Down Game with Blueprints 609
2. Double-click on the AI folder you just created to go into it.
3. Inside this new folder w e need to create a new Behavior Tree and a new Black-
board. To do this, per form the following tasks:
a. Right-click in the AI folder and choose
New->Miscellaneous->Behavior Tree.
b. Right-click in the AI folder again and select
New->Miscellaneous->Blackboard.
c. Name your Behavior Tree EnemyBT a nd your Blackboard Enemy BB.
4. Now, we need to create a new blueprin t for the Enemy Controller. Perform the
following ta sks:
a. When creating this blue print, click All C lasses.
b. Search for AI Controller and select it as the base-class (see Fig-
ure 9.40(a)).
c. Name this class blueprint Enemy Controller.
5. Now, go bac k to the main editor and search for Nav Mesh Bounds Volume in
the search bar un der the Modes tab.
6. Drag the Nav Mesh Bounds Volume onto the scene.
7. Change the scale of the volume to be roughly th e same size as our arena . Re-
member that the size of our arena was X: 3000, Y: 3000, Z:200. I suggest using
values like X:3200, Y:3200, and Z:300 fo r the size of your Nav Mesh Bounds
Volume.
8. Press the P key to see the Nav Mesh coverage on the scene. It will show up as a
green area.
9. Align the Nav Mesh Bounds Volume so it covers the entire ground of the level
(see Figure 9.40(b)). Note: I t’s okay if it goes through walls a bit as the enemies
will collide with the wall anyway.
10. Open u p the behavior tree we created earlier by double-clicking it.
11. Make sure that in the Details rollout of the behavior tree, the Blackboard
Asset is set to the EnemyBB we created earlier in step (3c).
12. Go into the event graph of the Enemy Controller we ju st crea te d in step 4.
13. Create a new Event Begin Play node.
14. Drag a wire off of the Event Begin Play and place a Run Behavior Tree
node.
15. Set the BTAsset channel of the Run Behavior Tree to our created Enemy B T
behavior tree in step (3 a). You can click on the drop-down section and search for
the behavior tre e.
16. Compile and save your Enemy Controller bluep rint.
17. Now, we will set up the AI controller of our Enemy class. Perform the following
tasks:
a. Open the Enemy blueprint.
b. Click o n the Defaults mode of the En emy blueprint.
c. Scroll down to the AI section and change the AIController class to our
created Enemy Controller (see Figure 9.40(c) ).
18. Compile and save the Enemy blueprint.
610 Game Development and Simulation with Unreal Technology
(a) (b)
FIGURE 9.41: (a) Blackboard for Programming the AI Behavior Tree. (b) Adding a New
Key in Blackboard.
IMPLEMENTING THE BEHAVIOR TRE E
Now that we have our AI controller and Behavior Tree, it is time to program the
Behavior Tree to control our enemy characters.
19. Open the EnemyBT behavior tree and select the Blackboard mode. This is the
rightmost tab on the toolbar (see Figure 9.41(a) ).
20. Click the New Key button and create a new Vector. Call it PlayerPos ition
(see Figure 9.41(b)).
21. Click on the Behavior Tree mode and create a New Task.
22. With the Ev ent Graph of the New Task open, go back to the main editor.
23. Go into your AI folder in the Content Browser you crea te d in step (1).
24. You will see that this new task has been created and named for you in the co ntent
browser. Rename it something like SetPlayerPo sition.
25. Inside the event graph of this new blueprin t cr eate a new pu blic variable called
Position.
26. Change the type of this variable to Blackboard Key Selector. There are two
easy ways to change this variable type. You c an do either one of the following:
a. Click on the d rop-down menu next to Variable Type and expand Str ucture
section (see Figu re 9.42).
b. Click on the dr op-down m enu next to Variable Type and search for
BlackboardKeySelector.
27. Right-click on the canvas and search for Event Receive Execute to place this
node in the Event Graph.
28. Right-click to the left and below the Event Receive Execute node and create a
Get Player Pawn.
29. Drag a wire from the Get Player Pawn and place a Cast to RotationChar
node to its right.
30. Connect th e execution channel of the Event Receive Execute.
31. From the execution channel of the Cast to RotationChar drag off a wire and
search for Set Blackboard Value as Vector.
A Top-Down Game with Blueprints 611
FIGURE 9.42: Blackboard Key Selector Variable Type.
32. From the Set Blackboard Value as Vector node drag a wire and search for
Finish Execute, place the node and ch eck the Success che ckbox.
33. Drop the Position variable we cre ated in step (25) into the Key cha nnel of the
Set Blackboard Value as Vector.
34. Drag a wire o ut of the As RotationChar C channel of the Cast to Rotation-
Char node and create a Get Actor Location . Note: You may have to turn context
sensitivity off.
35. Connect the Return Value channel from the Get Actor Location to the
Value input channel of the Set Blackboard Valu e as Vector node (see Fig-
ure 9.43(a)).
36. Go back into the behavior tree and cre ate yet another task. You can choose the
BTTask_BlueprintBase.
37. We will need to go back into the content browser and rename this task to
ChasePlayer.
38. Open the event grap h of the ChasePlayer.
39. Create a new public variable called TargetPosition.
40. Make the variable type also B lackboard Key Selector ( see step 26).
41. Right-click on the canvas and search for a new node called Event Recei ve
Execute to place it on the event graph.
42. From the Owner Actor channel drag off a new node called Cast to
AIController. Note: You may ne ed context sensitivity turned off for these nodes.
43. Make sure tha t the execution channel of the event node is connected to the cast to
AI controller.
44. From the casting node’s As AIController channel, drag off a Move to
612 Game Development and Simulation with Unreal Technology
(a)
(b)
FIGURE 9.43: (a) Send Player Position to Blackboard. (b) Chase the Pl ayer Net work.
Location nod e.
45. Control-drag the TargetPosition variable created ear lier onto the graph below
the Event Receive Execute. This will create a GET node for the variable.
46. Drag a wire from TargetPosition you just created to place a Get Blackboard
Value as Vector node.
47. Connect the Return Value channel of the Get Blackboard Value as Vector
node to the Destination channel of the Move to Locatio n node.
48. From the Cast Failed c hannel of the casting node, drag off a Print String node
that outputs Casting to AI Controll er Failed. This could prove usefu l for
troubleshooting if necessary.
49. Finally dr ag off the execution channel o f the Move t o Location node a Finish
Execute Node.
50. As always, check the Success ch eckbox ( see Figure 9.43(b)).
51. Go back into the Behavior Tree and drag off a sequence from the Root (see
Figure 9.44(a)).
52. From the sequence drag off two new node s; one of them a SetPlayerPosition
and the other a ChasePlayer you may type these names in the search bar, or
expand the Tasks section when you drag off the Sequence (see Figure 9 .44(b)).
A Top-Down Game with Blueprints 613
(a) (b)
FIGURE 9. 44: (a) Drag a Wire from Root to Place Sequence. (b) Create the Chase Player
and the Set Player Position Behaviors.
53. Compile and save your blueprin t.
54. Test drive your level and see how the AI behaves. Make any tweaks necessary to
make the Enemies chase the player and try to catch up with him.
What Happened in TUTORIAL 9.12. . .
In this tutorial we gave our enemy actors an Artificial Intelligence behavior to
enable them to seek and chase the p la yer. In order to establish this behavior,
we will have to create a behavior tree and a blackboard for programming o ur
sequences. We will a lso need to create an AI controller to communic ate with the
enemy class to make them dynamic.
Before pro grammin g our AI, we first need to have a tool c alled a N avigation
Mesh (or Nav Mesh for short) in our level. A Nav Mesh enables our AI characters
to perfor m pathfinding and to be able to autonomously navigate inside of the
level geometry. Once the Nav Mesh is placed in the level, the areas which can
be navigated and traversed by the AI controlled actors will be highlighted with a
green color. To see the Nav Mesh area, you can press the P key on your keyboard
to toggle the view o n and off.
Our next step in establishing ou r AI is to create the behavior tree and a black-
board for pro grammin g our AI behavior. Once you create your behavior tree, you
will be able to assign tasks. We created two tasks, one for the AI to update the
position based on where the player is located, and one to enable the AI a ctors to
chase the player’s position.
..................Content has been hidden....................

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