Skip to main content

Class BaseVariableReference<T, K>

A generic reference wrapper that allows selecting between a constant value or a reference to a scriptable variable.

public abstract class BaseVariableReference<T, K> where T : BaseScriptableVariable<K>

Type Parameters​

NameTypeDescription
TThe type of scriptable variable, inheriting from BaseScriptableVariable.
KThe value type stored and accessed.

Inheritance​

Examples​

// Declare a FloatReference field — the Inspector shows a toggle between
// a hard-coded constant and a FloatVariable asset:
[SerializeField] private FloatReference maxSpeed;

// At runtime, Value resolves transparently to whichever mode is active:
float speed = maxSpeed.Value;

// The implicit conversion operator allows direct use in arithmetic
// without an explicit .Value call at every use site:
float half = maxSpeed / 2f;

Remarks​

This pattern exists instead of a plain [SerializeField] value because scriptable variables are shared assets: multiple systems can read the same BaseScriptableVariable without holding a direct reference to each other. This is critical for the metric pipeline, where a calculator writes a score to a FloatVariable and the feedback UI reads that same asset independently — no direct dependency between the two systems is needed.

UseConstant defaults to true as a safe authoring default: a field that has not yet been wired to a variable asset will return the designer-supplied constant rather than silently returning zero from a null variable.

Constructors​

BaseVariableReference()​

Initializes a new instance of the BaseVariableReference class.

public BaseVariableReference()

BaseVariableReference(K)​

Initializes a new instance of the BaseVariableReference class using a constant value.

public BaseVariableReference(K value)

Parameters​

NameTypeDescription
valueKThe constant value to assign.

Fields​

ConstantValue​

The constant value to use when UseConstant is set to true.

public K ConstantValue

Field Value​

K

UseConstant​

Determines whether to use the constant value or the referenced scriptable variable. Defaults to true so that an unconfigured reference returns a usable constant rather than falling through to a null variable.

public bool UseConstant

Field Value​

bool

Variable​

The scriptable variable to use when UseConstant is set to false.

public T Variable

Field Value​

T

Properties​

Value​

Gets the current value of the reference, either from the constant or the scriptable variable.

public K Value { get; }

Property Value​

K

Remarks​

When UseConstant is false and Variable is null the property silently returns default(K) (typically zero for numeric types). This is a potential silent failure: callers will receive a plausible-looking zero rather than an exception, which can mask a missing asset assignment in the Inspector. Consider adding an assertion or warning log in the null branch if silent zero is unacceptable for a given use case.

Operators​

implicit operator K(BaseVariableReference<T, K>)​

Implicit conversion from BaseVariableReference to its value type K.

public static implicit operator K(BaseVariableReference<T, K> reference)

Parameters​

NameTypeDescription
referenceBaseVariableReference<T, K>The reference to convert.

Returns​

K

Remarks​

The implicit operator exists so that BaseVariableReference fields can be used directly in arithmetic and comparisons without an explicit .Value call at every use site (e.g., float x = myRef + 1f rather than float x = myRef.Value + 1f). This is the primary ergonomic benefit of the wrapper: callers interact with it as if it were a plain value of type K, while the indirection to either the constant or the shared scriptable asset remains transparent.