I am trying to get a custom unique id of the players in ue5 multiplayer.
Here is what I am doing:
in the editor I setup with 3 new windows clients and a dedicated server.
I create a session in one of them with advanced session plugin
then in the two others I find and join that session.
What I have tried:
in my custom playerstate I have added a replicated string property like this
UPROPERTY(Replicated, BlueprintReadOnly)
FString PS_UniqueStringId;
then in the game mode which run on the server only, I added a function to generate unique ids
UCLASS()
class AMY_GameMode : public AGameMode
{
GENERATED_BODY()
public:
UFUNCTION()
FString GenerateUniquePlayerId();
UFUNCTION()
void ReleaseUniquePlayerId(const FString& IdToRelease);
virtual void PostLogin(APlayerController* NewPlayer) override;
virtual void Logout(AController* Exiting) override;
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
virtual void SwapPlayerControllers(APlayerController* OldPC, APlayerController* NewPC) override;
private:
TSet UsedPlayerIds;
};
FString AMY_GameMode::GenerateUniquePlayerId()
{
FString NewId;
do {
NewId = FGuid::NewGuid().ToString();
} while (UsedPlayerIds.Contains(NewId));
UsedPlayerIds.Add(NewId);
return NewId;
}
then on the postlogin and SwapPlayerControllers I get the playerstate and set it’s PS_UniqueStringId property
void AMY_GameMode::PostLogin(APlayerController* NewPlayer)
{
Super::PostLogin(NewPlayer);
AMYPlayerState* PS = Cast(NewPlayer->GetPlayerState());
if (PS)
{
PS->PS_UniqueStringId = GenerateUniquePlayerId();
PS->ForceNetUpdate();
}
else
PS->PS_UniqueStringId = "INVALID";
}
void AMY_GameMode::SwapPlayerControllers(APlayerController* OldPC, APlayerController* NewPC)
{
Super::SwapPlayerControllers(OldPC, NewPC);
if (OldPC && NewPC)
{
AMYPlayerState* OldPS = Cast(OldPC->GetPlayerState());
AMYPlayerState* NewPS = Cast(NewPC->GetPlayerState());
if (NewPS && OldPS && NewPS->PS_UniqueStringId.IsEmpty())
{
NewPS->PS_UniqueStringId = OldPS->PS_UniqueStringId;
}
else if (NewPS && NewPS->PS_UniqueStringId.IsEmpty())
{
NewPS->PS_UniqueStringId = GenerateUniquePlayerId();
NewPS->ForceNetUpdate();
}
}
}
from my tests PS_UniqueStringId is the same for two clients and different of the last one sometimes all the ids are the same on each client,
but the three are different on the server which I get from the gamestate playerarray
please take a look on this
the yellow id’s come from the local playerstate
and the blue ones from the gamestate playerarray
any idea on what’s going on please, I have been fighting with this for a few days now.
6