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
usingSystem;usingCrashKonijn.RabbitBlackboard.Contracts;namespaceCrashKonijn.BlackboardPro.Blackboards.Examples{ // Make sure the class is serializable so it can show in the editor [Serializable]publicpartialclassClassBlackboard:IBlackboard {privateint health =50;privatestaticboolisLowHealth(Signals.Health health) =>health.Value<50; }}
using CrashKonijn.BlackboardPro.Blackboards.Examples;
using CrashKonijn.RabbitBlackboard.Contracts;
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();
/* 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()
{
this.blackboard.IsLowHealthSignal.OnValueChanged.AddListener(this.OnLowHealthChanged);
this.blackboard.HealthSignal.OnValueChanged.AddListener(this.OnStartPlayerHealthChanged);
}
private void OnDisable()
{
this.blackboard.IsLowHealthSignal.OnValueChanged.RemoveListener(this.OnLowHealthChanged);
this.blackboard.HealthSignal.OnValueChanged.RemoveListener(this.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()
{
this.blackboard.Health += 10;
}
public void DoDamage()
{
this.blackboard.Health -= 10;
}
}
}