# Structure

This chapter describes the actions available for structuring your scripts to make them more generic and reusable.

## Execute script <a href="#execute-script" id="execute-script"></a>

Executes another [script](https://modeltrainscript.gitbook.io/docs/manual/automation/scripts) inline as part of the current script. The called script shares the same context — including the currently selected vehicle, active flags, and variables.

You can execute a script in two ways:

* **Specific script** — Select a specific script to run.
* **Random by tag** — Select a tag, and Model Train Script randomly picks one of the available scripts with that tag. Only scripts that are not disabled and whose execute conditions are met are considered.

| Setting                       | Description                                                                                                                                                                           |
| ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Wait for available script** | When enabled and no matching script is currently available (e.g., all are running or their conditions are not met), the action waits until one becomes available instead of skipping. |

{% hint style="info" %}
Random execution by tag is a powerful way to add variety. For example, tag several station approach scripts with "station-arrival" and randomly pick one each time a train arrives — one might include a station announcement, another might not.
{% endhint %}

## Set flags <a href="#set-flags" id="set-flags"></a>

Sets [flags](https://modeltrainscript.gitbook.io/docs/manual/automation/flags) at the **script level**. Script-level flags are active only for the current script run and are combined with global and vehicle flags when evaluating conditions.

The action **replaces** all previously set script-level flags — it does not add to them. To clear all script flags, use this action with no flags selected.

{% hint style="info" %}
Flags set at the script level cannot override global or vehicle flags. If a flag is active at any level (global, vehicle, or script), it is considered active.
{% endhint %}

## Set variable <a href="#set-variable" id="set-variable"></a>

Modifies a [variable](https://modeltrainscript.gitbook.io/docs/manual/automation/variables) during script execution. Variables store numeric values between 0 and 255.

| Operation         | Description                                   |
| ----------------- | --------------------------------------------- |
| **Set to value**  | Set the variable to a specific value (0–255). |
| **Increase by 1** | Increment the variable by 1. Clamps at 255.   |
| **Decrease by 1** | Decrement the variable by 1. Clamps at 0.     |

Changing a variable can trigger other scripts that have a variable execution event configured.

## Show notification <a href="#show-notification" id="show-notification"></a>

Displays an in-app notification banner during script execution.

| Setting        | Description                                                                                                                  |
| -------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| **Type**       | The visual style: **info**, **warning**, or **error**.                                                                       |
| **Title**      | The notification text to display.                                                                                            |
| **Auto close** | When enabled, the notification dismisses automatically after a short time. When disabled, the user must dismiss it manually. |

The notification automatically includes the name of the executing script as context. This action is useful for alerting the user about situations that require attention — for example, when a station track is fully occupied and a train cannot enter.
