Skip to main content

Class BaseScriptableVariable<T>

Generic ScriptableObject variable whose runtime value is read from and written to an SQLite database, so it persists across play sessions and scene loads.

public class BaseScriptableVariable<T> : BaseScriptableVariable

Type Parameters​

NameTypeDescription
TPrimitive or serialisable type stored in the database column.

Inheritance​

Inherited Members​

Examples​

// Reference a FloatVariable asset from a component (assign the asset in the Inspector):
[SerializeField] private FloatVariable maxSpeed;

void Start()
{
// Read the persisted or initial value:
float speed = maxSpeed.RuntimeValue;

// Write a new value (automatically persisted to SQLite):
maxSpeed.RuntimeValue = 15f;
}

Remarks​

Unity's default ScriptableObject pattern stores a single value in the asset itself. That works for authoring-time defaults but loses any runtime changes when the domain reloads (leaving Play Mode, restarting the build). This class bypasses that problem by treating the SQLite database as the authoritative store: every read goes through DatabaseConnector and every write is immediately committed.

The InitialValue field set in the Inspector acts as the fallback when no database row exists yet (first run, reset). Calling ResetRunTimeValue deletes the row so the next read returns InitialValue again.

ISerializationCallbackReceiver hooks reset the runtime value on both serialize and deserialize so that the Inspector always shows the authored default rather than a stale runtime value from a previous play session.

Fields​

InitialValue​

The value shown and edited in the Inspector, used as the database fallback when no runtime row exists.

[SerializeField]
protected T InitialValue

Field Value​

T

Properties​

RuntimeValue​

Database-backed runtime value; reads from SQLite on get and immediately commits on set. Falls back to InitialValue when no database row exists.

public virtual T RuntimeValue { get; set; }

Property Value​

T

Remarks​

Every get issues a ReadByID query. For performance-sensitive hot paths (e.g., per-frame reads) consider caching the value locally and writing only on mutation.

Methods​

GetValue()​

Returns the current runtime value as an untyped object, allowing tools that don't know the concrete variable type to read it.

public override object GetValue()

Returns​

object — The current runtime value, or the initial value if no runtime value has been set.

IsValid()​

Returns true when GetValue would return a usable non-null value.

public override bool IsValid()

Returns​

bool — true if the runtime value is non-null; false otherwise.

Remarks​

The base implementation always returns false; the generic subclass overrides this to check whether the SQLite query returned a non-null result.

OnAfterDeserialize()​

Clears the runtime value when Unity deserialises the asset (e.g., after exiting Play Mode), so the Inspector displays the authored InitialValue rather than a stale runtime result.

public void OnAfterDeserialize()

OnBeforeSerialize()​

Clears the runtime value before Unity serialises the asset, preventing runtime database state from being baked into the asset on disk.

public void OnBeforeSerialize()

ResetRunTimeValue()​

Removes the stored runtime value so the next RuntimeValue read returns InitialValue again.

public virtual void ResetRunTimeValue()

Remarks​

The base implementation is intentionally empty; concrete subclasses override this to delete or invalidate their database row.