Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Third Party How to use interface as value of Mirror's SyncDictionary?

Discussion in 'Multiplayer' started by Atsutake, Apr 9, 2023.

  1. Atsutake

    Atsutake

    Joined:
    Mar 8, 2023
    Posts:
    18
    Is there a way I can avoid storing durability or enchantment level for any item i.e. health potions or a fruit?

    My initial relevant code piece:

    Code (CSharp):
    1. [System.Serializable]
    2.     public struct ItemData
    3.     {
    4.         public uint ID;
    5.         public long Count;
    6.  
    7.         public ItemData(uint newID, long newCount)
    8.         {
    9.             ID = newID;
    10.             Count = newCount;
    11.         }
    12.     }
    13.  
    14. [System.Serializable]
    15.     public class SyncDictionaryInventoryData : SyncDictionary<int, ItemData> { }
    16.  
    17.     [System.Serializable]
    18.     public class SyncDictionaryEquipmentData : SyncDictionary<EquipmentSlot, ItemData> { }
    Then I wanted to add Durability and Enchantment data and
    my new code piece became:

    Code (CSharp):
    1. public interface IItemData
    2.     {
    3.         public uint ID { get; set;  }
    4.         public long Count { get; set; }
    5.     }
    6.  
    7.     [System.Serializable]
    8.     public struct ItemData : IItemData
    9.     {
    10.         public uint ID { get; set; }
    11.         public long Count { get; set; }
    12.  
    13.         public ItemData(uint newID, long newCount)
    14.         {
    15.             ID = newID;
    16.             Count = newCount;
    17.         }
    18.     }
    19.  
    20.     [System.Serializable]
    21.     public struct EquipData : IItemData
    22.     {
    23.         public uint ID { get; set; }
    24.         public long Count { get; set; }
    25.         public short Durability { get; set; }
    26.         public short Enchantment { get; set; }
    27.  
    28.         public EquipData(uint newID, long newCount, short newDurability, short newEnchantment)
    29.         {
    30.             ID = newID;
    31.             Count = newCount;
    32.             Durability = newDurability;
    33.             Enchantment = newEnchantment;
    34.         }
    35.     }
    36.  
    37. [System.Serializable]
    38.     public class SyncDictionaryInventoryData : SyncDictionary<Vector2Byte, IItemData> { }
    39.  
    40.     [System.Serializable]
    41.     public class SyncDictionaryEquipmentData : SyncDictionary<EquipmentSlot, EquipData> { }
    But this gives errors like:
    "(0,0): error Cannot generate writer for interface IItemData. Use a supported type or provide a custom writer (at Inventory.IItemData)"
     
  2. Atsutake

    Atsutake

    Joined:
    Mar 8, 2023
    Posts:
    18
    Solved, just use nullable fields.
     
    mischa2k likes this.