skip to Main Content

Introduction to UE4 Networking – Part 1

In this post we’re going to re-create the Blueprint project from the official Networking Tutorials of Unreal Engine 4 into a C++ project. I highly advise you to follow the official tutorials first, since they explain in great detail some of the concepts that will be briefly mentioned here. This post assumes you’re familiar with the basic networking terminology and functionality inside UE4 (explained in videos 1 – 4 in the official tutorials).

Before we start re-creating the mentioned project in C++, here is the end result of this post:


In the first part, we’re going to create some basic networking functionality, meaning, we will create a mechanism that decreases the health and bomb count by a press of button. In the next part, we’re going to modify this functionality a bit. Specifically, we will spawn bombs and when they explode we will decrease the HP in every affected character.

A link with the full source code of networking tutorials will be given in the final part.

In the above video, both characters have a text renderer component that displays their Health and Bomb Count which are replicated variables. This means that we want every character to be able to “see” the Health and Bomb count of other characters.

Since damage is an important part of our gameplay, meaning we don’t want any cheating to take place and ruin our fun, we’re going to tell the server to apply any damage to specific players. This means that in case a character wants to damage another character, the following logc we’ll be executed:

  • The character will tell the server that he wants to damage another character
  • The server will perform a check to determine if this is a valid request (for example a dead character can’t damage another character).
    • In case of a valid request, the server will apply the damage to the requested character

Having said that, let’s start by replicating the Health and Bomb variables. Then, we will create the mentioned damage logic.

Create a Third Person C++ template project and add the following include to the header file of your project [YourProjectName.h]:

#include "Net/UnrealNetwork.h"

This is necessary in order to use any networking features in our game.

Replicating Health and Bomb Count

In order to replicate these variables, we’re going to use RepNotifies. RepNotifies execute a certain function when a variable changes. Each variable has its own function. For example, when the Health variable changes, a function named OnRep_Health will get called. The naming of the function follows the official naming convention of UE4.

Before adding any code, add the following header before the .generated.h file inside your character’s class:

#include "Components/TextRenderComponent.h"

We need that in order to use the TextRenderComponent that is displayed in the video.

Then, add the following code inside the header file of your character’s class:

Then, inside the constructor of your class, create and attach the CharText:

Moreover, add the following logic inside the GetLifetimeReplicatedProps:

Then, add the following logic for the DoRep functions as well as the initializations:

As you can see, the On_Rep functions simply update the TextRenderComponent. Here is the logic of the UpdateCharText:

At this point, we have achieved a simple logic that initializes the BombCount and Health variables and updates the CharText each time the Health or the BombCount variables change. The last thing we need is an input that (for this part only) decreases the Bomb Count and applies damage to a player.

To apply damage to a character, we’re going to use the built-in TakeDamage function. As we already mentioned, since the damage is important to our gameplay, only the server will be able to apply any damage to any character. To handle this, each time a player attemps to damage another player, we’re going to perform a check to determine if the damage dealer is the server. If not, then the server will handle that functionality.

Please note that we will follow the same logic for the bomb spawn too. This means that if a client wants to spawn a bomb, he will tell the server to do so.

Inside the header file of your character, add the following logic:

Then, provide the following logic for each function:

As a last step, provide the following input for your character:

PlayerInputComponent->BindAction("ThrowBomb", IE_Pressed, this, &ANetworkingTutCharacter::AttempToSpawnBomb);

At this point, if you test your project with two windows (one being the server and one being the client) you will have the same result as the video in the beginning of this post.

Avatar photo

This Post Has 5 Comments

  1. Hey great tutorial 😉 but i have a question regarding networking… If i have two teams with different colors, how would i go about setting each of the player’s colors according to their team index?

    1. Hey Alex, assuming you have already set up the logic that correlates a color with a team index (let’s say 1 = red, 2 = green, etc..) , one way to do that is to make a Rep notify to change the material of the character based on his team index.

      If you’re wondering how that works, you will see a similar example on the next part of the tutorial.


  2. In end of video it says you can test project but I was not able to until adding definitions for
    void ANetworkingTutCharacter::ServerSpawnBomb_Implementation();
    bool ANetworkingTutCharacter::ServerSpawnBomb_Validate();

    Thank you for tutorial

    1. You’re right, I forgot to write that you need to provide an implementation and a return true in the validation for this part at least. Updated – Thanks!


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