18. Create a variable during play
Most puzzle facts are easiest to define in Variable Designer before the game starts. That gives them a known type, a starting value and an entry in reference selectors. Create Variable is useful when a script or action must introduce a key during play.
| Create Variable field | Example |
|---|---|
| Variable ID | runtime_note |
| Type | string |
| Initial Value | "First value" in the action-list value field |
Create Variable only creates the key if it does not already exist. Running it again with another initial value leaves the existing current value alone. Use Set Variable when you intend to replace that value.
await CreateVariable({
"name": "runtime_note",
"variableType": "string",
"value": "First value"
})await SetVariable({
"name": "runtime_note", "value": "Replacement"
})What it does and does not add#
The new key belongs to the running game. It does not create a new authoring record in Variable Designer, so do not expect it to appear there after the test. It can be included in a saved game because saves keep the current runtime variables.
The Type choice is not a universal conversion step. Supply the correct kind of Initial Value yourself. In code, use true for a Boolean, a number for a Number and an actual Array for an Array.
| During the test | Result |
|---|---|
| Create runtime_note = "First value" | The missing key is created. |
| Create it again = "Second value" | It remains "First value". |
| Set it = "Replacement" | It becomes "Replacement". |
If a variable is central to the game and other designers should select it, define it in Variable Designer. Runtime-created keys are better kept for deliberate dynamic data than for working around a misspelt ID.
