findIf: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
mNo edit summary
m (Text replacement - "(\|[pr][0-9]+ *= *[^- ]*) *- *Z([a-z ])" to "$1 - z$2")
 
(25 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{Command|= Comments
{{RV|type=command
____________________________________________________________________________________________


| arma3dev |= Game name
|game1= arma3
|version1= 1.82


|1.81|= Game version
|gr1= Arrays
____________________________________________________________________________________________


| Searches for an element within array for which the code evaluates to true. Returns the 0 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.<br><br>
|descr= 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.<br>
Code on the right side of the command is evaluated for each element of the array, processed element can be referenced in code as [[Magic Variables#x|_x]].
{{Feature|informative|[[findIf]] terminates as soon as it finds an element that fulfills the condition, thus it is more efficient than [[count]] in scenarios where a check for presence / absence of at least one element should be done (See {{Link|#Example 2}}).}}


NOTE: Because command findIf terminates as soon as it finds an element that fulfills the condition, it is more efficient then [[count]] in scenarios where you need to check for presence / absence of at least one element that evaluates to true.  |= Description
|s1= array [[findIf]] code
____________________________________________________________________________________________


| 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 [[Magic Variables#x|_x]]


|p2= code: [[Code]] - code defining an element to find, processed element can be referenced within the code as _x |= Parameter 2
|r1= [[Number]] - zero-based position of the first array element for which the code evaluate to true, -1 if not found


| [[Number]] - 0 based position of the first array element for which the code evaluate to true, -1 if not found |= Return value
|x1= <sqf>[unit1, unit2, unit3] findIf {not alive _x}; // return index of the first dead unit)</sqf>


____________________________________________________________________________________________
|x2= <sqf>
 
// two ways how to 'wait for all units to be dead':
|x1= <code>[unit1,unit2,unit3] [[findIf]] {!alive _x}; //return index of first dead unit</code> |= Example 1
waitUntil { [unit1, unit2, unit3] findIf { alive _x } == -1 }; // fast, terminates as soon as it finds a living unit
|x2= <code>waitUntil{[unit1,unit2,unit3] [[findIf]] {alive _x} == -1}; //wait until all are dead, faster then count as it doesn't need to go through all elements unless all evals to false = are dead</code> |= Example 2
waitUntil { { alive _x } count [unit1, unit2, unit3] == 0 }; // slow, always goes through all array elements
___________________________________________________________________________________________
</sqf>
 
| [[find]] [[count]] |= See also


|seealso= [[find]] [[count]]
}}
}}
<h3 style="display:none">Notes</h3>
<dl class="command_description">
<!-- Note Section BEGIN -->
<!-- Note Section END -->
</dl>
<h3 style="display:none">Bottom Section</h3>
[[Category:Scripting Commands|FIND]]
[[Category:Scripting Commands OFP 1.99|FIND]]
[[Category:Scripting Commands ArmA|FIND]]
[[Category:Command_Group:_Variables|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands ArmA2|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting_Commands_Take_On_Helicopters|{{uc:{{PAGENAME}}}}]]

Latest revision as of 17:36, 8 November 2023

Hover & click on the images for description

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.
findIf terminates as soon as it finds an element that fulfills the condition, thus it is more efficient than count in scenarios where a check for presence / absence of at least one element should be done (See Example 2).
Groups:
Arrays

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

See also:
find count

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