For the complete documentation index, see llms.txt. This page is also available as Markdown.

11. Advanced: immutable profile table and state machine

Difficulty: Advanced

immutable-profile-table-and-state-machine.gpc
enum {
    PROFILE_SOFT,
    PROFILE_MEDIUM,
    PROFILE_HARD,
    PROFILE_COUNT
};

const int16 GAIN_PERCENT[] = { 75, 100, 125 };

int profile;
int raw_x;
int final_x;

main {
    if(event_press(PS5_TRIANGLE)) {
        profile = profile + 1;
        if(profile >= PROFILE_COUNT) profile = PROFILE_SOFT;
    }

    raw_x = get_ival(PS5_RX);
    final_x = (raw_x * GAIN_PERCENT[profile]) / 100;

    if(final_x < -100) final_x = -100;
    if(final_x > 100) final_x = 100;

    set_val(PS5_RX, final_x);
    set_val(TRACE_1, profile);
    set_val(TRACE_2, GAIN_PERCENT[profile]);
}

What it teaches: enum states, compiler-managed table, bounded index, fixed-point scaling.

Last updated