Persistent variables source
Supply named variables to a Smart String through groups of serialized assets, without passing arguments.
Read time 3 minutesLast updated 6 days ago
Supply named variables to a Smart String through groups of serialized assets, without passing arguments.
The Persistent variables source lets you reference named values inside a placeholder instead of passing them as arguments to . Values live in a , a ScriptableObject that holds named variables. You register one or more groups with the source, each under a unique name, and reach a variable with dot notation:
Smart.FormatVariablesGroupAsset{group-name.variable-name}{group-name.nested-group-name.variable-name}
Variables group asset
A contains multiple persistent variables and nested groups. Create one from the Assets menu: Assets > Create > Smart Strings > Variables Group.
VariablesGroupAssetAdd variables in script with the method. Register the source with or on a specific .
AddSmart.Default.AddExtensions(...)SmartFormatterThe source first checks the current value for a matching variable group, then any group registered on the source. The value a variable returns is handled by the remaining sources and formatters, so you can apply formatters such as Choose to it.
The following example shows how to create and use named persistent variables:
var group = ScriptableObject.CreateInstance<VariablesGroupAsset>(); group.Add("my-float", new FloatVariable { Value = 1.23f }); group.Add("my-string", new StringVariable { Value = "This is an example" }); group.Add("my-bool", new BoolVariable { Value = true }); var source = new PersistentVariablesSource(); source.Add("global", group); var smart = Smart.CreateDefaultSmartFormat(); smart.AddExtensions(source); // "My float value is 1.23" smart.Format("My float value is {global.my-float}", null);
Group names and variable names must be unique and must not contain whitespace; any whitespace is replaced with when a name is added.
-Smart String | Result |
|---|---|
| My float value is 1.23 |
| This is an example of using variables. |
| The door is Open. |
| This is an integer 255 as hex FF. |
| Values can be nested Player 1 |
Variable types
The following variable types are available by default. Each holds a single value of its type:
Type | Description |
|---|---|
| A single |
| A single signed or unsigned byte value. |
| A single signed or unsigned short value. |
| A single signed or unsigned int value. |
| A single signed or unsigned long value. |
| A single float or double value. |
| A single |
| A reference to a UnityEngine Object. |
| A reference to another |
Triggering updates
Each variable derives from and raises a event when its changes. Variables that implement can drive automatic updates in callers that subscribe to that event. To change several variables and notify once at the end, wrap the changes in an update scope:
Variable<T>ValueChangedValueIVariableValueChangedusing (PersistentVariablesSource.UpdateScope()) { floatVariable.Value = 2.5f; stringVariable.Value = "Updated"; }
Custom variables
A custom variable must be and implement . Return the value from , which other sources and formatters then process. Implement to support automatic updates.
[Serializable]IVariableGetSourceValueIVariableValueChanged[Serializable] public class DateTimeVariable : IVariable { public object GetSourceValue(ISelectorInfo selector) => DateTime.Now; }