skip to Main Content

Introduction to UE4 Networking – Part 2

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:

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!

This Post Has 3 Comments

  1. Followed the tutorial yesterday and I couldn’t get my bomb to work.
    I randomly got an idea today and set CollisionPresets to “Projectile” from inside of my Bomb_BP blueprint under the SphereComp (Inherited) and it started working.

    I could have just missed you setting it earlier since I was tired at the time of doing this.
    Thank you for tutorial

  2. Cheers for putting this together.

    Been a massive help getting my head around the basics of C++ networking in Unreal.

    Still got to work out exactly how it’s all working so I can work build on it but it’ll be a great starting point.

    Max is right by the way, we never enabled a collision profile / default response for the SphereComponent in the contructor so it’ll just fall through the floor if you dont something like:

    SphereComp->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Block);

    Thanks Again Orfeus.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top