Restart Player after SetLifeSpan destroys

I have an OnDeath function within my character class where I am handling dying.

void AFPSCharacter::OnDeath(AController* Killer, AActor* DamageCauser)
{
	// Destroy the character after x seconds
	SetLifeSpan(5.0f);
    
    // Get the player controller
	APlayerController* playerController = Cast<APlayerController>(GetController());
	if (playerController)
	{
		// IMPORTANT: We don't actually want to do this until after current pawn is destroyed
		//			  or it's going to restart the player and then destroy the new pawn
		// Tell the server to restart the player
		playerController->ServerRestartPlayer();
	}
}

I am calling SetLifeSpan to destroy the characters corpse after 5 seconds.

However, I don’t want ServerRestartPlayer to be called until his corpse is actually destroyed.

I have tried overloading OnDestroyed within AFPSCharacter, but at that point I no longer have a reference to the playerController to call ServerRestartPlayer.

You can override BeginDestroy()

Ok dont know why this if delay there.
Try to override LifeSpanExpired() than.

You should execute your code before super call or get playercontroller before super call and execute restart after.

Yeah so I tried overriding BeginDestroy() and am getting odd results. I set a breakpoint at the Super::BeginDestroy() line and it doesn’t hit that for a long while after the corpse has already been destroyed (like 25 or so seconds). And even when it does hit the function it doesn’t seem to have the player controller there either.

FPSCharacter.h

private:
    virtual void BeginDestroy() override;

FPSCharacter.cpp

void AFPSCharacter::BeginDestroy()
{
	Super::BeginDestroy();

	// Get the player controller
  	APlayerController* playerController = Cast<APlayerController>(GetController());
	if (playerController)
	{
		// Tell the server to restart the player
		playerController->ServerRestartPlayer();
	}
}

There we go :slight_smile: That worked great. Thank you!

void AFPSCharacter::LifeSpanExpired()
{
	// Get the player controller
  	APlayerController* playerController = Cast<APlayerController>(GetController());

	Super::LifeSpanExpired();

	if (playerController)
	{
		// Tell the server to restart the player
		playerController->ServerRestartPlayer();
	}
}