PlayerState not keep between Levels (Seamless Travel)

Hi !

I’m working on a multiplayer game so I’m implementing all the fundations to the game.
Actually i’m facing a problem.
In my case, players have to choose a ‘class’ (Sentinel for exemple) that going to be saved in the PlayerState.
But one I run

GetWorld()->ServerTravel(MapName);

My players travels but when I’m on my new map all the data is erased.
But I’ve done this

Here is my header file

class AEONRECKONING_API ABasePlayerState : public APlayerState
{
	GENERATED_BODY()

public:
	ABasePlayerState();

	void BeginPlay() override;

	void CopyProperties(APlayerState *PlayerState);

	void GetLifetimeReplicatedProps(TArray<FLifetimeProperty> &OutLifetimeProps) const override;

	UPROPERTY(BlueprintReadOnly, Replicated)
	class UBaseClass *CurrentClass;
};

and here is the c++ one

ABasePlayerState::ABasePlayerState()
{
    PrimaryActorTick.bCanEverTick = false;

    bReplicates = true;
    bNetLoadOnClient = true;
    bAlwaysRelevant = true;
}

void ABasePlayerState::BeginPlay()
{
    Super::BeginPlay();

    // log current class address
    GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("CurrentClass for player %s address is %p"), *GetFName().ToString(), CurrentClass));
}

void ABasePlayerState::CopyProperties(APlayerState *PlayerState)
{
    Super::CopyProperties(PlayerState); // This is called so we preserve data chosen to be preserved by default

    if (ABasePlayerState *NewPlayerState = Cast<ABasePlayerState>(PlayerState))
    {
        GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, FString::Printf(TEXT("Copying properties for player %s, class %p"), *NewPlayerState->GetFName().ToString(), CurrentClass));
        NewPlayerState->CurrentClass = CurrentClass;
    }
}

void ABasePlayerState::GetLifetimeReplicatedProps(TArray<FLifetimeProperty> &OutLifetimeProps) const
{
    Super::GetLifetimeReplicatedProps(OutLifetimeProps);

    DOREPLIFETIME(ABasePlayerState, CurrentClass);
}

But in the CopyProperties function the CurrentClass value is nullptr whereas it’s defined if I do a check before the seamless travel :confused:
And in the new beginPlay it’s nullptr too :frowning:

I really don’t understand why it’s not working as I’m following the doc and the previous topic here so if someone have any idea I’ll take it ahah !

You are using a raw UPROPERTY pointer which most likely is set to nullptr during pendingkill or destruction of the object it points to. Using a TSoftClassPtr instead should fix this.

If it still doesn’t work try to copy a value type like an integer to verify that the CopyProperties function work as intended.

2 Likes