2019-03-22 13:50:43 +00:00
# SyncDictionary
2019-06-25 05:17:28 +00:00
A SyncDictionary is an associative array containing an unordered list of key, value pairs. Keys and values can be of the following types:
- Basic type (byte, int, float, string, UInt64, etc)
- Built-in Unity math type (Vector3, Quaternion, etc)
- NetworkIdentity
- Game object with a NetworkIdentity component attached.
- Struct with any of the above
2019-08-24 15:20:33 +00:00
SyncDictionary works much like [SyncLists ](SyncLists.md ): when you make a change on the server the change is propagated to all clients and the Callback is called.
2019-06-25 05:17:28 +00:00
2019-08-19 21:52:41 +00:00
2019-06-25 05:17:28 +00:00
To use it, create a class that derives from SyncDictionary for your specific type. This is necessary because the Weaver will add methods to that class. Then add a field to your NetworkBehaviour class.
2019-03-24 09:18:31 +00:00
2019-08-20 13:29:55 +00:00
> 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.</p>
2019-08-19 21:52:41 +00:00
2019-08-20 13:29:55 +00:00
>Note SyncDictionaries must be initialized in the constructor, not in Startxxx(). You can make them readonly to ensure correct usage.
2019-08-19 21:52:41 +00:00
2019-03-24 09:18:31 +00:00
## Simple Example
2019-07-07 05:52:37 +00:00
```cs
2019-03-24 09:18:31 +00:00
using UnityEngine;
using Mirror;
public class ExamplePlayer : NetworkBehaviour
{
public class SyncDictionaryStringItem : SyncDictionary< string , Item > {}
public struct Item
{
public string name;
public int hitPoints;
public int durability;
}
2019-08-19 21:52:41 +00:00
public readonly SyncDictionaryStringItem Equipment = new SyncDictionaryStringItem();
2019-03-24 09:18:31 +00:00
2019-08-27 13:05:52 +00:00
public override void OnStartServer()
2019-03-24 09:18:31 +00:00
{
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 });
}
2019-08-27 13:05:52 +00:00
public override void OnStartClient()
2019-03-24 09:18:31 +00:00
{
// 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;
}
2019-08-27 13:05:52 +00:00
void OnEquipmentChange(SyncDictionaryStringItem.Operation op, string key, Item item)
2019-03-24 09:18:31 +00:00
{
// equipment changed, perhaps update the gameobject
Debug.Log(op + " - " + key);
}
}
```
2019-04-03 11:34:25 +00:00
2019-06-25 05:17:28 +00:00
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` , add a constructor to your SyncDictionary implementation and pass a dictionary to the base class. For example:
2019-04-03 11:34:25 +00:00
2019-07-07 05:52:37 +00:00
```cs
2019-04-03 11:34:25 +00:00
public class ExamplePlayer : NetworkBehaviour
{
public class SyncDictionaryStringItem : SyncDictionary< string , Item >
{
public SyncDictionaryStringItem() : base (new SortedList< string , Item > ()) {}
}
2019-08-19 21:52:41 +00:00
public readonly SyncDictionaryStringItem Equipment = new SyncDictionaryStringItem();
2019-04-03 11:34:25 +00:00
}
```
2019-08-19 21:52:41 +00:00