# Variables

Variables allow you to store numeric values (0–255) that can be used across your scripts. They are useful for counting events, tracking state, or creating more complex automation logic.

A typical use case is to count how many times a train has passed a sensor, or to coordinate behaviour between multiple scripts.

## Creating variables

Variables can be created from the [profile](/docs/manual/profile.md) screen under the automation section. Each variable has a name that you define and stores a single numeric value between 0 and 255.

## Using variables in scripts

Variables can be used in three ways within your [scripts](/docs/manual/automation/scripts.md):

### Actions

The [set variable](/docs/manual/automation/scripts/action/structure.md#set-variable) action allows you to modify a variable during script execution. You can choose from three operations:

* **Set to value** — Set the variable to a specific value (0–255).
* **Increase by 1** — Increment the variable by 1 (maximum 255).
* **Decrease by 1** — Decrement the variable by 1 (minimum 0).

### Conditions

You can use a variable as a condition in your scripts, sections, or actions. The condition checks whether a variable **equals** a specific value. This allows you to create branching logic — for example, only executing a section when a counter has reached a certain value.

### Execution events

Variables can be used as execution events to trigger a script when a variable reaches a specific value. This is useful for coordinating between scripts — one script can set a variable and another script can be triggered by that change.

## Layout display

Variables can be placed on your [layout](/docs/manual/layout.md) as an item. This displays the current value of the variable directly on the layout, making it easy to monitor variable state during automation.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://modeltrainscript.gitbook.io/docs/manual/automation/variables.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
