Code clarity - use of IsPlayerControlled() to check if the player has died

Hi! I wasn’t entirely convinced with the code clarity in the PawnKilled function where a cast is used to check if the pawn that was killed (passed in) was the player.

There is a built-in Pawn function called IsPlayerControlled() which seems to me like a decent way to check if the pawn that died was the player, as long as it’s called before DetachFromControllerPendingDestroy(). Is this technique fine to use?

My PawnKilled function implementation:

void AKillEmAllGameMode::PawnKilled(APawn* PawnKilled)
{
	Super::PawnKilled(PawnKilled);

	UE_LOG(LogTemp, Warning, TEXT("Pawn Killed!"));

	if (PawnKilled->IsPlayerControlled())
	{
		APlayerController* PlayerController = Cast<APlayerController>(PawnKilled->GetController());
		if (PlayerController != nullptr)
		{
			PlayerController->GameHasEnded(nullptr, false);
		}
	}
}
1 Like

That should be fine.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms