Skip to content
ffredyk edited this page Jul 23, 2026 · 1 revision

sub

Category

Shared (Atomic) / Arithmetic

Arity

Binary<shared|number> sub <number>.

<shared> sub <number>     // atomic decrement
<number> sub <number>     // regular subtraction

Description

Dual-purpose subtraction command:

  • On Shared variable: performs an atomic decrement. Thread-safe from any scheduler. Returns the new value.
  • On regular number: performs standard subtraction (same as -). Returns the difference.

How It Works

  • Shared: uses atomic CAS loop to decrement. Indivisible operation.
  • Number: delegates to standard - implementation.

Usage

Atomic decrement

shared _ammo = 30;
_ammo sub 1;                   // atomic decrement — 29
_ammo sub 5;                   // atomic subtract 5 — 24

Resource pool

shared _availableSlots = 10;
// Try to acquire a slot:
_availableSlots sub 1;
if (get _availableSlots < 0) then {
    _availableSlots add 1;     // give back — no slots available
    systemChat "No slots";
};

Regular subtraction

_result = 10 sub 3;            // 7 (same as 10 - 3)

vs - operator

shared _counter = 10;
_counter sub 1;                // ATOMIC — correct
_counter = get _counter - 1;   // NOT atomic — race condition!

Thread Safety

  • On Shared: Fully atomic. Thread-safe from any scheduler.
  • On Number: Standard computation. Thread-safe for value types.

See Also

  • add — atomic add
  • get — atomic read
  • compareSwap — atomic CAS
  • - — standard subtraction

SQ# Wiki

Home

Engine Docs

Migration

Commands

Value Constructors

Arithmetic

Comparison

Logic

Array

String

Math

Random

Type & Introspection

HashMap

Code Execution

Concurrency

Scheduler

Thread Safety

Error

Output

Time

Multiplayer

Compiler

Clone this wiki locally