findIf: Difference between revisions
Jump to navigation
Jump to search
Killzone Kid (talk | contribs) (note format) |
Lou Montana (talk | contribs) m (then → than) |
||
Line 1: | Line 1: | ||
{{Command|= | {{Command|Comments= | ||
____________________________________________________________________________________________ | ____________________________________________________________________________________________ | ||
| arma3 | | | arma3 |Game name= | ||
|1.82| | |1.82|Game version= | ||
____________________________________________________________________________________________ | ____________________________________________________________________________________________ | ||
| Searches for an element within array for which the code evaluates to true. Returns the | | Searches for an element within array for which the code evaluates to true. | ||
Returns the zero-based index on success or -1 if not found. | |||
Code on the right side of the command is evaluated for each element of the array, processed element can be referenced in code as [[_x]]. | |||
{{Informative | [[findIf]] terminates as soon as it finds an element that fulfills the condition, thus it is more efficient | {{Informative|[[findIf]] terminates as soon as it finds an element that fulfills the condition, thus it is more efficient than [[count]] in scenarios where you need to check for presence / absence of at least one element that evaluates to true.}} |Description= | ||
____________________________________________________________________________________________ | ____________________________________________________________________________________________ | ||
| array | | array [[findIf]] code |Syntax= | ||
|p1= array: [[Array]] - array to search in | | |p1= array: [[Array]] - array to search in |Parameter 1= | ||
|p2= code: [[Code]] - code defining an element to find, processed element can be referenced within the code as | |p2= code: [[Code]] - code defining an element to find, processed element can be referenced within the code as [[_x]] |Parameter 2= | ||
| [[Number]] - Zero-based position of the first array element for which the code evaluate to true, -1 if not found |Return value= | |||
____________________________________________________________________________________________ | ____________________________________________________________________________________________ | ||
|x1= <code>[unit1,unit2,unit3] [[findIf]] {[[ | |x1= <code>[unit1, unit2, unit3] [[findIf]] {[[not]] [[alive]] [[_x]]}; {{codecomment|// return index of the first dead unit)}}</code> |Example 1= | ||
|x2= <code>//two ways how to 'wait for all units to be dead': | |x2= <code>{{codecomment|// two ways how to 'wait for all units to be dead':}} | ||
[[waitUntil]] { [unit1,unit2,unit3] [[findIf]] {[[alive]] [[_x]]} | [[waitUntil]] { [unit1, unit2, unit3] [[findIf]] {[[alive]] [[_x]]} == -1 }; {{codecomment|// fast, terminates as soon as it finds a living unit}} | ||
[[waitUntil]] { {[[alive]] [[_x]]} [[count]] [unit1,unit2,unit3] | [[waitUntil]] { {[[alive]] [[_x]]} [[count]] [unit1, unit2, unit3] == 0 }; {{codecomment|// slow, always goes through all array elements}}</code> |Example 2= | ||
___________________________________________________________________________________________ | ___________________________________________________________________________________________ | ||
| [[find]] [[count]] | | | [[find]] [[count]] |See also= | ||
}} | }} | ||
Line 40: | Line 40: | ||
<h3 style="display:none">Bottom Section</h3> | <h3 style="display:none">Bottom Section</h3> | ||
[[Category:Scripting Commands| | [[Category:Scripting Commands|{{uc:{{PAGENAME}}}}]] | ||
[[Category:Command_Group:_Variables|{{uc:{{PAGENAME}}}}]] | [[Category:Command_Group:_Variables|{{uc:{{PAGENAME}}}}]] | ||
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]] | [[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]] |
Revision as of 22:18, 15 January 2019
Description
- Description:
- Searches for an element within array for which the code evaluates to true. Returns the zero-based index on success or -1 if not found. Code on the right side of the command is evaluated for each element of the array, processed element can be referenced in code as _x.
- Groups:
- Uncategorised
Syntax
- Syntax:
- array findIf code
- Parameters:
- array: Array - array to search in
- code: Code - code defining an element to find, processed element can be referenced within the code as _x
- Return Value:
- Number - Zero-based position of the first array element for which the code evaluate to true, -1 if not found
Examples
- Example 1:
[unit1, unit2, unit3] findIf {not alive _x}; // return index of the first dead unit)
- Example 2:
// two ways how to 'wait for all units to be dead': waitUntil { [unit1, unit2, unit3] findIf {alive _x} == -1 }; // fast, terminates as soon as it finds a living unit waitUntil { {alive _x} count [unit1, unit2, unit3] == 0 }; // slow, always goes through all array elements
Additional Information
Notes
-
Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note