Download

Replicate Health and Armor variables for the Players

Hello.

I made a character who has health and armor in C++ (also Kills and Deaths)

I want these kind of variables to be replicated over the clients (players) in the server lobby.
I have this HUD that shows on the screen the current health and armor (and also Kills and Deaths) of the player.

In the second window (client) both health and armor are set to 0, but If I try to damage myself I can verify that I actually HAVE that ammount of health and armor, but just the HUD does not display those values.

Also, If I pickup something on the client (second window), the PickupMessage appears **ALSO **on the server (play in the editor)

The “Armor +15” message **on the top **appears both in the client and server (it’s a GEngine->AddOnScreenDebugMessage), but at the center it appears on the server and not on the client.

Why?

Please don’t link me Youtube videos and things like that, cause I already watched a lot of them but still can’t understand.

Any answer?

Any answer? pt.2

  1. Please only bump threads every 4 days. Otherwise you clutter the forum and others can’t get their questions answered.

  2. You need to mark variables to let them replicate. Check out this wiki entry to get started: https://wiki.unrealengine.com/Networking/Replication

It may be a bit out dated on the function signature, but the idea stays the same.

I already read all those guides… I can’t find the solution <.<

Correct me if I am wrong, but just setting a variable to replicate wont do it. One thing to keep in mind when doing multiplayer is that replication of variables only goes FROM server TO clients, and not any other way. What this means is that any change to a variable on the local client will not reflect on other clients even if its set to replicate. So, what you need to do is also make sure you are changing the variable on the server. For example, when taking damage, the health variable is set to replicate, but also the function changing the health variable when hit should be Server side function, executed on server.

Correct, that’s the main idea behind replication. Every Variable that should reach other clients needs to be set to replicate and changed by the Server.
This is also used to prevent cheating since Client changed variable will never reach other clients and the Server always holds the correct variables.

If you have a variable “Health” set to replicate and the client attacks someone, you need to let the Server reduce the health. For this you either have the whole damaging thing run
on the Server anyway or you let the Client call a “RunOnServer” function that is called on the PlayerController or any other Class that the Client is owner of.

And replicating a simple Health variable is something the Wiki shows you. The second example on the wiki shows you how to get a function to call something on the server and change
a bool. Change the bool with the float health variable and that’s it.

Guys, I think I figured out where’s the problem with my game.

f9a86bcfb294a87aa5ca300ce7853e4b8ffbb455.png

Accessed none MyCharacter

Why? Why isn’t my character reachable in the Blueprint (the cast always fails)?

2.png

What’s the game mode of the level set to (is the game mode configured to spawn a first person character pawn)?

This is the GameMode

1.png