Good evening, fellow Devs,
in a multiplayer project, I’m using a TextRender component as name label above the character of each player.
Each time a new player connects to the server, he’s got to choose a name for his character.
Therefore, players are spawned as spectators and presented a simple widget containing a text input for the name as well as a confirmation button.
When the button is clicked, the widget calls a C++ function (ServerSpawnCharacter()) in the PlayerController base class.
The character is spawned delayed by utilizing ServerRestartPlayer().
A multicast is then used to set the name + character name label on each client.
ACPlayerController
UFUNCTION(BlueprintCallable, Server, WithValidation, Reliable, Category = "General")
void ServerSpawnCharacter(const FString& CharacterName);
UFUNCTION(NetMulticast, Reliable, Category = "General")
void MulticastInitialize(const FString& NewName);
void ACPlayerController::ServerSpawnCharacter_Implementation(const FString& CharacterName)
{
ServerRestartPlayer();
MulticastInitialize(CharacterName);
}
void ACPlayerController::MulticastInitialize_Implementation(const FString& NewName)
{
((ACCharacter*)GetPawn())->SetName(NewName);
}
ACCharacter
FString CharacterName;
UFUNCTION(BlueprintNativeEvent)
void SetName(const FString& NewName);
void ACCharacter::SetName_Implementation(const FString& NewName)
{
CharacterName = NewName;
}
The BP class inherting from ACCharacter (which is used as DefaultPawn class) overrides the SetName function in a way that also updates the name label text.
So basically: [Client]SpawnCharacter() -> [Server]SpawnCharacter() -> [Every Client]SetName() + Update Text Label.
The remaining issue is that players connecting afterwards don’t get the current value of already connected players name labels replicated, despite replication being enabled for the character BP and for the TextRender component.
To me it seems like the replication ain’t working correctly.
In fact, I only moved to using RPC for something as simple as this because simply setting the name label value on the server didn’t replicate back to the clients.
Any insight on this?