> 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/gpc-developer-guide/main-section.md).

# Main Section

The `main` section is the heart and soul of any GPC script. It is run in a constant loop from the top down and any code executed during run time can be traced back to it. Therefore, the `main` section is mandatory and a GPC script is not valid without one.

Just as with any nested code, the main section start and finish points are denoted with a `{` or `}` respectively, like so:

```gpc
main {
    // main start
}   // main end
```

When the `main` section reaches the endpoint, the output report to the console is created, any remaps are evaluated and the report is sent to the console. The `main` section is then restarted from the beginning.

The code within the `main` section is executed in the order it is written. It is important to remember this when building your own GPC scripts as the output report to the console is created at the end of the `main` section, so it is possible to create code that would cancel each other out and result in a different output to the console than you expected.

For example:

```gpc
int hold_lt;

main {
    if(get_val(XB1_RT)) {
        hold_lt = TRUE;
    }
    hold_lt = FALSE;

    if(hold_lt) {
        set_val(XB1_LT, 100);
    }
}
```

As you can see above, the variable `hold_lt` is set to `TRUE` when the RT/R2 button is pressed but is immediately set to `FALSE` in the next line of code. Therefore the `if(hold_lt)` statement will always be false and the code nested within the `if` statement will never be executed.

However, if we were to move the code around, then we would get the expected output:

```gpc
int hold_lt;

main {
    hold_lt = FALSE;

    if(get_val(XB1_RT)) {
        hold_lt = TRUE;
    }

    if(hold_lt) {
        set_val(XB1_LT, 100);
    }
}
```

If RT/R2 is pressed then `hold_lt` is set to `TRUE` after it has been set to `FALSE`, the `if(hold_lt)` statement will see that it is true and the nested code will be run.

If RT/R2 isn't being pressed then `hold_lt` is not set to `TRUE` after it has been set to `FALSE` and the nested code is not run.

So, as you can see in the above examples, it is important to remember that code is executed in the order it is written and simply moving the placement of a line can have a significant effect on the output to the console.


---

# 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:

```
GET https://guide.cronuszen.com/gpcscripting/gpc-script-guide/gpc-developer-guide/main-section.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.
