UE4 Blueprints Multiplayer Get Variable From Same Class Not Working

Hi all,

I am working on a blueprints multiplayer game and am having some difficulties. I need to be able to access some values about ALL the players. What I am currently doing is (in the character blueprint) doing get all actors of class MyCharacter, then getting a variable from each of them. However, when I do this it just gives me the value of the variable within the player I am running the code in’s variable. Let me explain with an example - One player has an ID variable with value 100. The other has value 200. When I run the code from player 1, it thinks that both players ID is set to 100. When I run the code from player 2, it thinks that both players ID is set to 200. As a test, I made it get the players location instead. This time, it returned different locations for each player.

Can anyone help with this?

Thanks!

EDIT: This doesn’t just happen when I do this in my player character. If I put the same code in the Game Instance, or Game Mode, I still get the same strange result.

Okay, from my experience in Networking using Unity (not yet tried in UNreal but i know its very similar).

  1. Your variable must be synchronized across network.
  2. Use the Game mode class to have server control of the variable, thus making it synchronized.

In UE4, you can sync variables using replicated.

Also look into gameplay framework, Game mode implementation for Multi player.

The way I am doing it is I am using a node which detects if it is the server or the client running something and I am making sure that this action is only running on the server. I did try making the variable replicated but it didn’t do anything. It still thinks that both players have the same value of the variable.

Another update - When I try doing it from my Game Mode blueprint, I get the same result. It just gets 100 for both players even though one of them is set to 200.

I struggled with this for a long time.
I found that the only reliable way to uniquely identify a player across the network was not via their Pawn or their Player Controller, but through their Player State.

The Player State has a unique Player ID (I think that’s the name), which is an integer variable that is automatically set.
It will match for the same player on the client as it does on the server. You will not get this matching if you try to match via player controller index or by setting your own variable, because to find the correct Player controller or pawn to set that variable on, you need to know which one corresponds to the same unique player in the first place!

You can find ALL Player States by Game State → Player Array,
Or you can find a specific Pawn or Player Controller’s playerstate simply by dragging a wire off of it and getting the PlayerState theat belongs to it.