Arma 3 Scripting

count

  • Arma 3 v0.50
  • First in Operation Flashpoint v1.00

Command · also in Strings, Config, HashMap · Markdown · JSON · original wiki page

Description

Can be used to count:

Note

If Unicode support is desired, see forceUnicode.

Syntax

Syntax 1

count value

Parameters:

Returns: Number

Syntax 2

condition count array

Parameters:

Returns: Number

Examples

Example 1

count [0, 0, 1, 2]; // returns 4
count units group player; // returns number of units in player group

Example 2

private _cnt = { _x == 4 } count [1, 9, 8, 3, 4, 4, 4, 5, 6]; // returns 3
_cnt = { alive _x } count allUnits; // returns the number of alive units

Example 3

private _cnt = count (configFile >> "CfgVehicles");

Example 4

hint str count "japa is the man!"; // 16

Example 5

hint format ["There are %1 elements in the provided hashmap", count _myHashMap];

See also

apply · select · in · find · countFriendly · countEnemy · countUnknown · countSide · countType · findIf · forceUnicode

Community notes

Hardrock

Notes from before the conversion:

Use this to calculate how many "M16" mags a soldier has left.

{ _x == "M16" } count magazines soldier1;

Take care when using count to determine how many units are left alive in a group: count units group player or count units groupname Will return the number of units the leader of the group thinks are alive. If some units have been killed out of sight of other members of the group then it may take sometime for this to be the actual numbers in the group. To determine exactly how many units are really alive in a group use:

{ alive _x } count units group player;

or

{ alive _x } count units groupname;

Heeeere's Johnny!

count can be (ab)used for a very fast and simple check if at least one element in an array fulfills a certain condition:

if ({ if (/* _x fulfills condition */) exitWith {1}; false } count _array isEqualTo 1) then
{
    // do whatever here
};

This code will exit the count loop as soon as it finds an element fulfilling the condition, leaving the count with the value of 1, hence make the larger if-condition be true.

If no array element fulfills the condition, the count will be 0 and the if-condition will be false.

Killzone_Kid

Quit loop at first fulfilled condition (same as above but faster):

{
    if (_x == 4) exitWith {
        // do something when we reach 4
    }
} count [1,2,3,4,5,6];

Heeeere's Johnny!

Using exitWith inside a count loop will overwrite the default functionality and make count return whatever the exitWith returns:

_result = {
    if (_x isEqualTo 3) exitWith { "Hello" }
} count [1,2,3,4,5];
// _result = "Hello"

Ebay

With the alternative syntax each iteration should result in an interior return of bool or nothing. Example:

createDialog "RscFunctionsViewer";
{ lbAdd [292901, _x]; } count ["first", "second", "third"];

lbAdd returns a number, so this throws "Error Type Number, expected Bool". Tested in A2OA 1.63.131129