Download

Get Pawn for non-local PlayerState

Trying to implement the ability for players to spectate different players in spectate mode in ShooterGame.

Right now I’ve simply set up a BindInput to ‘Fire’ in ShooterPlayerController to initiate the call

and then I try to iterate through all player states to get the next Pawn to spectate however this is a naive attempt as PlayerState->GetOwner is only valid for local playerStates/clients

How can I get from the PlayerState to the replicated Pawn running around the world?

CODE
InputComponent->BindAction(“Fire”, IE_Pressed, this, &AShooterPlayerController::LeftClick).bConsumeInput = false;






void AShooterPlayerController::LeftClick()
{
//make sure we are spectating otherwise when a player fires their weapon they will switch cameras haha
if (IsInState(NAME_Spectating))
{
AShooterGameState const* const GameState = Cast(GetWorld()->GameState);
if (GameState)
{
for (int32 i = 0; i < GameState->PlayerArray.Num(); i++)
{
AShooterPlayerState * ShooterPlayerState = Cast(GameState->PlayerArray*);

                            //CurrentlySpectatingPlayerState is a AShooterPlayerState pointer var of ShooterPlayerController
                            //so that we know which PlayerState we're currently spectating to advance to the next player
			if (ShooterPlayerState != CurrentlySpectatingPlayerState && ShooterPlayerState != PlayerState)
			{
				CurrentlySpectatingPlayerState = ShooterPlayerState;

				AShooterPlayerController * ShooterPlayerController = Cast<AShooterPlayerController>(CurrentlySpectatingPlayerState->GetOwner());
				if (ShooterPlayerController)
				{
					ACharacter * ShooterCharacter = ShooterPlayerController->GetCharacter();
					if (ShooterCharacter)
					{
						SetViewTarget(ShooterCharacter);
					}
					else
					{
						UE_LOG(LogTemp, Warning, TEXT("ShooterCharacter for that ShooterPlayerController is NULL"));
					}
				}
				else
				{
					UE_LOG(LogTemp, Warning, TEXT("ShooterPlayerController for that PlayerState is NULL"));
				}
			}
			else
			{
				UE_LOG(LogTemp, Warning, TEXT("ShooterPlayerState == CurrentlySpectatingPlayerState || ShooterPlayerState == PlayerState"));
			}
		}
	}
}

}