I’m creating a multiplayer game at the moment and I’m trying to set the playername of the player.
But when I call the method SetPlayername of the character the only thing that’s get changed is the name on the client site so other clients or the server don’t know about the name.
I use a gameinstance where I save the name because the input comes from another level I made aka the main menu.
I also use a reliable server call to update the name on the server but apperantly he does not want to.
Is there something special I need to do to make this work?
When I join a server with the open command (or OpenLevel) I provide the option ?Name= with the player name. To otherwise change it during gameplay, you’d need to call a Server Reliable function on the playerstate and have the server set the player state’s name, which is replicated automatically.
apperntly not … Cuz I already did that and the only one who could see it was the client himself.
And I cannot use console commands because in a shipped game the user does not have a console command
I Found it!
Set Player Name does not change anything at all ( in my code it does not)
Set Name of the player controller changes the name of the currently controlled player.
Set name calls a server function of the gamemode that changes the name.
if (!S.IsEmpty())
{
GetWorld()->GetAuthGameMode()->ChangeName( this, S, true );
}
Thete is one interesting thing with the “?Name=” parameter.
Just imagine, that I have 2 game mode classes . In the first game mode I change the player name by ChangeName function. Next: I’m calling ServerTravel function with connecting string and “?Name=” parameter. When the second game mode, for certain game, is loaded the, ULocalPlayer::SpawnPlayActor will be called. Let’s look at this function code:
bool ULocalPlayer::SpawnPlayActor(const FString& URL,FString& OutError, UWorld* InWorld)
{
check(InWorld);
if ( InWorld->IsServer() )
{
FURL PlayerURL(NULL, *URL, TRAVEL_Absolute);
// Get player nickname
FString PlayerName = GetNickname();
if (PlayerName.Len() > 0)
{
PlayerURL.AddOption(*FString::Printf(TEXT("Name=%s"), *PlayerName));
}
URL - contains our connection string, but, according to the sources, the “?Name” parameter will be overridden by the GetNickname(). This function is based on the UniqueId and uses gethostname function (Ws2_32.lib, implementation for Windows). Since this moment I can forget about given “?Name” parameter, because it will contain PC host + GUID name and the PlayerURL variable will be used further. In this case you can send your own parameter instead of the “?Name” and parse it in the AGameMode::Login function.
I couldn’t understand why PlayerName was forced to PC name until i found that code. Why is it done that way? PlayerName is pretty much useless because of that.
PlayerName is only forced to the PC name if you’re using the Null OnlineSubsystem. If you don’t use any OnlineSubsystem, then it should use what’s specified by ?Name=. If you’re using the OnlineSubsystem then SetName after you join, but would have to be set each time you load the level. The proper way to do this would to actually look at the source code and figure out how to extend the OnlineSubsystem to give the player a name of your choosing. Or if you’re using steam, it will automatically use the player’s steam name.