Download

Assigning new pawn to playercontroller takes 30 seconds(approx) to update client

I have a character that calls the below method PlayerCharacterDied. The code works. However, it takes a good 30 seconds for the client to get the new pawn. The server side PlayerController OnPossess is called with the new pawn too.

Looking to get this to be a bit more responsive.

4.22.3. Dedicated server build.


void AAWGameModeBase::PlayerCharacterDied(AController* Controller)
{
    UE_LOG(AWGeneralLog, Warning, TEXT("Game Mode PlayerCharacterDied"));

    FActorSpawnParameters SpawnParameters;
    SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
    ASpectatorPawn* SpectatorPawn = GetWorld()->SpawnActor<ASpectatorPawn>(SpectatorClass, Controller->GetPawn()->GetActorTransform(), SpawnParameters);

    Controller->UnPossess();
    Controller->Possess(SpectatorPawn);

    FTimerHandle RespawnTimerHandle;
    FTimerDelegate RespawnDelegate;

    RespawnDelegate = FTimerDelegate::CreateUObject(this, &AAWGameModeBase::RespawnHero, Controller);
    GetWorldTimerManager().SetTimer(RespawnTimerHandle, RespawnDelegate, RespawnDelay, false);

    //AAWPlayerController* PC = Cast<AAWPlayerController>(Controller);
    //if (PC)
    //{
    //    PC->SetRespawnCountdown(RespawnDelay);
    //}

}

void AAWGameModeBase::RespawnHero(AController* Controller)
{
    UE_LOG(AWGeneralLog, Warning, TEXT("Game Mode RespawnHero"));

    if (Controller->IsPlayerController())
    {
        UE_LOG(AWGeneralLog, Warning, TEXT("Game Mode RespawnHero IsPlayerController"));

        // Respawn player hero
        AAWPlayerController* PC = Cast<AAWPlayerController>(Controller);
        AActor* PlayerStart = ChooseAWPlayerStart(PC);

        FActorSpawnParameters SpawnParameters;
        SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
        auto HeroClass = GetDefaultPawnClassForController(Controller);
        AAWPlayerCharacter* Hero = GetWorld()->SpawnActor<AAWPlayerCharacter>(HeroClass, PlayerStart->GetActorLocation(), PlayerStart->GetActorRotation(), SpawnParameters);

        APawn* OldSpectatorPawn = Controller->GetPawn();
        UE_LOG(AWGeneralLog, Warning, TEXT("Game Mode Respawn Hero OldSpectatorPawn %s"), *OldSpectatorPawn->GetName());
        Controller->UnPossess();
        OldSpectatorPawn->Destroy();
        Controller->Possess(Hero);
        Controller->ForceNetUpdate();
        //RestartPlayer(Controller);

        auto pawn = Cast<AAWPlayerCharacter>(Controller->GetPawn());
        UE_LOG(AWGeneralLog, Warning, TEXT("Game Mode Respawn Hero IsPlayerController pawn %s"), *pawn->GetName());  //<-This shows the new pawn
        pawn->PawnClientRestart();
        pawn->m_SQL_PC_ID = Cast<AAWPlayerController>(Controller)->PlayerCharacterFromConnection;
        pawn->Intialization();

        UE_LOG(AWGeneralLog, Warning, TEXT("Game Mode Respawn Hero IsPlayerController finish"));

    }
}