Get Server Variable Value On Client

Hello, I want to display the number of players on both client and server, I’m using the GetNumPlayers() function in C++, and I call that function in a blueprint widget bind function. (So I want to bind that function to a widget textblock).

It gets bound correctly, and it displays the data, but the server displays the correct number of players only (for example 2, 1 listen server, 1 client), because obviously it is the server so it knows about all the players, while all of the clients display 1 as the number of players. Which is because it only knows about itself.

So how can I get the Server number of players value on all of the clients? Server RPC? Multicast? (The c++ function with the number of players is written inside the gamemode, but also tried it in the gamestate, but no luck).

GetNumPlayers() is a bit confusingly named, because it really returns something like “count number of locally available player controllers.”

Easiest might be to replicate the number of players as a value on your GameState.

You can also count the number of PlayerState instances that are know to the client.

How would I do that? What I have done yet, is to write a function inside gamemode that calls GetNumPlayers function, then the gamestate calls the gamemode function and the widget prints that value.

Nothing is replicated yet, I should replicate the gamestate value or the gamemode value?

This is how it looks currently, this is the bind function inside the widget, it displays the number of players, but the server shows 2 the client shows 1 (with 1 listen server and 1 client instance). The function’ return value is replicated.

Okay, I got it, no need to make any custom functions anywhere, just get the length of the player array of the gamestate in the widget bind function.

Now the number of players is the same everywhere.

GameMode is not replicated to clients, but the GameState is.
The rules for Unreal gameplay object replication are documented here:

https://docs.unrealengine.com/4.26/en-US/InteractiveExperiences/Framework/GameMode/

and here:

https://docs.unrealengine.com/5.1/en-US/networking-and-multiplayer-in-unreal-engine/

It’s a bit to read through, but really helps to keep in mind.