06 Validations
Last updated
Last updated
This example shows you how to use validations in a signal.
These files can be found in the /Examples/[example] and /Blackboards/Examples folders.
using CrashKonijn.Blackboard.Contracts;
using CrashKonijn.BlackboardPro.Blackboards.Examples.Attributes;
namespace CrashKonijn.BlackboardPro.Blackboards.Examples
{
public partial class ValidationsBlackboard : BlackboardBehaviour
{
// This value can never be less than 0
// Note: that this is not the same attribute as the one Unity provides.
[Min(0)]
private int _bullets;
// This is a custom validation that will capitalize the string
// It can be viewed in ./Attributes/CapitalizeAttribute.cs
[Capitalize]
private string _name;
// Validations can be stacked!
// This value can never be less than 0 or greater than 150
[Min(0)]
[Max(150)]
private int _health;
// This is a computed signal that is normalized health between 0 and 1.
// Even if health becomes higher than 100, this value will never be higher than 1.
[Normalize(100f, true)]
private static float _normalizedHealth(Signals.Health health) => health.Value;
// This is a computed signal that is a curve between 0 and 1.
[EaseInOutCurve(0f, 1f, 1f, 0f, 2f)]
[MinMax(0f, 1f)]
private static float _shouldHeal(Signals.NormalizedHealth health) => health.Value;
}
}
using BlackboardsLite;
using CrashKonijn.Blackboard.Contracts;
using CrashKonijn.BlackboardPro.Blackboards.Examples;
using UnityEngine;
namespace CrashKonijn.BlackboardPro.Examples._06_validations
{
[RequireComponent(typeof(ValidationsBlackboard))]
public class ValidationsHealthBehaviour : MonoBehaviour
{
/* 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;
/* Simple hack to show a button in the editor */
[Header("Add 5 bullets.")]
[Button(nameof(AddBullets))]
public string addBulletsButton;
/* Simple hack to show a button in the editor */
[Header("Removed 5 bullets")]
[Button(nameof(RemoveBullets))]
public string removeBulletsButton;
private ValidationsBlackboard blackboard;
public ValidationsBlackboard Blackboard
{
get
{
if (this.blackboard == null)
{
this.blackboard = this.GetComponent<ValidationsBlackboard>();
}
return this.blackboard;
}
}
public void DoDamage()
{
this.Blackboard.Health -= 10;
}
public void Heal()
{
this.Blackboard.Health += 10;
}
public void AddBullets()
{
this.Blackboard.Bullets += 5;
}
public void RemoveBullets()
{
this.Blackboard.Bullets -= 5;
}
}
}