Detecting where Code is Running UE5

Chris Didier
3 min readJan 5, 2024

How to differentiat between the client and server? That is a good question. So we are going to write some code that is only going to run on the server and not on the client.

We are going to create a moving platform that moves between two fixed points.

Create a new c++ class and all classes option> StaticMeshActor

This mesh is going to continuously move in the X direction.

Note: In UE5 you can activate live coding and your visual studio of your choice by going into editor preferences. You will have to restart once selected.

Now once we have created our MovingPlatform.cpp we can start coding in it.

We want to create a public MovingPlatform function in the header as well as a Tick Function with a deltatime override

Over in the cpp.

We want to make sure the actor can be called every tick, and on every tick we want to move 5cm in the X direction.

Now once we compile and go into the editor. Drag your MovingPlatform classs into the scene and assign it a static mesh. For this a cube is fine.

As you can see the cube is moving 5cm in the X direction.

Now we can test if we are running on the server or client.

The code we write runs on the server and the client in the same way, but there are some method calls that alert us to which one we are on the client or the server. This call is has.authority. For us we want the cube to only move on the server and not on the client.

After compiling you can see that on the upper left the cube has stayed in it’s location, while on the server it has moved

This lets us know which player is on the server and which is the client.

Next stop: Authority and Replication

--

--