SmartBuilder > Help Documentation > Actions

Using Variables

A variable is a named value that can be changed (varies). For example, you could create a variable called "First_Name" and then set the variable to whatever the learner types into a text input object:

Set Variable to Next from Input Text Object

Then, on a later page, you could retrieve the variable and display it back to the learner. For example, when the page loads, Text 1 will display something like "Hello, Name" (where Name is whatever the learner entered earlier).

Set text to Appended Variable

Create a variable

You may create variables at any time through the "set" block. These are traditionally called "global variables" meaning that they can be set and retrieved from any page in your lesson.

Set Global Variable to 0

By default, each lesson contains two variables "Variable1" and "List1"

Two Default Variables

You can rename these default variables, or create new variables. For example, to create a new variable called "Count":

  1. From the set variable block, click on the variable dropdown and select Create New Variable.
    Create New Variable
  2. Type a name for the variable and click Create.
    New Variable Name
  3. The set block will be using the new variable.
    Set New Variable to 0

Set a variable

You can set a variable to specific value. For example:

Set Variable to 15

Or, you can modify a variable using add, subtract, append or set. For example, this will add 1 to the value of Count.

Add 1 to a variable

Get a variable

Use the get variable block to retrieve the value of a variable. For example, to make the Text 1 object display the Count variable:

  1. Add a Set Text to Text Block to block to your canvas.
  2. From the Variable action category, select the get variable block.
    Variable Actions: Get Variable
  3. Snap the get variable block into your statement.
    Set Text Object's text to Variable
  4. Click on the variable dropdown and choose the desired variable
    Select your new Varibale
    Select your new Varibale

Debugging variables

The debug feature is a powerful way to quickly analyze how variables are changing at runtime.

preview debug button

Using set path and get path

For most use cases, you can use the methods above to set or get variable values. This reduces the chance of typos, and makes searching for variables easier.

For advanced use cases, where the specific variable that is being targeted might vary, it is possible to use the set path and get path blocks.

set path

This method also makes some changes to list variables easier:

set path advanced

The primary use case is for getting and setting values with map variables, which previously required many blocks to get or set values.

get path value

Setting specific map values can be done in a few different ways:

set map and set path

In very advanced use cases, you can also use the curly bracket notation inside of get/set path blocks or inside text blocks to refer to a variable value.

curly brackets inside get set path blocks

Or use a join block inside of a get path block to reference a dynamic value.

get path with join block

Using variables in text

The easiest way to display a variable is to use the name of the variable inside curly brackets within a text object. For example, if you store the learner’s first name into a variable called "user_name", then "Welcome {{user_name}}, to this lesson..." would become "Welcome Fiona, to this lesson…" once you Preview or Publish the lesson.

If the variable value updates at any point, you do not need to use any actions to update the displayed text, it will automatically update.

This same method can also be used for more complex variable structures, such as lists and maps. For a list, use square brackets to supply the index of the value that you want. For maps, you can access data stored within the structure using dots.

Examples:

Welcome, {{user_name}}, to this lesson!
The third item in fruitList is {{fruitList[3]}}.
On Question 3, you got {{Quizzes_map.Quiz1.pg27.points_earned}} points.

curly brackets in text object