Multiplayer - PlayerState's SetPlayerName not replicating

Chumble
Fri, 19 Feb 2021 19:57:57 +0000
Character & Animation

I’m trying to use the “SetPlayerName” function on the server to set the player’s name but I am getting very inconsistent results.

If I do not use SetPlayerName at all, the server will return an empty string when using GetPlayerName and the clients will return my computer’s name followed by a 4 digit generated ID.

If I run SetPlayerName in BeginPlay of the PlayerState on the server, it will return correctly when using GerPlayerName on the server. Clients, however, still get the same computer name with 4 digit ID.

PlayerState code:

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

	if(GetNetMode() == NM_DedicatedServer)
	{
		FString Name = "Player ";
		Name.AppendInt(FMath::RandRange(0,100));
		SetPlayerName(Name);
		UE_LOG(LogTemp, Error, TEXT("Set Player Name to %s"), *GetPlayerName());
	}
}

void AMyCPPPlayerState::Tick(float DeltaSeconds)
{
	if(GetNetMode() == NM_Client)
	{
		UE_LOG(LogTemp, Error, TEXT("My playername is %s"), *GetPlayerName());
	}
}

Log Output:

LogTemp: Error: Set Player Name to Player 98
LogTemp: Error: My playername is DESKTOP-AAA-AB70
LogTemp: Error: My playername is DESKTOP-AAA-AB70
LogTemp: Error: My playername is DESKTOP-AAA-AB70
LogTemp: Error: My playername is DESKTOP-AAA-AB70
LogTemp: Error: My playername is DESKTOP-AAA-AB70
LogTemp: Error: My playername is DESKTOP-AAA-AB70
LogTemp: Error: My playername is DESKTOP-AAA-AB70
LogTemp: Error: My playername is DESKTOP-AAA-AB70
LogTemp: Error: My playername is DESKTOP-AAA-AB70
LogTemp: Error: My playername is DESKTOP-AAA-AB70
LogTemp: Error: My playername is DESKTOP-AAA-AB70
LogTemp: Error: My playername is DESKTOP-AAA-AB70
LogTemp: Error: My playername is DESKTOP-AAA-AB70
LogTemp: Error: My playername is DESKTOP-AAA-AB70
LogTemp: Error: My playername is DESKTOP-AAA-AB70
LogTemp: Error: My playername is DESKTOP-AAA-AB70
LogTemp: Error: My playername is DESKTOP-AAA-AB70
LogTemp: Error: My playername is DESKTOP-AAA-AB70

1 post - 1 participant