Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to get player names sync?

Discussion in 'Multiplayer' started by Athalansy, Aug 28, 2015.

  1. Athalansy

    Athalansy

    Joined:
    Oct 7, 2014
    Posts:
    17
    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:
    Code (CSharp):
    1.  
    2.     [SyncVar]
    3.     public string playerName;
    4.  
    5.  
    6.     public override void OnStartLocalPlayer(){
    7.         SetPlayerName();
    8.     }
    9.  
    10.     [Client]
    11.     void SetPlayerName(){
    12.         playerName = PlayerPrefs.GetString("Name");
    13.         CmdSendNameToServer(playerName,gameObject);
    14.     }
    15.     [Command]
    16.     void CmdSendNameToServer(string nameToSend,GameObject gameObjectToChange){
    17.         gameObjectToChange.transform.FindChild("Canvas").transform.FindChild ("Text").GetComponent<Text>().text = nameToSend;
    18.     }
    19.  
    But it's not working for me..
     
    Last edited: Aug 28, 2015
  2. SuperNeon

    SuperNeon

    Joined:
    Mar 16, 2014
    Posts:
    85
    Hi !

    You don't have to send the gameObject.
    Can you try this:
    Code (CSharp):
    1. [Command]
    2. void CmdSendNameToServer(string nameToSend)
    3. {
    4.         RpcSetPlayerName(nameToSend);
    5. }
    6.  
    7. [ClientRpc]
    8. void RpcSetPlayerName(string name)
    9. {
    10.     gameObject.transform.FindChild("Canvas").transform.FindChild ("Text").GetComponent<Text>().text = name;
    11. }
     
    random_otter and Athalansy like this.
  3. Athalansy

    Athalansy

    Joined:
    Oct 7, 2014
    Posts:
    17
    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?
     
  4. SuperNeon

    SuperNeon

    Joined:
    Mar 16, 2014
    Posts:
    85
    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).
     
  5. Athalansy

    Athalansy

    Joined:
    Oct 7, 2014
    Posts:
    17
    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?
     
  6. SuperNeon

    SuperNeon

    Joined:
    Mar 16, 2014
    Posts:
    85
    Yes it will work :)
     
  7. Athalansy

    Athalansy

    Joined:
    Oct 7, 2014
    Posts:
    17
    Nice, thanks for the help, you rock! :)
     
  8. Firoball

    Firoball

    Joined:
    Aug 6, 2015
    Posts:
    60
    Code (CSharp):
    1.     [SyncVar]
    2.     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?
     
  9. Firoball

    Firoball

    Joined:
    Aug 6, 2015
    Posts:
    60
    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?
     
    Last edited: Sep 18, 2015
  10. Athalansy

    Athalansy

    Joined:
    Oct 7, 2014
    Posts:
    17
    Why do you need syncvar on name?
    Not like it's gonna change during gameplay, no?
     
  11. moco2k

    moco2k

    Joined:
    Apr 29, 2015
    Posts:
    294
    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.
     
    Firoball and Evil-Otaku like this.
  12. Firoball

    Firoball

    Joined:
    Aug 6, 2015
    Posts:
    60
    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...
     
    Last edited: Sep 21, 2015
  13. bzoli40

    bzoli40

    Joined:
    Jun 27, 2017
    Posts:
    2
    Hi guys! Since a weeks ago I wanted to find this problem answer, but I didn't find until this post. Thanks :)
     
  14. taim_ar

    taim_ar

    Joined:
    Sep 28, 2020
    Posts:
    3
    whare is the syncvar must be

    edit: the host is working and can see client name but client cant see host name
     
    Last edited: Mar 17, 2021
    MuntyMcFly likes this.