# SyncDictionary

SyncDictionary supports all the functionality a normal dictionary would, just as SyncList supports List abilities.

Callbacks for SyncDictionary are similar to SyncList. And like other synchronization types changes are set immediately before the callback occurs.

```csharp
private readonly SyncDictionary<NetworkConnection, string> _playerNames = new();
    
private void Awake()
{
    _playerNames.OnChange += _playerNames_OnChange;
}

// SyncDictionaries also include the asServer parameter.
private void _playerNames_OnChange(SyncDictionaryOperation op,
    NetworkConnection key, string value, bool asServer)
{
    /* Key will be provided for
    * Add, Remove, and Set. */     
    switch (op)
    {
        // Adds key with value.
        case SyncDictionaryOperation.Add:
            break;
        // Removes key.
        case SyncDictionaryOperation.Remove:
            break;
        // Sets key to a new value.
        case SyncDictionaryOperation.Set:
            break;
        // Clears the dictionary.
        case SyncDictionaryOperation.Clear:
            break;
        // Like SyncList, indicates all operations are complete.
        case SyncDictionaryOperation.Complete:
            break;
    }
}
```

{% hint style="danger" %}
If you are using this SyncType with a container, such as a class or structure, and want to modify values within that container, you must set the value dirty. See the example below.
{% endhint %}

```csharp
[System.Serializable]
private struct MyContainer
{
    public string PlayerName;
    public int Level;
}

private readonly SyncDictionary<int, MyContainer> _containers = new();

private void Awake()
{
    MyContainer mc = new MyContainer
    {
        Level = 5
    };
    _containers[2] = mc;
}

[Server]
private void ModifyContainer()
{
    MyContainer mc = _containers[2];
    // This will change the value locally but it will not synchronize to clients.
    mc.Level = 10;
    // You may re-apply the value to the dictionary.
    _containers[2] = mc;
    // Or set dirty on the value or key. Using the key is often more performant.
    _containers.Dirty(2);
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://fish-networking.gitbook.io/docs/guides/features/network-communication/synchronizing/syncdictionary.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
