Update PlayerName on PlayerState and let the other players see the change

Hi!

I’m developing a multiplayer game for the first time. So, I have NO IDEA about how to do it.

I have a blueprint widget, which appears at the beginning of the game, where the player needs to set his/her name. When the player pressed the Enter key it runs:

By the way, I don’t know why it always appears an error in the Set Player Name node. I have to delete it a create it every time.

I have created a custom PlayerState class header:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/PlayerState.h"
#include "OpenDoorPlayerState.generated.h"

/**
 * 
 */
UCLASS()
class MULTIPLAYERTEST_API AOpenDoorPlayerState : public APlayerState
{
	GENERATED_BODY()

public:
	virtual void OnRep_PlayerName() override;

	UFUNCTION(BlueprintCallable)
	virtual void SetPlayerName(const FString& S) override;
	
};

And the code:


#include "OpenDoorPlayerState.h"


void AOpenDoorPlayerState::OnRep_PlayerName()
{
	Super::OnRep_PlayerName();

	// Update The name in the widget on his head

}

void AOpenDoorPlayerState::SetPlayerName(const FString& S)
{
	Super::SetPlayerName(S);

	ENetMode NetMode = GetNetMode();

	UE_LOG(LogTemp, Warning, TEXT("[ AOpenDoorPlayerState::SetPlayerName ] "));
}

Also, I have created a blueprint class that inherits from the above, BP_OpenDoorPlayerState.

But, when I run the above blueprint code, on the client, it doesn’t call the OnRep_PlayerName because the GetNetMode(); returns NM_Client.

How can I make run OnRep_PlayerName function?

I think the problem is that I don’t know how to run the blueprint code on the server.

a) if you are changing a value relying on an OnRep_ function and you want the function to be run on the server then you have to call it manually on the server (it should run automatically on client but server version needs to be called by hand).

b) Is this a dedicated server? If so blueprints are not spawned on dedicated servers (there is only gameplay logic and no “presentation” logic there by default).

How?

Thanks!

No. I’m testing it this way because this is the first time I work with multiplayer.

Thanks!

void AOpenDoorPlayerState::SetPlayerName(const FString& S)
{
	Super::SetPlayerName(S);

	ENetMode NetMode = GetNetMode();

	// check if this is on server
	if (HasAuthority()) {
// if server vall on_rep manually because server will not call it
		OnRep_PlayerName();
	}

	UE_LOG(LogTemp, Warning, TEXT("[ AOpenDoorPlayerState::SetPlayerName ] "));
}

in the specific case of a listen server you could probably do a comparison

if(NetMode == ENetMode::NM_ListenServer){
}

Thanks.

I think it should work but, I don’t think this is a standard way to do it.

I don’t think it doesn’t have so difficult to update the PlayerName in the PlayerState on the server.

Thanks again.

Answered in the other thread the OP created.