YD3K
(YD3K)
May 7, 2017, 12:15am
1
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?
Vaei
(Vaei)
May 7, 2017, 1:08am
2
Try:
UE_LOG(LogTemp, Warning, TEXT("GameState->PlayerArray.Num(): %s"), *FString::FromInt(GetWorld()->GetGameState()->PlayerArray.Num()));
YD3K
(YD3K)
May 7, 2017, 2:00am
3
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
DennyR
(DennyR)
May 7, 2017, 5:35am
4
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.
YD3K
(YD3K)
May 7, 2017, 1:37pm
5
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.
YD3K
(YD3K)
May 7, 2017, 1:42pm
6
Thanks to DennyR (see comment) for bringing me on the right track.