Hello,
I’m trying to get the player controller on a listen server. Sadly, the IsLocalPlayerController()
function returns true for every controller in the server, since it owns them all, so I’m having a hard time searching for the listen server.
I’m trying to use this code to get the hud of each player (and show some info on screen to help in debugging):
AMyHUD* hud = NULL;
APlayerController* pc = NULL;
for (FConstPlayerControllerIterator Iterator = GetWorld()->GetPlayerControllerIterator(); Iterator; ++Iterator)
{
pc = Iterator->Get();
if (pc->IsLocalPlayerController())
{
hud = Cast<AMyHUD>(pc->GetHUD());
break;
}
}
if (!hud)
return;
That works for the clients, but it’s failing for the listen server.
After some research, I started to try this variation:
AMyHUD* hud = NULL;
APlayerController* pc = NULL;
pc = UGameplayStatics::GetPlayerController(GetWorld(), 0);
if (pc->IsLocalPlayerController())
{
hud = Cast<AMyHUD>(pc->GetHUD());
}
else
{
for (FConstPlayerControllerIterator Iterator = GetWorld()->GetPlayerControllerIterator(); Iterator; ++Iterator)
{
pc = Iterator->Get();
if (pc->IsLocalPlayerController())
{
hud = Cast<AMyHUD>(pc->GetHUD());
break;
}
}
}
if (!hud)
return;
Since I found searching that the player with index 0 would be the listen server, but apparently that’s wrong.
Can you, please, help me with this issue?
Thanks in advance!