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

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:

[System.Serializable]
    public struct ItemData
    {
        public uint ID;
        public long Count;

        public ItemData(uint newID, long newCount)
        {
            ID = newID;
            Count = newCount;
        }
    }

[System.Serializable]
    public class SyncDictionaryInventoryData : SyncDictionary<int, ItemData> { }

    [System.Serializable]
    public class SyncDictionaryEquipmentData : SyncDictionary<EquipmentSlot, ItemData> { }

Then I wanted to add Durability and Enchantment data and
my new code piece became:

public interface IItemData
    {
        public uint ID { get; set;  }
        public long Count { get; set; }
    }

    [System.Serializable]
    public struct ItemData : IItemData
    {
        public uint ID { get; set; }
        public long Count { get; set; }

        public ItemData(uint newID, long newCount)
        {
            ID = newID;
            Count = newCount;
        }
    }

    [System.Serializable]
    public struct EquipData : IItemData
    {
        public uint ID { get; set; }
        public long Count { get; set; }
        public short Durability { get; set; }
        public short Enchantment { get; set; }

        public EquipData(uint newID, long newCount, short newDurability, short newEnchantment)
        {
            ID = newID;
            Count = newCount;
            Durability = newDurability;
            Enchantment = newEnchantment;
        }
    }

[System.Serializable]
    public class SyncDictionaryInventoryData : SyncDictionary<Vector2Byte, IItemData> { }

    [System.Serializable]
    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)"

Solved, just use nullable fields.

1 Like