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

set

Category

HashMap / Array / Shared

Arity

Binary<target> set [key|index, value].

<hashmap> set [key, value]
<array> set [index, value]
<shared> set [nil, value]    // write shared value

Description

Multi-purpose assignment command. Packs parameters into an array on the right side:

  • HashMap: _map set [key, value] — stores a key-value pair. Creates key if not present, overwrites if exists.
  • Array: _arr set [index, value] — sets element at index. Index must be within bounds.
  • Shared: _shared set [nil, value] — writes a value to a shared (atomic) variable. Not atomic — prefer compareSwap for concurrent updates.

How It Works

The host dispatches based on the left operand type, unpacks the right-side array, and performs the appropriate write operation.

Usage

HashMap set

_map = createHashMap;
_map set ["health", 100];
_map set ["name", "player"];
_map set ["health", 75];       // overwrite existing key

Array set

_arr = [1, 2, 3, 4];
_arr set [0, 99];              // _arr is [99, 2, 3, 4]
_arr set [3, 88];              // _arr is [99, 2, 3, 88]

// Out of bounds → error
_arr set [99, 0];              // ERROR — index out of range

Shared set

shared _flag = false;
_flag set [nil, true];         // set shared value (not atomic!)
// Better for concurrent updates:
_flag compareSwap [false, true];

Bulk hashmap population

_config = createHashMap;
{
    _x params ["_key", "_val"];
    _config set [_key, _val];
} forEach [
    ["volume", 0.8],
    ["music", true],
    ["difficulty", "normal"]
];

Thread Safety

  • HashMap set: Not thread-safe. HashMap must be owned by current scheduler.
  • Array set: Not thread-safe. Array must be owned by current scheduler.
  • Shared set: Not atomic. Use compareSwap for concurrent updates.

See Also

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