Replicated variables UE5.4.4 Android C++

Hi guys!

I’ve been suffering for weeks with my project, because I can’t do the following:

Imagine a local game on Android, 2 players.
I want a variable that can be seen and used by players…but…there can only be one variable.

Therefore, if player 1 changes the value of the variable, player 2 will see the new value and also be able to change the value.

The logic I have followed has been using BluePrint:

  1. Widget (Play a button)
    :down_arrow:
  2. PlayerController (Run On Service an tell to GameInstance variable change the valour)
    :down_arrow:
  3. GameInstance (Here is the variable RepNotify)

RepNotify supposedly changes the value of the variables on all clients and Host…but doesnt work for me.I don’t know if the PlayerController or GameInstance files need to be configured ( I test also in GameState), but impossible…I appreciate the help I’m stuck :melting_face:

afaik game instance is not replicated. there’s one for each client and server.
i would recommend have a manager class for those “global” variables.
replicated, always relevant, spawn ignoring collisions (should not have collisions) but owned by the server. if it’s in cpp use an AInfo.
have a variable. make that variable replicated.
each player can call an rpc on their own controller. than changes that variable on the server side.

then each player can read the value from that manager.

usually the game mode would be a nice place for that, but afaik, the game mode only exists on the server.
there’s also the player state, but the ps exists for each player controller, and i can’t remember if all players can see all other player controllers.

beware though that replication is not instant, and that network packets can be lost, and there are like 100 other caveats.

so when player a says “TellServerToPutThisValue”
and player b says “TellMeTheValue”
they might see really different things.
also beware of race conditions.
imagine both playerA and B changes a value. maybe PlayerA hit the event first, but the server receives the event for playerB first. The last value in the server is PlayerA. so playerB changes are lost.

dragons beware.

Character → owner player controller .SetSrvValue → player controller on server .SetSrvValueOnManager → Manager .Set the value
make sure you can only set the value on the server.

repnotify doesn’t “change” the value.
the value will change if it’s marked as replicated.
repnotify is called when a value is changed.

another hint. remember that there is “network frequency” so you’ll have to make sure you use a correct one (faster or slower depends on your specific case, there’s not one single “correct” one).
also network relevancy.

check these links for better help. it’s critical to get network right to avoid problems. Character LocalRole is SimulatedProxy while Controller is AutonomousProxy - #2 by nande