Networking - Variables across Level

You need to use a Server RPC to send the selected color from the client. Replicated properties can only be set on the server.

Dear community,

I cant get the clients and server instances sync with variables that I stored in the Games Instance class.

In the main menu widget you can select a color. It sets a color variable in the Game Instance. On event begin play the variable stored in the game instance sets a replicated variable in the player character. A displayed text gets updated with this replicated value. The network instances doesnt sync, I tried many methods.

I have one example:
-Server selects the color blue
-Client selects the color red

-On Server side the Server text color and the Client text color is blue
-On Client side the Server has a red color and the client has a red color

But on both sides the text of the Server should be blue and the text of the client should be red.

I am pretty sure that I make a little mistake, but I cant find it out.

Thanks for your help:)

Thanks for your answer, I tried many methods. I can show you one method, which doesnt work…
Maybe you can see the issue.

For a test reason the event “ServerSetNameColor” is called every two seconds in player class.

UE4 may not be able to serialize Color class/struct (or whatever it is lol). Try sending all RGB values as floats/integers and than construct your own Color class/struct when the endpoint receives that RPC.

I think this code is running for both players on both server and client. You need to make sure it only runs for the local player (Is Local Controller).

You want each client to only set the color for the local player. I think the problem is that this code runs on the server for all players, setting them all to the server color.

Before sending the RPC to set the color, check “Is Local Controller”

Which part of the code should run locally?

Do you mean this?

And how can each client set the color locally? If so the clients cant see the color of other clients and the server or am I wrong?

Almost but you only want to send the RPC when Is locally Controlled is true.

Then the colors are wrong too. Quite the same as described at the begining.

Oh wait, I missed something in your screenshots. There is only one GameInstance on the server, so all players are saving their colors to the same property. Try moving the property to the PlayerController class.

Thanks, I thought something like that. The Problem is that the player controller doesnt exist across level. I have a main menu level with a widget and then you travel to the game level. This mean the player controller variable is set, but after creating a session with a other level the variable is empty, because a new controller was created.

How about setting it on the local game instance before moving level. Then send an RPC to the server using the client game instance value to set it on the player character & replicate to all clients?

What do mean exactly?
I set the game instance value already before switching the level.

In your first screenshot of the widget blueprint “Set Name Color” is a Server RPC? What happens if you set it locally there. Then once you’ve loaded the new map, in PlayerController begin play or something you send the value stored in the local game instance to the server.

Hi, neither game instance nor widgets replicate, they only exist locally. Therefore it makes no sense to have replicated variables there or call RPCs there…

If you want to sync widgets you need to do some manual workaround. I currently don’t see any good way to do that, what you could do is reroute the widget clicks to the player controller and from there replicate them to the server. Then use a replicated actor that does exist on the server and all clients that would store the replicated color variable. The server then sets that color variable (I would use repnotify) and then from the repnotify variable you change the color of the widget.

So widget (local client) → player controller (local client) → server (server) → replicated actor (all) → widget (all)

or in more of way like you do it

widget (local client) → game instance (local client) → player character (local client) → server (server) → player character (all) → widget (all)

So widget sets a variable inside the game instance which then calls an event on player character which then calls RunOnServer with the variable, then the server updates a repnotify variable which then executes the repnotify function on all and from there you set the widget variable.

I will try

Its the same issue

Thanks for your anwser. Its really hard to understand the second way. So I have to set the game instance variable and then I have to make an event in the player character. But what should the event do?

I did this, but it seems to be wrong. What can I correct?

I call the setcolor event on begin play (player character)

Picture 4 is the rep notifiy function.