# set ## Category HashMap / Array / Shared ## Arity **Binary** — ` set [key|index, value]`. ``` set [key, value] set [index, value] 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](compareSwap.md) 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 ```sqf _map = createHashMap; _map set ["health", 100]; _map set ["name", "player"]; _map set ["health", 75]; // overwrite existing key ``` ### Array set ```sqf _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 ```sqf shared _flag = false; _flag set [nil, true]; // set shared value (not atomic!) // Better for concurrent updates: _flag compareSwap [false, true]; ``` ### Bulk hashmap population ```sqf _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](compareSwap.md) for concurrent updates. ## See Also - [get](get.md) — retrieve value - [pushBack](pushBack.md) — append to array - [compareSwap](compareSwap.md) — atomic CAS for shared - [createHashMap](createHashMap.md) — create empty hashmap