Networking: issues with replication

Hello guys.

I’m making a multiplayer game in which every character has his own health and armor.

Every player spawns (SHOULD spawn) with 100 health and 50 armor <- these values are indicated in a HUD

When I run the network simulation, here’s what happens:

The server-player has it’s HUD looking perfect, meanwhile the client-player has not.
Also, if I pickup the health in the client, the health goes to the server-player!

I don’t know what’s going on, can you please help me?

I replicated the Health and Armor variables from the Blueprints.

Also, if I try to replicate the variables of the HUD, it doesn’t work: they turn to their initial value (none)

The HUD

The variables that, when I set them to Replicate, after they come back to NONE

Thanks very much in advance!

Your going to need to show us where you setup the initial variables, how you retrieve them to display on the HUD, and how you handle picking up the health.

The character health/armor variables are initialized in the FirstPersonCharacter blueprint as follows

This is how the HUD gets the health/armor and displays them:

This is how the health gets picked:

When and where are you calling ‘Init’? When and where are you setting the ‘My Character’ variable? As for the pickup, rather than casting using ‘Get Player Character’ at index 0, try casting using the ‘Other Actor’ from the event.

I’m calling Init in the FirstPersonCharacter. As soon as the character is created, Init will set 100 health and 50 armor to him.

My Character is a variable that I declared inside the HUD Blueprint. It’s a reference to the actor whose health and armor should be updated on the HUD.

I did as you said. Nothing changes

Also, I cannot see the bullets on the server when the client shoots and vice versa

You are calling Init on EventBeginPlay? Do you have any sort of replication set up for it? If not, you should create an event that runs on server, this event should initialize the variables. Something you could try is adding a delay before calling Init, that may work. Also, is the screenshot above in your pickup blueprint?

No, it’s the default INIT function of the FirstPersonCharacter

Well, I only checked all the variables (health, armor…) as Replicates so they should be supposed to replicate their value

Umh, I call INIT from nowhere!

Yes!

Well I have never used the Init function, so I don’t know who it is called for. I’d imagine that if you moved the Init code to EventBeginPlay, it would work as desired as far as initialization goes.

No, it doesn’t. That’s weird.

I don’t know how to solve… i’m self-taught and my only way to learn is from you guys…

There is no function called Init that comes with the FirstPersonCharacter by default. I assume you are calling this from EventBeginPlay. Try using a custom event node set to run on server that runs init instead. Then, call this custom event from EventBeginPlay.

You will probably have an issue with your pickup, as it is setting a replicated value without being run only on the server. This could yield bad results later on (server overwriting the value).

As for the projectile, it is not set to replicate at all by default. You will need to set it to replicate, and replicate movement in order to see it across clients.

Here are screenshots from my small bit of testing.

FirstPersonCharacter Blueprint:

Widget Variable Settings:

Projectile Replication:
projectilereplication.png

Game Windows:

Do remote players not call begin play on another players pawn?

Well, I changed the properties for the projectile and now the client can see server’s bullets but not vice versa

Furthemore, the HUD for the client still remains 0 - 0
The positive thing is that now if I pickup the health from the client, it doesn’t increases server’s health… but simply nothing happens lol

ANOTHER THING: when I move the controller up and down, both client and server don’t see this movement… they only see if the character moves left/right but not up/down.

What do you need to solve the problem?

What?

Remove your SwitchHasAuthority from Init. Setup a custom event on your blueprint that is set to run on the server. Have the custom event run the init function, and call it from beginplay. If you have net ownership of the actor calling this, the client is basically requesting the server handle running the function. If this doesn’t fix your issue with replication of the variables, then you have an issue with how you are attempting to display them (it doesn’t look like this is the issue).

As I suspected with the projectiles: they are not being spawned purely by the server. The server player has server authority already, and the spawned projectiles are hence on the server, making them replicate to clients. Your client is, however, spawning them only on the client. Client spawned actors do not replicate to other players, as they do not exist on the server. You need to, once again, use a custom event set to run on server. This way, the client will request the server version of himself to spawn the object. This will fix the issue you are experiencing with the projectiles.

EDIT:

Here is a wonderful series of tutorials on network replication https://www.youtube.com/playlist?list=PLZlv_N0_O1gYwhBTjNLSFPRiBpwe5sTwc

It would be wise to watch through those, as they will certainly help you understand how replication works in UE4. (I still go back to this when I’m having issues sometimes)

im too lazy to read all posts, so if you have precise questions about blueprint network, i can answer it
here i have old video of my project, everything is networked, so if you want to know how to make something, just ask.

Done

Done (in the level blueprint)

Done

The problem is still there

I’ve already watched those tutorials

Delay on begin play, always. Small one like 0.2 would be ok.

Uuuh, you’re right! Now it works! Why should I always add a delay on begin play?

Problem number 2:

When I pickup an Item, the text “Item picked!” appears on both the server and the client. How to solve this?

This is the function called when a new Item is picked

Well, if you are interacting with actors that are generated at runtime, or modified at runtime, or getting some variable that is modified by another actor’s begin play event etc., you can’t be sure you will get consistent results because you don’t know what order different actor’s begin play event have been called, and so won’t know the state of those actors or variables.

The delay means its begin play will happen after all the other actor’s. The delay is only a workaround really, if you know what it relies on you should only call the event if the conditions are how you expect them to be. Or not use begin play at all, and instead call functions on actors to initialize them, from other relevant actors that have already been initialized. You would know what order things should happen, and can ensure that it will always happen that way.

1.Seams like begin play has wrong implementation/name and instead of “call when everything done” is “call when construction script of this exact object is done” which means it cannot rely on any other objects, even basic one: you cant rely on player controller existence on level begin play. Also there are delay between PIE sessions created and clients connected to them, so there are another issue, you might call the object which is exist only on server and not yet replicated.
2. I dont understand what you asking, so lets rebuild the question
What you want to happen (exact effect what you want to achive)
When you want it to happen (what cause this effect)
Where you want it to happen (what object you want to have this functionality)
go on.