Getting your own PC is easy on both standalone and client(they are actually the only PlayerControllers) but I cannot figure out how to get the PlayerController that belongs to the host in a Listen game
Only the server has all the player controllers, so you can only get the APlayerController of the host on the actual host.
AFAIK the controller on index 0 is always “your player controller”, so if you’re calling the following in the server, it returns the host player controller:
UGameplayStatics::GetPlayerController( GetWorld(), 0 );
If you do it on a client, it returns their player controller, and remember that in the clients the other player controllers do not exist
Also, in the change that server is dedicated, call IsLocalPlayerController() on the return from that function above, if it returns false it is a dedicated server (hence the host has no player controller).
Regards,
Nuno Afonso
#Solution C++ Code For You
NAfonso’s answer is very good.
I also just answered this over here!
https://answers.unrealengine.com/questions/214565/get-player-controller-on-listen-servers.html
Rama
I know it’s an old question, but I had encoutered the same kind of issue. So I’ll to explain 2-3 things.
You have to separate two things : Controller ID and Controller Index.
The index is the position in the world player controller’s array. It may change for the same controller especially in a Listen Server after a seamless travel with a controller swap (ex : if changing the class).
The ID (indentifier) of the controller is store in the LocalPlayer and is unique for each local controller, where 0 is the “main” controller that can be used for referencing HUD & Widgets.
So " the controller on index 0 is always "your player controller" " is not true.
If you need the main controller, you have to add a little C++ function like so :
APlayerController* UMyClass::GetMainController(const UObject* WorldContextObject)
{
// Get world context (containing player controllers)
if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull))
{
// Loop on player controllers
for (FConstPlayerControllerIterator Iterator = World->GetPlayerControllerIterator(); Iterator; ++Iterator)
{
// Get player controller from iterator
APlayerController* PlayerController = Iterator->Get();
// Get local player if exist
ULocalPlayer* LocalPlayer = PlayerController->GetLocalPlayer();
// If it's local and id is 0, it's the main controller
if (LocalPlayer != nullptr && LocalPlayer->GetControllerId() == 0)
{
return PlayerController;
}
}
}
// Not found
return nullptr;
}
Hope it’ll help some other devs !