Validations

The framework allows for the creation of complex blackboard systems in Unity, facilitating communication between different parts of your game or application. One of the powerful features of this framework is the ability to validate signals using IValidation attributes. These attributes ensure that the data within your signals meets certain criteria, enhancing the robustness and reliability of your blackboard system.

IValidation attributes are used to enforce data integrity and constraints on the signals within a blackboard. By applying these attributes, developers can define rules that signal values must adhere to, such as minimum and maximum values, required string patterns, or custom validation logic.

Using validation attributes on Signals

To use IValidation attributes on signals, you first need to define a signal within a class that extends BlackboardBehaviour. Then, you can apply one or more IValidation attributes to this signal to enforce validation rules.

Here's an example of a blackboard with a signal that uses a built-in validation attribute:

using CrashKonijn.Blackboard.Contracts;
using UnityEngine;

namespace CrashKonijn.Blackboard.Blackboards.Examples
{
    public class HealthBlackboard : BlackboardBehaviour
    {
        [MinMax(0, 100)]
        public int health;
    }
}

Creating Custom Validation Attributes

To create a custom validation attribute, you need to extend the ValidateAttributeBase class and override its validation method. This method should return true if the value passes validation and false otherwise.

Here's an example of a custom validation attribute that ensures a string value is always capitalized:

public class CapitalizeAttribute : ValidateAttributeBase
{
    public override T Validate<T>(T value)
    {
        if (value is string stringValue)
        {
            return (T) (object) stringValue.ToUpper();
        }

        return value;
    }
}

You can then apply this custom validation attribute to a signal in a BlackboardBehaviour:

namespace CrashKonijn.Blackboard.Blackboards.Examples
{
    public class PlayerNameBlackboard : BlackboardBehaviour
    {
        [Capitalize]
        public string playerName;
    }
}

Existing validations

The following validations are provided by the package:

NormalizeAttribute

Normalizes a numeric value relative to a base value, optionally limiting the result between 0 and 1.

Parameters

  • baseValue (float): The value that should represent 1f in the normalization process. Default is 1f.

  • limit (bool): Determines whether to clamp the normalized value between 0 and 1. Default is false.

Usage

[Normalize(100f, true)]
private float speed;

EaseInOutCurveAttribute

Transforms a value based on an ease-in-ease-out animation curve, allowing for smooth transitions between two values over time.

Parameters

  • timeStart (float): The start time for the ease curve.

  • valueStart (float): The start value for the ease curve.

  • timeEnd (float): The end time for the ease curve.

  • valueEnd (float): The end value for the ease curve.

  • exponent (float): The exponent applied to the curve's output value. Default is 1f.

Usage

[EaseInOutCurve(0f, 0f, 1f, 100f, 2f)]
private float animationProgress;

MinMaxAttribute

Ensures a numeric value stays within a specified minimum and maximum range.

Parameters

  • min (float/int): The minimum allowable value.

  • max (float/int): The maximum allowable value.

Usage

[MinMax(0, 100)]
private int health;

MinAttribute

Ensures a numeric value does not fall below a specified minimum.

Parameters

  • min (float/int): The minimum allowable value.

Usage

[Min(0)]
private float minimumDistance;

MaxAttribute

Ensures a numeric value does not exceed a specified maximum.

Parameters

  • max (float/int): The maximum allowable value.

Usage

[Max(100)]
private float maximumDistance;

Last updated