# Callbacks

Go back to the hierarchy and select one of the ElementInfo, under UICanvas/Infos:

<figure><img src="/files/VWwfKqFgiN5IglCK56jk" alt="" width="209"><figcaption></figcaption></figure>

In the inspector you will see a component attached to it "UIElementInfo".

<figure><img src="/files/OfFcbeBinxXs2kVAguht" alt="" width="375"><figcaption></figcaption></figure>

This component is responsible for updating the UI whenever an entry in the dictionary is added or removed for a specific element. Let's check out the code to see how we can register to useful callbacks.

1. OnItemAdded / OnItemRemoved&#x20;

In this case, the first time a certain element is spawned, we want to enable the count text. Therefore, we can use the OnItemAdded event and do this. Similarly, when an element is removed from the dictionary (when the count is 0, see previous code page), we want to disable the count text.&#x20;

```csharp
[SerializeField] private ScriptableDictionaryElementInt _scriptableDictionary = null;
[SerializeField] private TextMeshProUGUI _text = null;

private void Awake()
{
    _scriptableDictionary.OnItemAdded += OnItemAdded;
    _scriptableDictionary.OnItemRemoved += OnItemRemoved;
    _text.transform.parent.gameObject.SetActive(false);
    _image.sprite = _scriptableEnumElement.Icon;
}

private void OnDestroy()
{
    _scriptableDictionary.OnItemAdded -= OnItemAdded;
    _scriptableDictionary.OnItemRemoved -= OnItemRemoved;
}

private void OnItemAdded(ScriptableEnumElement element, int value)
{
    if (element != _scriptableEnumElement)
        return;
    _text.transform.parent.gameObject.SetActive(true);
}

private void OnItemRemoved(ScriptableEnumElement element, int value)
{
    if (element != _scriptableEnumElement)
        return;
    _text.transform.parent.gameObject.SetActive(false);
}
```

2. Modified

Now, when an element is already in the dictionary but only the amount is changing, we simply want to update the count text. Therefore, we register to the **Modified** event, and update the count text with the value of that key (using the element we have referenced as the key)

```csharp
[SerializeField] private ScriptableEnumElement _scriptableEnumElement = null;
[SerializeField] private ScriptableDictionaryElementInt _scriptableDictionary = null;

private void Awake()
{
    _scriptableDictionary.Modified += OnModified;
}

private void OnDestroy()
{
    _scriptableDictionary.Modified -= OnModified;
}

private void OnModified()
{
    if (_scriptableDictionary.TryGetValue(_scriptableEnumElement, out var count))
        _text.text = $"Count: {count}";
}
```

That's it very simple!


---

# 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://obvious-game.gitbook.io/soap/scene-documentation/9_scriptabledictionaries/callbacks.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.
