Download

Why is it not straight forward to get the Player Controller of a Pawn?

I’m trying to create an array for the player controllers but I’m having a hard time finding a nice and tidy solution for this.

This code code doesn’t seem to work:

void AMyGameModeBase::PostLogin(APlayerController* NewPlayer)

{
Super::PostLogin(NewPlayer);

Players.Add(Cast<AMyPlayerController>(NewPlayer));

}

So I tried to get a all the pawns with GetAllActorsOfClass but for some reason it’s not a straight forward procedure to get the player controller of the Pawn.

Could someone tell me what would be a nice and elegant solution to this problem?

Thanks

UWorld maintains a player controller list but you can only access it indirectly with an iterator
(UWorld::GetPlayerControllerIterator() ).

1 Like

you get a Pawn’s controller with GetController(), I do believe.

I’d be more curious why you’re not getting what you think you’re getting in PostLogin, though. I’d set a breakpoint right at your call to Super::PostLogin and see what the value of NewPlayer is.

1 Like

Do like the shootergame example and just use the iterator like mentioned above. The moment you try to create your own array you need to mange it so it is much cleaner just using the built in iterator.

	for (FConstPlayerControllerIterator It = GetWorld()->GetPlayerControllerIterator(); It; ++It)
		AShooterPlayerController* PC = Cast<AShooterPlayerController>(*It);

I was going to say the same thing as eblade, what does “doesn’t seem to work” mean?
If you put a breakpoint there, what is the argument you get?
Is your GameMode set to create the right kind of player controller?