📋
Blackboard
  • Getting Started
  • How it works
  • FAQ
  • Release Notes
  • Concepts
    • Blackboard
    • Signals
    • Saving & Loading
    • Validations
    • Source Generators
  • Examples
    • 01 Basic
    • 02 Computed
    • 03 Events
    • 04 Saving & Loading
    • 05 Lists
    • 06 Validations
    • 07 Nested Objects
    • 08 Unity Attributes
    • 09 Object Lists
    • 10 Dictionaries
    • 11 Scriptable Object Blackboard
    • 12 Class Blackboard
    • 13 Blackboard References
Powered by GitBook
On this page
  • DictionaryBlackboard.cs
  • DictionariesBehaviour.cs
  1. Examples

10 Dictionaries

Previous09 Object ListsNext11 Scriptable Object Blackboard

Last updated 7 months ago

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

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

DictionaryBlackboard.cs

using System;
using System.Collections.Generic;
using System.Linq;
using CrashKonijn.Blackboard.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

using System.Linq;
using CrashKonijn.Blackboard.Contracts;
using CrashKonijn.BlackboardPro.Blackboards.Examples;
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();
        }
    }
}
Example 10 Dictionaries editor