Pawn PlayerState reference on client is sometimes NULL

anonymous_user_1d9d25db
Thu, 16 Jul 2015 11:39:10 +0000
Character & Animation

I have run into some problems with PlayerState reference inside Pawn class. PlayerState is sometimes NULL on clients. This happens right after loading up a level with a server and multiple clients. It is still NULL if you keep waiting for many minutes, so it should not be a replication issue. It is also completely random which clients it happens with and on how many.

I have provided an image where I show that PlayerState reference is NULL on two of clients.

I have managed to isolate this issue down to Player Controller function BeginSpectatingState that is called immediately at startup on all clients (I dont know why this function is called). way I see it, following is what normally happens:

When level has loaded up and gameplay has started, server calls Possess and PossessedBy for clients. At this point, PlayerState reference for Pawns are set to point to same as Controller PlayerState. PlayerState is eventually replicated down to client. (I have tested that this replication successfully happens).

Sometimes however, BeginSpectatingState function is called after this replication happen. problem is that this function calls UnPossessed for Pawn which again sets PlayerState to NULL on client.

Since this sometimes happens after PlayerState was replicated from server, I believe this may be what causes it to be NULL.

This issue only happens sometimes, and only on clients. Most of time, PlayerState is ok. I have managed to reproduce this issue in a project based on Third Person template. Please see my explanation below for modifications I made:

Reproducable steps:

Using Engine version 4.8.2, I created a project based on C++ Third Person template, and did following modifications:

Create a C++ function in custom character class with below code. Also make sure to add input to project that calls this function when clicking left mouse button (or any other button). We will just use this function to check if PlayerState is NULL.

void ABugTestProjectCharacter::checkPlayerState()
{
if (Controller->PlayerState != NULL)
{
GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Yellow, TEXT(“Controller->PlayerState is ok”));
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Yellow, TEXT(“Controller->PlayerState is NULL”));
}

 if (PlayerState != NULL)
 {
 	GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Yellow, TEXT("PlayerState is ok"));
 }
 else
 {
 	GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Yellow, TEXT("PlayerState is NULL"));
 }

}

To test, click Play button as Standalone Game from inside editor and test with at least four players (1 listen server and 3 clients). (Testing with more than two players increases chances for it to happen). I also used following settings: Uncheck “Use Single Process” and use Editor Multiplayer Mode as “Play As Listen Server”.

When clicking left mouse button on some of clients, you will hopefully see that on some of them, PlayerState reference is reported as being NULL. Most of time however, PlayerState reference is ok, therefore it is a good idea to test multiple times. Hopefully you will see it as NULL some of times.

27 posts - 15 participants