-
Notifications
You must be signed in to change notification settings - Fork 0
forEach
ffredyk edited this page Jul 23, 2026
·
1 revision
Array
Binary — <array> forEach { code }.
<array> forEach <code>
Iterates over each element in the array, executing the given code block for each element. Inside the code block, magic variables provide access to the current element and index.
Inside the forEach code block:
| Variable | Value |
|---|---|
_x |
Current element |
_forEachIndex |
Zero-based index |
_this |
[element, index, array] (Arma SQF compat) |
The compiler inlines forEach into a loop over the array. For each iteration, it sets the magic variables and executes the code block. This is a compile-time construct — no runtime command lookup overhead.
[1, 2, 3] forEach {
systemChat f"Value: {_x}";
};_names = ["Alice", "Bob", "Charlie"];
_names forEach {
systemChat f"{_forEachIndex}: {_x}";
};_arr = [1, 2, 3];
_arr forEach {
_arr set [_forEachIndex, _x * 2];
};
// _arr is [2, 4, 6]_data forEach {
_this params ["_element", "_index", "_array"];
systemChat f"[{_index}] {_element}";
};_units = [unit1, unit2, unit3];
_alive = [];
_units forEach {
if (_x get "alive") then { _alive pushBack _x };
};_arr forEach {
if (_x == "target") exitWith {
systemChat "Found!";
};
};ReadOnly on the array (does not mutate). Code block may have side effects. Array must be accessible from current scheduler (frozen OK, mutable owned OK).
- Getting Started
- Language Guide
- Type System
- Control Flow
- Functions & Code
- Strings & Text
- Arrays & Collections
- Concurrency
- Promise System
- Thread Safety
- Host API & Embedding
- CLI Tool
- Bytecode Reference
- Multiplayer
- Syntax Sugar
- Optimization Guide
- Benchmarks
- count
- select
- pushBack
- append
- deleteAt
- deleteRange
- resize
- reverse
- sort
- find
- in
- forEach
- freeze
- thaw
- isFrozen
- currentScheduler
- clientOwner
- allSchedulers
- schedulerName
- schedulerExists
- schedulerBudget
- setSchedulerBudget
- fiberCount
- readyFiberCount
- waitingFiberCount
- schedulerLoad
- sendTo