Multiplayer GetGameState()->PlayerArray on Client is always 1

Try:

UE_LOG(LogTemp, Warning, TEXT("GameState->PlayerArray.Num(): %s"), *FString::FromInt(GetWorld()->GetGameState()->PlayerArray.Num()));

After connecting with 2 clients (on different machines) to my dedicated server I’m running the following code in MyCustomPlayercontroller through a Button:

AGameStateBase *MyGameState = GetWorld()->GetGameState<AGameStateBase>(); 
//BP node equivalent (?)
//AGameStateBase *MyGameState = UGameplayStatics::GetGameState(GetWorld());

if (MyGameState)
{

	UE_LOG(LogTemp, Warning, TEXT("GameState->PlayerArray.Num(): %d"), GetWorld()->GetGameState()->PlayerArray.Num());

}

Every time a new client connects the Server is running this code in MyCustomGameModeBase::InitNewPlayer(…):

 ...
    UE_LOG(LogTemp, Warning, TEXT("GameState->PlayerArray.Num(): %d"), GameState->PlayerArray.Num());
    ...

Output Client1: GameState->PlayerArray.Num(): 1

Output Client2: GameState->PlayerArray.Num(): 1

Output Server: GameState->PlayerArray.Num(): 2

The same thing in Blueprint :

Output Client1: GameState->PlayerArray.Num(): 2

Output Client2: GameState->PlayerArray.Num(): 2

Output Server: GameState->PlayerArray.Num(): 2

This works like a charm so am I missing something in my C++ implementation?

That leads to the same result, Unfortunately the log formatting is not the problem…

Another test:

if (MyGameState)
	{
	int foo = 0;
		for (APlayerState* SinglePS : MyGameState->PlayerArray)
		{
			++foo;
		}
		UE_LOG(LogTemp, Warning, TEXT("foo: %s"), *FString::FromInt(foo));
	}

Output Client1 : foo: 1

Output Client2 : foo: 1

Output Server : foo: 2

Where are you calling this code? I feel like you are calling it after login but before the actual PlayerState has been created and replicated.

This was indeed the problem, thanks for bringing me on the right track DennyR

I’m reffering to this: https://answers.unrealengine.com/questions/183488/player-controller-get-controlled-pawn-client-delay.html

which states the same problem.
After adding a RPC to MyCustomPlayerController and executing it at PostLogin() it works.

Thanks to DennyR (see comment) for bringing me on the right track.