# 10 Dictionaries

This example shows you how to use dictionaries in a blackboard.

These files can be found in the /Examples/\[example] and /Blackboards/Examples folders.

![Example 10 Dictionaries editor](/files/dbXtX7xargClFUTm4aCr)

## DictionaryBlackboard.cs

```csharp
﻿using System;
using System.Collections.Generic;
using System.Linq;
using CrashKonijn.RabbitBlackboard.Contracts;

namespace CrashKonijn.BlackboardPro.Blackboards.Examples
{
    [Serializable]
    public class DictionaryObj
    {
        private string name;
    }

    [Serializable]
    public partial class DictSignalObj : IAllAreSignals
    {
        
    }
    
    public partial class DictionaryBlackboard : BlackboardBehaviour
    {
        // Unity doesn't support serializing dictionaries!
        // Therefore, you won't be able to see its values in the inspector.
        // Changing and updating it's values trough code will work!
        private Dictionary<string, int> scores = new Dictionary<string, int>();
        private Dictionary<string, DictionaryObj> scoresNested = new Dictionary<string, DictionaryObj>();
        private Dictionary<string, DictSignalObj> scoresNestedSignalObj = new Dictionary<string, DictSignalObj>();
        
        // This computed method will show the values in the editor as a computed signal value.
        // Don't use this in production code, it's only for demo purposes.
        private static string _scoresLog(Signals.Scores scores) => string.Join(",\n", scores.Value.Select(x => $"{x.Key}: {x.Value}"));
    }
}
```

## DictionariesBehaviour.cs

```csharp
﻿using System.Linq;
using CrashKonijn.BlackboardPro.Blackboards.Examples;
using CrashKonijn.RabbitBlackboard.Contracts;
using UnityEngine;

namespace CrashKonijn.BlackboardPro.Examples._10_dictionaries
{
    [RequireComponent(typeof(DictionaryBlackboard))]
    public class DictionariesBehaviour : MonoBehaviour
    {
        private int _index;

        /* Simple hack to show a button in the editor */
        [Header("Adds an item to the dictionaries")]
        [Button(nameof(Add))]
        public string addButton;

        /* Simple hack to show a button in the editor */
        [Header("Removes the last item from the dictionaries")]
        [Button(nameof(Remove))]
        public string removeButton;

        /* Simple hack to show a button in the editor */
        [Header("Removes all items from the dictionaries")]
        [Button(nameof(Clear))]
        public string clearButton;

        private DictionaryBlackboard blackboard;

        public DictionaryBlackboard Blackboard
        {
            get
            {
                if (this.blackboard == null)
                {
                    this.blackboard = this.GetComponent<DictionaryBlackboard>();
                }

                return this.blackboard;
            }
        }

        public void Add()
        {
            this._index++;

            this.Blackboard.Scores.Add($"test{this._index}", this._index);
            this.Blackboard.ScoresNested.Add($"test{this._index}", new DictionaryObj());
            this.Blackboard.ScoresNestedSignalObj.Add($"test{this._index}", new DictSignalObj());
        }

        public void Remove()
        {
            if (this.Blackboard.Scores.Any())
                this.Blackboard.Scores.Remove(this.Blackboard.Scores.Keys.Last());

            if (this.Blackboard.ScoresNested.Any())
                this.Blackboard.ScoresNested.Remove(this.Blackboard.ScoresNested.Keys.Last());

            if (this.Blackboard.ScoresNestedSignalObj.Any())
                this.Blackboard.ScoresNestedSignalObj.Remove(this.Blackboard.ScoresNestedSignalObj.Keys.Last());
        }

        public void Clear()
        {
            this._index = 0;

            this.Blackboard.Scores.Clear();
            this.Blackboard.ScoresNested.Clear();
            this.Blackboard.ScoresNestedSignalObj.Clear();
        }
    }
}

```


---

# 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://blackboard.crashkonijn.com/examples/10_dictionaries.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.
