> 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/examples-and-recipes/8.-advanced-versioned-packed-spvar.md).

# 8. Advanced: versioned packed SPVAR

{% hint style="info" %}
**Difficulty:** Advanced
{% endhint %}

{% code title="versioned-packed-spvar.gpc" lineNumbers="true" %}

```cpp
define SAVE_MAGIC = 0x5A31;
define SAVE_VERSION = 1;

define MODE_SHIFT = 0;
define MODE_MASK = 0x00000003;
define ENABLE_SHIFT = 2;
define ENABLE_MASK = 0x00000004;
define STRENGTH_SHIFT = 3;
define STRENGTH_MASK = 0x000003F8;

int mode;
int enabled;
int strength = 25;
int packed;
int dirty;

init {
    if(get_pvar(SPVAR_1, 0, INT32_MAX, 0) == SAVE_MAGIC &&
       get_pvar(SPVAR_2, 0, 100, 0) == SAVE_VERSION) {
        packed = get_pvar(SPVAR_3, 0, 1023, 0);
        unpack_settings(packed);
    }
}

main {
    if(event_press(PS5_TRIANGLE)) {
        mode = mode + 1;
        if(mode > 3) mode = 0;
        dirty = TRUE;
    }

    if(event_press(PS5_CROSS)) {
        enabled = !enabled;
        dirty = TRUE;
    }

    if(event_press(PS5_UP)) {
        strength = strength + 1;
        if(strength > 100) strength = 100;
        dirty = TRUE;
    }

    if(dirty && event_press(PS5_OPTIONS)) {
        packed = pack_settings();
        set_pvar(SPVAR_1, SAVE_MAGIC);
        set_pvar(SPVAR_2, SAVE_VERSION);
        set_pvar(SPVAR_3, packed);
        dirty = FALSE;
    }

    set_val(TRACE_1, mode);
    set_val(TRACE_2, enabled);
    set_val(TRACE_3, strength);
}

function pack_settings() {
    int_dummy = 0;
    int_dummy = int_dummy | ((mode << MODE_SHIFT) & MODE_MASK);
    int_dummy = int_dummy | ((enabled << ENABLE_SHIFT) & ENABLE_MASK);
    int_dummy = int_dummy | ((strength << STRENGTH_SHIFT) & STRENGTH_MASK);
    return int_dummy;
}

function unpack_settings(value) {
    mode = (value & MODE_MASK) >> MODE_SHIFT;
    enabled = (value & ENABLE_MASK) >> ENABLE_SHIFT;
    strength = (value & STRENGTH_MASK) >> STRENGTH_SHIFT;

    if(mode < 0 || mode > 3) mode = 0;
    if(strength < 0 || strength > 100) strength = 25;
}

int int_dummy;
```

{% endcode %}

GPC permits top-level declarations later in the file, but production style should move `int_dummy` beside the other globals. It is shown here to reinforce that "top-level" matters more than textual position.

***

**What it teaches:** magic/version, manual bit layout, identical pack/unpack schema, post-load validation.


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://guide.cronuszen.com/gpcscripting/gpc-script-guide/examples-and-recipes/8.-advanced-versioned-packed-spvar.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
