Use Script Variables

As a story developer, you can use script variables, which are useful for storing intermediate results repeatedly used in a script, for example.

About Script Variables

Script variables are reusable elements that store a value of a certain type, which exist throughout the runtime. The script variables you create in a story are available to itself only. You can define their values not only in the story, but also in its URL by adding a specific URL parameter.

For each script variable, you can define name, optional description, type and a default value, which is optional and depends on the variable type. A script variable can be defined as an array, which represents a set of values of the specified type. You can use the following primitive types:
  • string

    Represents text as a sequence of characters.

  • boolean

    Represents a logical value, either true or false.

  • number

    Represents a floating-point numerical value.

    Can be entered in ordinary format or scientific notation format such as 1e+20 or 1e20. If the number is longer than 21 digits, it will be automatically displayed in scientific notation format.

  • integer

    Represents a non-fractional numerical value.

    Scientific notation format isn't supported for entering values. However, at runrime if the number is longer than 21 digits, it'll be displayed in scientific notation.

In addition to the primitive types, you can select a wider variety of non-primitive types for the script variable, such as button, category, chart, clock, data source, and table.

Create a Script Variable

Procedure

  1. In the Scripting area of the Outline panel, choose Start of the navigation path Next navigation step End of the navigation path Script Variables to add a new script variable.

    You can see the variable under Script Variables, and Script Variable panel opens.

  2. In the panel, you can change the ID of the script variable. The default one is ScriptVariable_1.
  3. Optional: Enter the description for the script variable.
  4. Choose the type of the script variable and whether to set it as an array.
  5. Specify the default value depending on the type.
  6. Choose Done to close the panel.

    You can now use the variable in scripting within the story.

Define a Script Variable's Value via URL Parameter

You can initialize your story based on your needs with the script variable value applied, which is passed via URL parameter.

Procedure

  1. Choose the script variable you want to define, and go to the Scipt Variable panel.
  2. Select Expose variable via URL parameter.
  3. Optional: If you want the URL parameter to dynamically change with the script variable value in view mode, select Enable dynamic URL.
  4. Save the story with the changes, and choose View.
  5. If dynamic URL is enabled, the script variable parameter is automatically appended to the story URL.

    If not, in the URL of the story, start the parameter with p_, followed by the script variable name and value. For example, if you want to set the value of ScriptVariable_1 to 3.14, add the following parameter to the URL: p_ScriptVariable_1=3.14. Then, reload your story.

Results

When you open the story with the script variable URL parameter, the value defined in the URL is passed to the script variable. You can change the script variable value via URL.

If dynamic URL is enabled, any view time change to the script variable value automatically updates the related URL parameter. You can therefore get the latest story URL without reloading the story.

Use a Script Variable

You can use a script variable in your script as a local variable. For example, you change the value by assigning a new one and use this value by passing it as argument when calling a function.

If you write scripts in the story, you can insert the script variable you've created by selecting it from the value help in the script editor. You can activate the value help at any place in the script by pressing CTRL + Space .

If you need to use the script variable in calculation, you can type @ in the formula in Calculation Editor to insert it. All the available script variables will be automatically displayed in the dropdown list. Note that only global variables of string, integer and number types can be referenced in the calculation editor, while the array type isn't supported.

You can also define script variables as the source of dynamic text in a text widget.

Change or Delete a Script Variable

To change an existing scripting variable, select it in the Outline panel. You can see the Scipt Variable panel open again where you can also make the changes there.

To delete an existing scripting variable, choose beside the script variable in the Outline panel, and select Delete.