📋
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
  • ClassBlackboard.cs
  • ClassBehaviour.cs
  1. Examples

12 Class Blackboard

This example shows you how to use a class Blackboard.

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

ClassBlackboard.cs

using System;
using CrashKonijn.Blackboard.Contracts;

namespace CrashKonijn.BlackboardPro.Blackboards.Examples
{
    // Make sure the class is serializable so it can show in the editor
    [Serializable]
    public partial class ClassBlackboard : IBlackboard
    {
        private int health = 50;
        private static bool isLowHealth(Signals.Health health) => health.Value < 50;
    }
}

ClassBehaviour.cs

using CrashKonijn.Blackboard.Contracts;
using CrashKonijn.BlackboardPro.Blackboards.Examples;
using UnityEngine;

namespace CrashKonijn.BlackboardPro.Examples._12_blackboard_class
{
    public class ClassBehaviour : MonoBehaviour
    {
        // You can manage your own instance of classes that implement IBlackboard.
        [SerializeField]
        private ClassBlackboard blackboard = new ClassBlackboard();
        
        /* Simple hack to show a button in the editor */
        [Header("Removes 10 health.")]
        [Button(nameof(DoDamage))]
        public string doDamageButton;
        
        /* Simple hack to show a button in the editor */
        [Header("Adds 10 health")]
        [Button(nameof(Heal))]
        public string healButton;
        
        private void OnEnable()
        {
            blackboard.IsLowHealthSignal.OnValueChanged.AddListener(OnLowHealthChanged);
            blackboard.HealthSignal.OnValueChanged.AddListener(OnStartPlayerHealthChanged);
        }

        private void OnDisable()
        {
            blackboard.IsLowHealthSignal.OnValueChanged.RemoveListener(OnLowHealthChanged);
            blackboard.HealthSignal.OnValueChanged.RemoveListener(OnStartPlayerHealthChanged);
        }
        
        private void OnLowHealthChanged(bool value)
        {
            Debug.Log($"Is low health changed to {value}");
        }

        private void OnStartPlayerHealthChanged(int value)
        {
            Debug.Log($"Start player health changed to {value}");
        }
        
        public void Heal()
        {
            blackboard.Health += 10;
        }
        
        public void DoDamage()
        {
            blackboard.Health -= 10;
        }
    }
}
Previous11 Scriptable Object BlackboardNext13 Blackboard References

Last updated 7 months ago