# Persistent variables source

> Supply named variables to a Smart String through groups of serialized assets, without passing arguments.

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 [`Smart.Format`](/engine/6000.7/script-reference/unity/smartstrings/smart/format.md). Values live in a [`VariablesGroupAsset`](/engine/6000.7/script-reference/unity/smartstrings/persistentvariables/variablesgroupasset.md), a [ScriptableObject](https://docs.unity3d.com/ScriptReference/ScriptableObject.html) 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:

```text
{group-name.variable-name}
{group-name.nested-group-name.variable-name}
```

## Variables group asset

A `VariablesGroupAsset` contains multiple persistent variables and nested groups. Create one from the Assets menu: **Assets > Create > Smart Strings > Variables Group**.

Add variables in script with the `Add` method. Register the source with `Smart.Default.AddExtensions(...)` or on a specific [`SmartFormatter`](/engine/6000.7/script-reference/unity/smartstrings/smartformatter.md).

The 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](/engine/6000.7/manual/scripting/smart-strings/formatters/choose-formatter.md) to it.

The following example shows how to create and use named persistent variables:

```cs
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 {global.my-float}`                                      | My float value is 1.23                 |
| `{global.my-string} of using variables.`                                   | This is an example of using variables. |
| `The door is {global.my-bool:Open\|Closed}.`                               | The door is Open.                      |
| `This is an integer {global.int-variable} as hex {global.int-variable:X}.` | This is an integer 255 as hex FF.      |
| `Values can be nested {global.nested-group.player-name}`                   | 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                                                                                  |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------- |
| [`BoolVariable`](/engine/6000.7/script-reference/unity/smartstrings/persistentvariables/boolvariable.md)                                                                                                                 | A single `bool` value.                                                                       |
| [`SByteVariable`](/engine/6000.7/script-reference/unity/smartstrings/persistentvariables/sbytevariable.md), [`ByteVariable`](/engine/6000.7/script-reference/unity/smartstrings/persistentvariables/bytevariable.md)     | A single signed or unsigned byte value.                                                      |
| [`ShortVariable`](/engine/6000.7/script-reference/unity/smartstrings/persistentvariables/shortvariable.md), [`UShortVariable`](/engine/6000.7/script-reference/unity/smartstrings/persistentvariables/ushortvariable.md) | A single signed or unsigned short value.                                                     |
| [`IntVariable`](/engine/6000.7/script-reference/unity/smartstrings/persistentvariables/intvariable.md), [`UIntVariable`](/engine/6000.7/script-reference/unity/smartstrings/persistentvariables/uintvariable.md)         | A single signed or unsigned int value.                                                       |
| [`LongVariable`](/engine/6000.7/script-reference/unity/smartstrings/persistentvariables/longvariable.md), [`ULongVariable`](/engine/6000.7/script-reference/unity/smartstrings/persistentvariables/ulongvariable.md)     | A single signed or unsigned long value.                                                      |
| [`FloatVariable`](/engine/6000.7/script-reference/unity/smartstrings/persistentvariables/floatvariable.md), [`DoubleVariable`](/engine/6000.7/script-reference/unity/smartstrings/persistentvariables/doublevariable.md) | A single float or double value.                                                              |
| [`StringVariable`](/engine/6000.7/script-reference/unity/smartstrings/persistentvariables/stringvariable.md)                                                                                                             | A single `string` value.                                                                     |
| [`ObjectVariable`](/engine/6000.7/script-reference/unity/smartstrings/persistentvariables/objectvariable.md)                                                                                                             | A reference to a UnityEngine [Object](https://docs.unity3d.com/ScriptReference/Object.html). |
| [`NestedVariablesGroup`](/engine/6000.7/script-reference/unity/smartstrings/persistentvariables/nestedvariablesgroup.md)                                                                                                 | A reference to another `VariablesGroupAsset`, which allows variables to nest.                |

## Triggering updates

Each variable derives from [`Variable<T>`](/engine/6000.7/script-reference/unity/smartstrings/persistentvariables/variable1.md) and raises a `ValueChanged` event when its `Value` changes. Variables that implement [`IVariableValueChanged`](/engine/6000.7/script-reference/unity/smartstrings/persistentvariables/ivariablevaluechanged.md) 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:

```cs
using (PersistentVariablesSource.UpdateScope())
            {
                floatVariable.Value = 2.5f;
                stringVariable.Value = "Updated";
            }
```

## Custom variables

A custom variable must be `[Serializable]` and implement [`IVariable`](/engine/6000.7/script-reference/unity/smartstrings/persistentvariables/ivariable.md). Return the value from `GetSourceValue`, which other sources and formatters then process. Implement `IVariableValueChanged` to support automatic updates.

```cs
[Serializable]
    public class DateTimeVariable : IVariable
    {
        public object GetSourceValue(ISelectorInfo selector) => DateTime.Now;
    }
```

## Additional resources

* [Create a custom source](/engine/6000.7/manual/scripting/smart-strings/sources/create-a-custom-source.md)
* [Choose formatter](/engine/6000.7/manual/scripting/smart-strings/formatters/choose-formatter.md)
* [Smart Strings](/engine/6000.7/manual/scripting/smart-strings.md)
