Managing spawn rates and limits with variables

Rather than relying on the placement of enemies in a Level on a set patrol, we are going to gradually spawn those enemies in the Level to present an increasingly aggressive threat to the player. As a consequence, we will set up our spawning logic to trigger repeatedly in a loop, with the time between spawns determined by a variable. In the My Blueprint panel, add a new variable called SpawnTime. Set its type to Float, make it editable, and set the default value to 10.0 for a 10-second spawn rate.

In addition to setting the spawn rate, we will also want some form of limiter on the spawning of enemies. Without this, an enemy would spawn every 10 seconds until the game ends, potentially filling the map with dozens of enemies. To prevent this, we will create an additional variable to set a cap on the number of enemies. Create another variable and call it MaxEnemies. Set the variable type to Integer and make it editable. I set the default value of MaxEnemies to 5, but you can set the number as high or as low as you think is the appropriate maximum number of enemies that your Level can support:

In order for MaxEnemies to Function as a cap on the number of enemies present in the level, we need a way of keeping track of the current number of enemies. To do this, we will temporarily leave the Level Blueprint, and instead, open the FirstPersonCharacter Blueprint, which is found in the Blueprints folder of Content Browser. Inside FirstPersonCharacter, create a new variable called CurrentEnemyCount. Set its type to Integer and ensure that Instance Editable is checked.

Level Blueprints can receive information from other Blueprints through casting, but there is no easy way to get information stored in a Level Blueprint and use it in other Blueprints. As a consequence, any variable you make that is likely to be affected by Actions in other Blueprints, such as the CurrentEnemyCount variable, is better placed outside of the Level Blueprint. In this case, we are storing it with the rest of our game data information on the player object.

Now that we have a variable to track the current enemy count, we need to decrease this value whenever an enemy is destroyed. Recall that the Blueprint nodes managing enemy destruction are located in the EnemyCharacter Blueprint. Open the Enemy folder in Content Browser, and then open the EnemyCharacter Blueprint:

In the EnemyCharacter Blueprint, locate the series of nodes that are triggered by the Event Hit node. Near the end of this node sequence, find the Cast To FirstPersonCharacter node. Create some additional space between the Set Target Kill Count node and the Branch node, and then break the connection between their input and output execution nodes. Drag a wire out from the As First Person Character output pin of Cast To FirstPersonCharacter, and attach it to a Set Current Enemy Count node. Drag a wire from this node's Current Enemy Count input pin to an integer - integer node.

Now, drag another wire from the As First Person Character output pin of Cast To FirstPersonCharacter, and then attach it to a GET Current Enemy Count node. Attach this node to the top input pin of the integer - integer node, and then fill in the bottom input field with 1. Next, connect the SET Target Kill Count node to the input execution pin of SET Current Enemy Count. Finally, connect the output execution pin of this node to the Branch node.

Now that we have established variables to determine the spawn rate and cap the number of enemies in a level, we should return to the Level Blueprint. Click on the tab with a Blueprint icon that is labeled FirstPersonExampleMap.

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

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