[C++] Post-login get Character

Hi,

I’m trying to get the character controlled by the given APlayerController in the AGameMode::PostLogin function.
But APlayerController::GetContolledPawn, as well as APlayerController::GetCharacter returns… Nothing

Is that normal ? How can I get the pawn owned by the APlayerController ?

Thanks for your help

I’m having the same issue, I searched on the internet during hours and I found no solution, maybe we are doing something wrong

void AInvadersGameMode::PostLogin(APlayerController *NewPlayer)
{
Super::PostLogin(NewPlayer);

AAInvadersAvatar *avatar = Cast<AAInvadersAvatar>(NewPlayer->GetPawn());

}

When I do something on “avatar” like avatar->SetTeam(TeamEnum::Blue); the game is crashing.
It occurs only in Standalone Mode, I don’t understand.

Hey -

I’m not sure exactly what your code looks like, but the following code works for me:

void AMyGameMode::PostLogin(APlayerController *NewPlayer)
{
	Super::PostLogin(NewPlayer);
	APawn* NewPawn = NewPlayer->GetControlledPawn();
	if (NewPawn)
	{
		if (GEngine)
		{
			GEngine->AddOnScreenDebugMessage(-1, 4.f, FColor::Yellow, TEXT("Testing"));
		}
	}
}

When I play in editor or standalone mode I do see “Testing” printed to the screen. Since you are trying to get the pawn, you’ll want to make sure that you are casting to the pawn class so that the cast will succeed. Let me know if this helps.

Cheers

Hi !
Thanks for the answer! Although this does not change our problem…
This is the code I use for tests :

void AInvadersGameMode::PostLogin(APlayerController *NewPlayer)
{
	Super::PostLogin(NewPlayer);

	APawn*	pawn = NewPlayer->GetControlledPawn();
	AAInvadersAvatar *avatar = Cast<AAInvadersAvatar>(pawn);
	if (GEngine)
	{
		if (pawn)
			GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Cyan, "Pawn exists");
		else
			GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Cyan, "Pawn does not exists");
		if (avatar)
			GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Cyan, "Avatar exists");
		else
			GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Cyan, "Avatar does not exists");
	}
}

And the result is “Pawn does not exists” and obviously “Avatar does not exists”.
This issue is happening only in Standalone Mode

I have no idea where this can come from

I copied your code into my game mode and “Pawn exists” printed in both Standalone and PIE modes. What result do you get in PIE mode on your end? Do you have the same result in a new project with this same code? Additionally, can you provide a copy of the project where you are having this problem?

In PIE, “Pawn exists” and “Avatar exists” are both printed.

We created a new project, and we have the same result with this code, it works in PIE but not in Standalone Mode.

You can find a copy of our project here :
https://www.dropbox.com/s/a5k0n25b04370w6/Invaders.rar?dl=1

Could you explain the steps you used to reproduce this in a new project? I copied the code from your PostLogin function (changing AAInvadersAvatar to ACharacter for the code to compile) into a new project . In stand alone mode it printed that both the pawn and avatar exist.

Yeah sure :
We created a new project, then a ACharacter based class (we tested in c++ and in blueprint).
Then we created a new C++ GameMode, copy paste the code, and run.
And it does not work… Now we just try our project on 4.11 preview 7, still the same problem.

I may not be following the steps correctly, I’m not seeing pawn and avatar set properly in PIE or standalone.

  1. Create a new project
  2. Add Class based on Character (MyCharacter)
  3. Add Class based on GameMode (MyGameMode)
  4. In MyGameMode.h I add void PostLogin(APlayerController *NewPlayer) override;
  5. In MyGameMode.cpp I copied the function you posted earlier, changing reference to InvaderGameMode to MyGameMode and changing references to AInvadersAvatar to MyCharacter
  6. In MyGameMode.cpp, also add include statements for MyCharacter.h and Engine.h
  7. After compiling, I set GameMode Override to MyGameMode

At this point when I PIE I see that the pawn exists but the avatar does not. Is there a step I’m missing or something I’m not setting as you are in your project? Let me know where my steps differ from what you’re doing in your project.

Hi ,

The steps seems similar to what we done in our project.
In order to be more accurate, I just followed the exact same steps in a new project.

The result is still the same for me, Pawn exists but not Avatar when I PIE, and in Standalone Mode, none of them exists…

Hello,

Could you please provide the project that you are using when you are experiencing this error, as we have been unable to reproduce it on our end. Go ahead and zip up your project, upload it to dropbox, and provide a link. You can PM me the link to download the project on the forums: https://forums.unrealengine.com/member.php?160394-Sean-Flint

Thank you

After some investigation, it seems the reason that you are seeing your pawn and avatar not exist in standalone is because post login is being called before your avatar exists in the game. In PIE, a copy of your avatar is created initially, which is why you are seeing different results.

This is an order of operations issue, and in order to solve it you’ll need to move your code over to a function that will be called after the avatar exists in the level.

Let me know if you have any further questions.

Have a great day