In the previous post we have created a basic networking behavior that reduces the health and bomb count of the character that presses the Throw Bomb input. In this final part, we’re going to add the spawn functionality for the bomb and configure our code so every character is damaged if he’s inside the explosion radius of the spawned bomb.

Before we start editing our existing code, here is the end result:

Download the source code from my github repo

The final functionality will be the following:

  • If the character has any bombs left, when he presses the Throw Bomb input, a bomb will get spawned
  • When the bomb bounces on the ground, we’re going to “arm” it
  • After a certain amount of time has passed since the bomb is armed, an explosion will take place
  • If any player is inside the explosion radius, he will take damage

Creating the Bomb class

The bomb actor constists of:

  • A static mesh
  • A projectile movement component (we’re going to use that in order to register the Bounce logic)
  • A sphere collision component (the projectile movement component works when a collision component is the root of our actor)

Create a class that inherits the Actor class and name it Bomb. Then, add the following header before the .generated.h file:

#include "GameFramework/ProjectileMovementComponent.h"

When you’re done with that, add the following declarations:

The reason we’re using a multicast function is because the SimulateExplosionFX essentially provides some “eye-candy” for our game. Since that isn’t important in terms of gameplay, it’s safe for every client to call his own implementation. Moreover, you can omit the reliable specifier in order to avoid any bottlenecks in case of bad internet connection. It’s up to you to decide what’s really important for your game and mark it as reliable.

Moving on to the logic for our Bomb class:

The last things we need in order to test our functionality is modify the code we’ve written in our character class in the previous section. Specifically, we’re going to add the actual logic for the bomb spawn and we’re going to remove the hardcoded take damage function calls.

Open up the header file of your character and add the following library before the .generated.h file:

#include "Bomb.h"

Then, declare a subclass property of our bomb:

As a last step, modify the AttempToSpawnBomb and SpawnBomb functions to match the following functionality:

Save and compile your code. Create a Blueprint based on your bomb and assign the Shape Sphere to your bomb’s static mesh. Moreover, make sure that your Sphere Component covers the whole static mesh (in my case the desired sphere radius equals to 55 units) and has a valid Collision Profile assigned to it (ie Projectile / BlockAll). Moreover, assign a valid explosion particle system to your Blueprint (I’ve used the P_Explosion that comes in with the starter content).

Then, make sure to reference your Bomb Blueprint to your Character’s blueprint and test your functionality!