RealDocs

AGameModeBase::SwapPlayerControllers

function Engine Since 4.14
#include "GameFramework/GameModeBase.h"
Access: public Specifiers: virtual

Description

Swaps a player's viewport and network connection from the old PlayerController to a newly spawned one during seamless travel when the controller class has changed between levels.

Caveats & Gotchas

  • The old PC is destroyed immediately after the swap — do not hold references to it after calling `Super::SwapPlayerControllers`. Copy any persistent data to the new PC before calling super.
  • This is only invoked when the PlayerController class actually changes between the source and destination GameModes. If the class is the same, the existing PC is reused and this function is never called.
  • The `K2_OnSwapPlayerControllers` Blueprint event is fired at the end of this function, so Blueprint subclasses can react to the swap without overriding the C++ function.

Signature

virtual void SwapPlayerControllers(APlayerController* OldPC, APlayerController* NewPC)

Parameters

Name Type Description Default
OldPC APlayerController* The player controller from the previous level that will be discarded.
NewPC APlayerController* The newly spawned player controller for the destination level.

Return Type

void

Example

Transfer custom player data during seamless travel C++
void AMyGameMode::SwapPlayerControllers(APlayerController* OldPC, APlayerController* NewPC)
{
    // Copy persistent data before the old PC is destroyed
    if (AMyPlayerController* OldMyPC = Cast<AMyPlayerController>(OldPC))
    {
        if (AMyPlayerController* NewMyPC = Cast<AMyPlayerController>(NewPC))
        {
            NewMyPC->PersistentInventory = OldMyPC->PersistentInventory;
        }
    }

    Super::SwapPlayerControllers(OldPC, NewPC);
}

Version History

Introduced in: 4.14

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.