Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Network List not Syncing On Clients

Discussion in 'Multiplayer' started by LAL11, Oct 4, 2023.

  1. LAL11

    LAL11

    Joined:
    Jul 27, 2020
    Posts:
    3
    Hello. I am updating a network list from the server, and immediately calling a client RPC to spawn game objects using data from that List. However, the list does not appear to be updating on client devices. Reading the list right before calling the client RPC shows that it has been properly written by the host, but reading it on the client device during the client RPC call shows that it is not identical to the host's list. Can anyone explain why this might be the case? I thought that network lists were constant between the host and all clients. I thought maybe the problem was the server needed time to sync the list to clients, but implementing a coroutine that waited a few seconds between updating the list and calling the client RPC did not work.

    The code for updating the list and calling the client rpc is given below.

    Code (CSharp):
    1.            
    2.  
    3. //Is host
    4.  
    5. Debug.Log("Old List");
    6. DebugList();
    7.  
    8.  
    9.  //Update the list
    10.          
    11.  Ship[] ships = FindObjectsOfType<Ship>();
    12.  foreach (Ship ship in ships)
    13.  {
    14.  
    15.   //find ship in list
    16.  
    17.   int i = 0;
    18.   foreach (ShipData shipData in WargameManager.Instance.ShipList)
    19.   {
    20.   if (ship.TeamIndex == shipData.TeamIndex && ship.ShipID == shipData.ShipId)
    21.   {
    22.  
    23.  
    24.    Debug.Log("Ship Updated");
    25.         //Ship Found - create new data and update list
    26.  
    27.  
    28. WargameManager.ShipData newShipData = new ShipData(ship.TeamIndex, ship.ShipID,                                ship.EMCONRange, ship.ActiveRange, true, ship.IsActive,                                                         ship.CurrentHexTile.HexGridIndex[0], ship.CurrentHexTile.HexGridIndex[1]);
    29.  
    30.  
    31.  
    32.   WargameManager.Instance.ShipList.RemoveAt(i);
    33.   WargameManager.Instance.ShipList.Add(newShipData);
    34.  
    35.    break;
    36.    }
    37.  
    38.    i++;
    39.    }
    40.  
    41.  
    42.  
    43.  
    44.    }
    45.  
    46.  
    47.          
    48.   Debug.Log("New Ship List");
    49.   DebugShipList();
    50.  
    51.  
    52.  
    53.  
    54.   PlayerNetwork.ReturnLocalPlayer().UpdateShipListClientRpc();

    And here is the client RPC

    Code (CSharp):
    1. [ClientRpc]
    2.     public void UpdateShipListClientRpc()
    3.     {
    4.  
    5.         //Only run on clients (not host)
    6.         if (PlayerNetwork.ReturnLocalPlayer().Client)
    7.         {
    8.  
    9.  
    10.  
    11.             Debug.Log("Running Client RPC");
    12.             WargameManager.Instance.DebugShipList();
    13.  
    14.  
    15.             //Destory all Ships
    16.             Ship[] ships = FindObjectsOfType<Ship>();
    17.             foreach (Ship _ship in ships)
    18.             {
    19.                 _ship.CurrentHexTile.RemoveShip(_ship);
    20.                 Destroy(_ship.gameObject);
    21.                 Debug.Log("Ship Destroyed");
    22.             }
    23.  
    24.          
    25.  
    26.  
    27.  
    28.  
    29.             //Spawn new ones from list
    30.          
    31.         }
    32.  
    33.      
    34.  
    35.      
    36.  
    37.     }
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    639
    It's hard to tell but it's possible the rpc is running on the client before its received the network list changes. If you want to react to changes in the network list take a look at network list events. I can't find a good example but you can set it up something like this:

    Code (CSharp):
    1.     [SerializeField] private NetworkList<ShipData> shipData;
    2.  
    3.     private void Awake()
    4.     {
    5.         shipData = new NetworkList<ShipData>();
    6.         shipData.OnListChanged += OnListChanged;
    7.     }
    8.  
    9.     private void OnListChanged(NetworkListEvent<ShipData> changeEvent)
    10.     {
    11.         /* changeEvent.Type could be:
    12.         Add,
    13.         Insert,
    14.         Remove,
    15.         RemoveAt,
    16.         Value,
    17.         Clear,
    18.         Full
    19.         */
    20.     }