Search Unity

Question MLAPI NetworkedDictionary same key when behavior created from client, with IsServer Bool

Discussion in 'Netcode for GameObjects' started by hoesterey, Jan 16, 2021.

  1. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    Hi.
    I'm not understanding why NetworkDictionary is exploding.

    This code that is in a component on the spawned object:
    Code (CSharp):
    1.  
    2. public class ResourceBehavior : NetworkedBehaviour
    3.     {
    4.        private NetworkedDictionary<string, int> m_NetIntResource = new NetworkedDictionary<string, int>();
    5.  
    6.              public override void NetworkStart()
    7.              {
    8.                       base.NetworkStart();
    9.                       if(IsServer)
    10.                       {
    11.                             m_NetIntResource.Add("Test", 0);
    12.                       }
    13.             }
    14.        }
    15.  
    16.  


    Works fine when objects are spawned from the host. However when the client sends a server a message to spawn the same object using this function:

    Code (CSharp):
    1. [ServerRPC]
    2.         private void Server_RequestAddActorToEvent(int objectToSpawnID, Vector3 position, Quaternion rotation)
    3.         {
    4.             RecordContent actor = Database.Instance.GetRecordActor(objectToSpawnID);
    5.  
    6.             //ServerCreateActor.
    7. #if GBSERVER
    8.             GamePlayer.GetLocal.CurrentEditModeEncounter.Value.Edit_AddActor(actor as RecordActor, position, rotation);
    9.        
    10. #endif
    11.         }

    The client gets the following error. I don't see how I'm adding a key twice. It should ONLY be adding on the server. I've put logs in and far as I can tell nothing I'm doing is adding this key twice. What am I missing?

    Code (CSharp):
    1. ArgumentException: An item with the same key has already been added. Key: Test
    2. System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) (at <9577ac7a62ef43179789031239ba8798>:0)
    3. System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) (at <9577ac7a62ef43179789031239ba8798>:0)
    4. MLAPI.NetworkedVar.Collections.NetworkedDictionary`2[TKey,TValue].ReadDelta (System.IO.Stream stream, System.Boolean keepDirtyDelta) (at <19597f6ed6a54f65b071432411566fd3>:0)
    5. MLAPI.NetworkedBehaviour.HandleNetworkedVarDeltas (System.Collections.Generic.List`1[T] networkedVarList, System.IO.Stream stream, System.UInt64 clientId, MLAPI.NetworkedBehaviour logInstance) (at <19597f6ed6a54f65b071432411566fd3>:0)
    6. MLAPI.Messaging.InternalMessageHandler.HandleNetworkedVarDelta (System.UInt64 clientId, System.IO.Stream stream, System.Action`2[T1,T2] bufferCallback, MLAPI.Messaging.Buffering.PreBufferPreset bufferPreset) (at <19597f6ed6a54f65b071432411566fd3>:0)
    7. MLAPI.NetworkingManager.HandleIncomingData (System.UInt64 clientId, System.String channelName, System.ArraySegment`1[T] data, System.Single receiveTime, System.Boolean allowBuffer) (at <19597f6ed6a54f65b071432411566fd3>:0)
    8. MLAPI.NetworkingManager.HandleRawTransportPoll (MLAPI.Transports.NetEventType eventType, System.UInt64 clientId, System.String channelName, System.ArraySegment`1[T] payload, System.Single receiveTime) (at <19597f6ed6a54f65b071432411566fd3>:0)
    9. MLAPI.NetworkingManager.Update () (at <19597f6ed6a54f65b071432411566fd3>:0)
    10.  
     
  2. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    After more testing I'm pretty confident I'm either missing something fundamental with how Networked Dictionary's are supposed to work or they simply don't...


    Another test that "should" 100% verify that I'm not adding a key twice. No Asserts thrown.
    Code (CSharp):
    1.  if(IsServer)
    2.             {
    3.  
    4.                 Debug.Assert(!m_NetIntResource.ContainsKey("Test"), " HOW?????");
    5.                 if(!m_NetIntResource.ContainsKey("Test"))
    6.                     m_NetIntResource.Add("Test", 10);

    With the above code I still recieve the following error if an object is instantiated and spawned on server from client with a server RTP:

    <19597f6ed6a54f65b071432411566fd3>:0)
    MLAPI.NetworkedBehaviour.HandleNetworkedVarDeltas (System.Collections.Generic.List`1[T] networkedVarList, System.IO.Stream stream, System.UInt64 clientId, MLAPI.NetworkedBehaviour logInstance) (at <19597f6ed6a54f65b071432411566fd3>:0)
    MLAPI.Messaging.InternalMessageHandler.HandleNetworkedVarDelta (System.UInt64 clientId, System.IO.Stream stream, System.Action`2[T1,T2] bufferCallback, MLAPI.Messaging.Buffering.PreBufferPreset bufferPreset) (at <19597f6ed6a54f65b071432411566fd3>:0)
    MLAPI.NetworkingManager.HandleIncomingData (System.UInt64 clientId, System.String channelName, System.ArraySegment`1[T] data, System.Single receiveTime, System.Boolean allowBuffer) (at <19597f6ed6a54f65b071432411566fd3>:0)
    MLAPI.NetworkingManager.HandleRawTransportPoll (MLAPI.Transports.NetEventType eventType, System.UInt64 clientId, System.String channelName, System.ArraySegment`1[T] payload, System.Single receiveTime) (at <19597f6ed6a54f65b071432411566fd3>:0)
    MLAPI.NetworkingManager.Update () (at <19597f6ed6a54f65b071432411566fd3>:0)

    Any ideas?
     
  3. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
  4. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    Thanks! I was afraid of that. May switch to a 3rd party solution that has more features production ready.
     
    Last edited: Jan 16, 2021