> For the complete documentation index, see [llms.txt](https://guide.cronuszen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://guide.cronuszen.com/gpcscripting/gpc-script-guide/advanced-architecture/state-machines.md).

# State machines

Use enums for named states and one transition owner:

{% code title="state-machine.gpc" %}

```cpp
enum {
    STATE_IDLE,
    STATE_ARMED,
    STATE_ACTIVE,
    STATE_COOLDOWN
};

int state;

main {
    switch(state) {
        case STATE_IDLE: {
            if(event_press(PS5_CROSS)) state = STATE_ARMED;
            break;
        }
        case STATE_ARMED: {
            if(get_ival(PS5_R2)) state = STATE_ACTIVE;
            if(event_press(PS5_CIRCLE)) state = STATE_IDLE;
            break;
        }
        case STATE_ACTIVE: {
            if(!get_ival(PS5_R2)) state = STATE_COOLDOWN;
            break;
        }
        case STATE_COOLDOWN: {
            if(get_ptime(PS5_R2) > 250) state = STATE_IDLE;
            break;
        }
        default: {
            state = STATE_IDLE;
            break;
        }
    }
}
```

{% endcode %}

Avoid several independent `if` blocks that can all transition the same state in one cycle. Define precedence for simultaneous events.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://guide.cronuszen.com/gpcscripting/gpc-script-guide/advanced-architecture/state-machines.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
