rust-samp

counter — stateful plugin

Plugin with persistent state, a hand-written impl SampPlugin, and the unified server tick. Larger surface than hello without the extra dependencies of advanced.

What it demonstrates

Natives

native Counter_Increment();                  // returns new value, or -1 if already at max
native Counter_Decrement();                  // returns new value, or -1 if already 0
native Counter_Reset();                      // returns the value that was discarded
native Counter_Get(&out);                    // writes the current value into `out`
native Counter_SetMax(max);                  // sets the cap; clamps current value if needed
native bool:Counter_IsAtMax();               // true when count >= max

Initial state: count = 0, max = 100, ticks = 0.

Every ~5 seconds (1000 ticks × ~5 ms) the plugin logs the current tick count and counter value at info level.

Pawn usage

public OnGameModeInit()
{
    Counter_SetMax(10);

    for (new i = 0; i < 5; i++) {
        new value = Counter_Increment();
        printf("count = %d", value);
    }

    new current;
    Counter_Get(current);
    printf("current = %d, at max = %d", current, Counter_IsAtMax());

    Counter_Reset();
    return 1;
}

Build

cargo build --release --target i686-unknown-linux-gnu -p counter

Output: target/i686-unknown-linux-gnu/release/libcounter.so.

Source

See src/lib.rs.