SyncDictionary

A SyncDictionary is an associative array containing an unordered list of key, value pairs. Keys and values can be any supported mirror type. By default we use .Net Dictionary which may impose additional constraints on the keys and values.

SyncDictionary works much like SyncLists: when you make a change on the server the change is propagated to all clients and the Callback is called. Only deltas are transmitted.

Usage

Add a field to your NetworkBehaviour class of type SyncDictionary.

SyncDictionary must be declared readonly and initialized in the constructor.

Note that by the time you subscribe to the callback, the dictionary will already be initialized, so you will not get a call for the initial data, only updates.

Simple Example

using UnityEngine;
using Mirror;

public struct Item
{
    public string name;
    public int hitPoints;
    public int durability;
}

public class ExamplePlayer : NetworkBehaviour
{
    public readonly SyncDictionary<string, Item> Equipment = new SyncDictionary<string, Item>();

    public override void OnStartServer()
    {
        Equipment.Add("head", new Item { name = "Helmet", hitPoints = 10, durability = 20 });
        Equipment.Add("body", new Item { name = "Epic Armor", hitPoints = 50, durability = 50 });
        Equipment.Add("feet", new Item { name = "Sneakers", hitPoints = 3, durability = 40 });
        Equipment.Add("hands", new Item { name = "Sword", hitPoints = 30, durability = 15 });
    }

    public override void OnStartClient()
    {
        // Equipment is already populated with anything the server set up
        // but we can subscribe to the callback in case it is updated later on
        Equipment.Callback += OnEquipmentChange;

        // Process initial SyncDictionary payload
        foreach (KeyValuePair<string, Item> kvp in Equipment)
            OnEquipmentChange(SyncDictionary<string, Item>.Operation.OP_ADD, kvp.Key, kvp.Value);
    }

    void OnEquipmentChange(SyncDictionary<string, Item>.Operation op, string key, Item item)
    {
        switch (op)
        {
            case SyncIDictionary<string, Item>.Operation.OP_ADD:
                // entry added
                break;
            case SyncIDictionary<string, Item>.Operation.OP_SET:
                // entry changed
                break;
            case SyncIDictionary<string, Item>.Operation.OP_REMOVE:
                // entry removed
                break;
            case SyncIDictionary<string, Item>.Operation.OP_CLEAR:
                // Dictionary was cleared
                break;
        }
    }
}

By default, SyncDictionary uses a Dictionary to store it's data. If you want to use a different IDictionary implementation such as SortedList or SortedDictionary, then use SyncIDictionary and pass the dictionary instance you want it to use. For example:

public class ExamplePlayer : NetworkBehaviour
{
    public readonly SyncIDictionary Equipment = 
        new SyncIDictionary(new SortedList());
}

Last updated