Download

Networking: Setting PlayerState variables and replicating them to other clients

I’m stuck. I’ve been trying to replicate custom variables on a custom PlayerState blueprint to all other clients. But no matter what I do, my variables are not accessible on other clients. I would like, for instance, to show a custom title above all clients’ heads. These will have to be set in the PlayerState, but I have no idea how to go about and replicate/access them from other clients.

Does anybody know how to do this or maybe have an example to show?

The way I got names to show for all clients was to not actually set them in playerstate/controller, as it seems the server will only communicate to that specific client. So I’d suggest calling a function from playerstate that sets it elsewhere. I did it in the PlayerBP, but I think it may work in gamestate.

Not entirely sure I’m doing it the “correct” way, but it works.

6d704ad73e9376f950bda7a5257e1508094caec2.jpeg

It is one way to do it, but it won’t work in my case, since I’ll be respawning a lot times and through game sessions. So I’d rather really store it in PlayerState, since it is exactly what we need according to the Unreal Engine Documentation.

The actual name is being stored in playerstate, I’m just “routing” it through the PlayerBP so the server can communicate to all clients. Please lmk if you find a better way !

What do you mean by that?

The Server should be the one changing the values of the PlayerState, the values being Replicated will be sent to the PlayerState of every remote machines. You should then be able to get the values from the client PlayerState.

Not sure what he meant, but I had a similar issue while trying to change the text above a players head. If changes the name variable inside the playerstate itself, it would only change on a single client. If I did it outside of playerstate, it worked perfectly for all clients… I concluded that when the server sends the command to all clients, it will only see one client, the one where it was commanded, because that’s all that exists inside playerstate…

Does that make sense? Do you know if this is the case?

Thanks a lot, guys! I got it to work.

The way I was implementing this was by locally setting the PlayerState value and somehow hoping it would get replicated across all clients, lol! I totally forgot to have the server set the values, that was all there was to it. I’m very new to networking and UE4 in general, so thanks for bearing with me :slight_smile:

Where did you have the server execute? PlayerState?

I did it from the GameMode, actually.

I still think it’s very tricky, for instance, changing all my Pawn’s PlayerState variables at once seems kind of awkward. But maybe I just need to do things differently.

Okay, so what I found out is that UE4 is unable to get a client’s PlayerState on BeginPlay’s same frame. I kept getting a false isValid on getting the PlayerState. However, when I add a very small delay (I guess waiting for the next frame) I suddenly am able to get the client’s PlayerState and do with it whatever I want. Is this a known way of dealing with it?

bfefd67460ccd9a93d7dad5243de0b8d435b9b2d.png