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;
}
}
}
Last updated