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

Why is the list not updated on the remote?

Discussion in 'Multiplayer' started by pKallv, Nov 19, 2016.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,177
    I have the following code on the players:

    Code (CSharp):
    1.  
    2.     private GameObject button1;
    3.     private GameObject button2;
    4.     public List<string> localCopyOfList = new List<string> ();
    5.  
    6.     [Command]
    7.     void Cmd_AddToList() {
    8.  
    9.         Rpc_AddToList ();
    10.  
    11.     }
    12.  
    13.     [ClientRpc]
    14.     void Rpc_AddToList() {
    15.         print ("Rpc_AddToList");
    16.  
    17.         // this code is executed on all clients
    18.         localCopyOfList.Add("#1");
    19.         print (">> " + localCopyOfList [0]);
    20.         localCopyOfList.Add("#2");
    21.         localCopyOfList.Add("#3");
    22.         localCopyOfList.Add("#4");
    23.         localCopyOfList.Add("#5");
    24.     }
    25.  
    26.     void PrintTheList() {
    27.         print ("PrintTheList: " + localCopyOfList.Count);
    28.         foreach (string mySTR in localCopyOfList) {
    29.             print ("> " + mySTR);
    30.         }
    31.     }
    Button1 = Add to List
    Button2 = PrintTheList

    However, localCopyOfList is empty on the remote player and I do not understand why. The print in the Rpc shows the data in the list but when doing the PrintTheList the List is empty.
     
  2. Max_Bol

    Max_Bol

    Joined:
    May 12, 2014
    Posts:
    168
    I tried your script and it seems to partially works :
    If the Host press Button 1, both client can press Button 2 and the print is done on the client that press it.
    If the Remote client press Button 1, nothing happens if the script is not on the remote player object due to authority issues.

    This is a kind of limitation from Unity Unet which works with the idea that any Remote Client can only directly controls one game object (and its children) remotely in a scene hosted on another client while the hosting client may controls any other on top of sending calls onto the remotes ones.

    It's a bit like remote player were a foot soldier while the host controls the rest of the army. The only way the "foot soldier" can act onto other soldiers is through the chain of command so he need to request his superior (the server) to send the order to other troops.

    Now, the way you can do this so that the remote player can send the order from the [command] function is by passing any call through a script that is placed onto the remote player's gameobject. (It can be the parent which has the Network Identity or any of its children.)

    So, to explain it in a simple way :
    • Each Remote Player can only communicate to the server through 1 Game Object with 1 single Network Identity. (This includes any childs of that Game Object.)
    • All remaining Game Object with a Network Identity are managed by the server automatically and no remote player have any authority on those.
    • To manage buttons (UI) to act onto the server, you got to pass any call through the 1 single Game Object with 1 single Network Identity that is recognized with authority from the server.

    In other words, you should make sure that the player "game object" that is spawned when a client join a hosting client to have some kind of "radio" script that can communicate whatever you need each remote client to communicate and a receiver on the server's end that can translate the message toward the right destination.

    Who said that building a online game wasn't like fighting a war? ;)
     
    pKallv likes this.
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,177
    That was a great answer, thank you very much :)
     
  4. Max_Bol

    Max_Bol

    Joined:
    May 12, 2014
    Posts:
    168
    Happy to help. Actually, I'm also kinda new to Unet so answering your question and testing it out helped me out understanding more of it.

    You actually saved me quite some headache and error on my end by making me looking it up. :p