How to get player names sync?

Hello!

I’m trying to make a multiplayer game, and have stumbled upon a problem I just can’t solve…

I have a player input his name in main menu scene, then that name gets saved to PlayerPrefs and player connects to the host, the player has child Canvas with child Text on it.

On Start() I pull the name from playerPrefs and set it to Text.text, and it should sync with other players, the name is displayed above the players head…

As far as I’ve understood, It should look something like this:

    [SyncVar]
    public string playerName;


    public override void OnStartLocalPlayer(){
        SetPlayerName();
    }

    [Client]
    void SetPlayerName(){
        playerName = PlayerPrefs.GetString("Name");
        CmdSendNameToServer(playerName,gameObject);
    }
    [Command]
    void CmdSendNameToServer(string nameToSend,GameObject gameObjectToChange){
        gameObjectToChange.transform.FindChild("Canvas").transform.FindChild ("Text").GetComponent<Text>().text = nameToSend;
    }

But it’s not working for me…

Hi !

You don’t have to send the gameObject.
Can you try this:

[Command]
void CmdSendNameToServer(string nameToSend)
{
        RpcSetPlayerName(nameToSend);
}

[ClientRpc]
void RpcSetPlayerName(string name)
{
    gameObject.transform.FindChild("Canvas").transform.FindChild ("Text").GetComponent<Text>().text = name;
}
2 Likes

Whoa, thank you, it works!
It doesn’t show the server name but I won’t have localPlayer anyways…
Could you just tell me how it works?

As far as I figured it out it works something like this:
Client sends the command to server to execute RPC and RPC basically means that server should send the change to all other clients right?
But does the name need to be a SyncVar then as it’s not gonna change during the gameplay?

oups sorry I forgot the SyncVar.
If you don’t change dynamically the playerName during the gameplay you don’t need the SyncVar.

As you mentioned the rpc is called on each client and update the player name.
The command is only executed on the server that why the player name was not updated on the local client.
If you don’t have a client on your server it seems the rpc is not called (I never tested).

I just tested it and it doesn’t work, damn…
If I run it on dedicated server as local client+Host, and dont disconnect the local client, it should work all fine, right?

Yes it will work :slight_smile:

Nice, thanks for the help, you rock! :slight_smile:

    [SyncVar]
    public string playerName;

As Rpc calls don’t seem to be buffered for clients joining late I wanted to sync the player names with the above method. Still have to further debug if this is really the case or if I have some hard to find timing / calling order issue hidden somewhere in my code
Whenever I try using SyncVar however the whole thing blows up…

PlayerIdentity is my NetworkBehaviour where I declare the above string.
If I remove the SyncVar attribute everything is alright.
Even initializing on declaration with =“”; does not fix this.

Am I missing something obvious?

It turns out some custom de/serialization script sitting on the same gameobject but using a different (unreliable) NetworkChannel was destroying any SyncVar automated network update on this object. I have yet to find what evil things I’m doing here and how this can influence things across different channels…

edit: found. Some strange condition made it happen that not all data was deserialized during startup/handshake phase.
Still, why is this wrong code on unreliable channel 1 influencing code for reliable channel 0? Are they sharing buffers internally? Is it a problem to use both channels on the same object in different behaviours?

Why do you need syncvar on name?
Not like it’s gonna change during gameplay, no?

I think the main reason is that player names also need to be available for those players joining the game at later points. This can be solved quite comfortably with SynVars. Alternatively, the server would probably need to send messages to joining players to inform them about all current players names. I don’t know if the second way might be a bit more efficient, but it is a bit more complicated and requires more code.

2 Likes

Unlike old Networking there don’t seem to be buffered RPCs anymore… or haven’t found out yet how to confgure those. SyncVar however is buffered.
I really miss the RPC buffer and the RemoveRPC feature. It was so useful for changing object states. Now it’s probably easier to use SyncVar and the SyncVar hook…

Hi guys! Since a weeks ago I wanted to find this problem answer, but I didn’t find until this post. Thanks :slight_smile:

whare is the syncvar must be

edit: the host is working and can see client name but client cant see host name

1 Like