Spawning new enemies in the Level Blueprint

In the Level Blueprint, turn your attention to the Event Graph. We want to initiate the spawning logic as soon as the game is played, and do so in a loop after every few seconds at a rate determined by the Spawn Time variable. Add an Event BeginPlay node to the Event Graph, and attach it to a Set Timer by Function Name node. Drag the SpawnTime variable onto the Time input pin, and check the box next to the Looping input pin. Finally, type Spawn inside the Function Name input field. We will create a custom Spawn Function that will be called on each pass through this loop. Remember to create a comment around these nodes as a reminder of the Function of this loop:

Now, add a Custom Event node to empty grid space by searching for and selecting Add Custom Event…, and then rename the node as Spawn. Drag a wire from the Spawn node and attach it to a Cast To FirstPersonCharacter node. Now, attach the output execution pin of this node to a Branch node.

Drag a wire out from the Object input node of Cast To FirstPersonCharacter, and then attach it to a Get Player Character node. Then, drag a wire from the As First Person Character output pin and attach it to a Get Current Enemy Count node. Drag out the Current Enemy Count output pin of this node and attach it to an integer < integer node. Next, drag the Max Enemies variable onto the bottom input pin of this node. Finally, drag a wire out from the Condition input pin of the Branch node and attach it to the integer < integer node's output pin. Your nodes should now match the following screenshot:

The next step is to actually spawn an enemy. To do this, drag a wire out from the True output execution pin of the Branch node, and then attach it to a Spawn AI From Class node. This node is custom-built to spawn new AI objects and requires you to designate a pawn and a Behavior Tree. Select Enemy Character from the drop-down menu on the Pawn Class input pin, and then select EnemyBehavior for the Behavior Tree input. Now, we need to get random locations and rotations for the remaining inputs of this node. The Blueprint nodes used to accomplish this can be seen in this screenshot:

Start by switching tabs back to the Level Editor. Inside the World Outliner panel of FirstPersonExampleMap, click on the SpawnPoint object so that it is highlighted. With SpawnPoint selected, return to the Level Blueprint tab. Go to the far-left of the other nodes, right-click in empty grid space, ensure that Context Sensitive is checked, and select the Create a Reference to SpawnPoint option. Attach the node that appears to a Get Actor Location node. Then, attach the Return Value output pin of this node to a Get Random Point in Navigable Radius node.

Get Random Point in Navigable Radius takes a location as an input and returns a random location within a designated distance away as an output. Set the Radius input of this node to a high number. I found 1000.0 to be an appropriate number to make most of my Level available for spawning. Attach the Return Value output pin to the Location input of the Spawn AI From Class node.

Next, drag a wire from the Rotation input pin of Spawn AI From Class, and then attach it to a Make Rotator node. This node converts three Float inputs into a rotation value. We only want to choose a random rotation for our spawned enemies in the Z axis because it is the rotation that indicates the direction that the enemy is looking in. So, drag a wire from the Z (Yaw) input pin and attach it to a Random Float node. The final input for the Spawn AI From Class node, No Collision Fail, enables or disables a built-in check to see whether or not the intended spawn location of the Actor is blocked by the collision of another object. If the check fails, which means that the Actor would be spawned partially inside another object, then the Actor will fail to spawn. Since we want to ensure that our enemies are not spawning inside other objects, we will leave this input unchecked, ensuring that this test happens. Create a comment container around this series of nodes, describing its utility for finding random rotation and location near the spawnpoint object.

The final step is to increase the enemy count each time an enemy is spawned. Drag a second wire from the As First Person Character output pin on the Cast To FirstPersonCharacter node, and then attach it to a SET Current Enemy Count node. Move this node to the right of the Spawn AI From Class node, and then connect the two execution pins. Now, drag a second wire out from the Get Current Enemy Count node near the casting node, and then attach it to an integer + integer node. Change the bottom input field to 1, and then move the node to the right of Spawn AI From Class. Finally, attach the output pin of the integer + integer node to the Current Enemy Count pin of the Set Current Enemy Count node.

After adding a descriptive comment to these nodes, the result should look like this:

Compile, save, and click on Play to test your enemy spawning. Based on what you have set your spawn rate to and where you have placed your spawn point, you should regularly see new enemies appear as you run the game. You will notice, however, that the enemies are not moving once spawned unless they hear or see the player. This is because they are not being created with an established patrol point to pursue. Rather than adding patrol points to our spawned enemies, we will add randomness to our enemy navigation behavior.

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

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