How to get the Player Controller with C++?

Hi guys, how can I get the Player Controller with C++? Is there some kind of a Global object that has some useful methods like, GetPlayerController(), GetDefaultPawn() or anything like that?

I see, thank you very much :slight_smile:

Depends on what you want to do and where you want to do it.

The main way you’d do this is here.

for( FConstPlayerControllerIterator Iterator = GetWorld()->GetPlayerControllerIterator(); Iterator; ++Iterator )
{
}

Some things have the owner variable as a APlayerController (APlayerState, etc)

Clients only have 1 controller, their own. Servers have ALL controllers. You’ll want to use IsLocalPlayerController to differentiate on the server.

1 Like

Out of curiousity, does GetWorld()->GetFirstPlayerController() do the same thing?

You could also use

UGameplayStatics::GetPlayerController(GetWorld(), 0);

Then you don’t need to iterate through all of them. For example if you are in a Singleplayer Game.

4 Likes

Yes, it gets the player controller with index 0.

Yes, but it takes less code (look at implementation).
Also it doesn’t check if the World is Null (because it’s a Worlds method obviously (only for your consideration).