> ## Documentation Index
> Fetch the complete documentation index at: https://docs.altoura.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Set Variable

> Set or update a variable's value during training execution

## Overview

The **Set Variable** action allows you to create, modify, or update variables throughout your training. Variables store data that persists across states and transitions, enabling you to track learner progress, record choices, and implement conditional logic.

## When to Use

* **Track scores or counts**: increment a score each time a learner completes a task
* **Record learner choices**: store which option the learner selected
* **Set flags**: mark milestones (e.g., `isComplete = true`, `hasAttempted = false`)
* **Compute derived values**: calculate new values based on existing variables using expressions
* **Enable conditional logic**: set variables that transitions or branching logic can check

## Parameters

| Parameter           | Type                  | Required | Description                                                                                                                                |
| ------------------- | --------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| Variable Name       | string                | Yes      | The name of the variable to create or update. If the variable doesn't exist, it will be created.                                           |
| Value or Expression | string/number/boolean | Yes      | The new value for the variable. Can be a literal value (e.g., `"completed"`, `42`, `true`) or an AXL expression that evaluates to a value. |

## Data Types

Variables can store:

* **string**: `"text"`, `"completed"`, `"user input"`
* **number**: `10`, `3.14`, `-5`
* **boolean**: `true`, `false`
* **object**: JSON-like structures
* **array**: lists of values

## Expression Examples

Use AXL expressions to compute or reference values dynamically:

```
score + 10              → adds 10 to the current score
attempts - 1            → subtracts 1 from attempts
"completed"             → literal string
true                    → literal boolean
score >= 80 ? "pass" : "fail"  → conditional: if score ≥ 80, set to "pass", else "fail"
learnerName + " passed" → concatenate strings
attempts <= 3           → boolean expression (true/false)
```

## Practical Example

**Scenario**: A training module where learners attempt a repair task. You want to track the number of attempts and set a completion flag when they succeed.

<Steps>
  <Step>
    In the **Attempt Failed** state's `onEntry`, add a Set Variable action:

    * Variable Name: `attempts`
    * Value or Expression: `attempts + 1`

    This increments the attempt counter each time the learner fails.
  </Step>

  <Step>
    In the **Repair Successful** state's `onEntry`, add two Set Variable actions:

    * Variable Name: `isComplete`
    * Value or Expression: `true`

    And:

    * Variable Name: `finalScore`
    * Value or Expression: `attempts <= 3 ? 100 : 50`

    This marks completion and awards points based on how many attempts were used.
  </Step>

  <Step>
    Later, use the `attempts` or `isComplete` variables in a transition condition to branch to different states (e.g., show a "Congratulations" screen only if `isComplete == true`).
  </Step>
</Steps>

<Tip>
  Variables are case-sensitive. Use consistent naming conventions (e.g., `camelCase`) to avoid typos and make your training logic easier to read and debug.
</Tip>

<Note>
  Variables set in one state persist across the entire training session. You can reference them in transitions, conditions, and other Set Variable actions later. There is no built-in "clear all variables" action — if you need to reset a variable, explicitly set it to its initial value.
</Note>
