Skip to content

Commit

Permalink
Merge pull request #1244 from CBATeam/selectBest
Browse files Browse the repository at this point in the history
Add CBA_fnc_selectBest function
  • Loading branch information
commy2 authored Nov 15, 2019
2 parents 6f568c0 + 0be0b06 commit e99f079
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions addons/arrays/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class CfgFunctions {
PATHTO_FNC(join);
PATHTO_FNC(reject);
PATHTO_FNC(select);
PATHTO_FNC(selectBest);
PATHTO_FNC(selectRandomArray);
PATHTO_FNC(shuffle);
PATHTO_FNC(sortNestedArray);
Expand Down
40 changes: 40 additions & 0 deletions addons/arrays/fnc_selectBest.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_selectBest
Description:
Select best element from an array
Parameters:
_array - Input Array [Array]
_criteria - Code that is passed element and should return an integer value [Code]
_return - default return value if array is empty (optional, default nil) [Any]
Returns:
Element that scores the highest [Any]
Example:
(begin example)
_result = [[2, -6, 4], {abs _x}] call CBA_fnc_selectBest;
// _result => -6
_result = [[q1, q2], {-(t1 distance _x)}, objNull] call CBA_fnc_selectBest; // selects closest target (negative distance)
// _result => q1
(end)
Author:
PabstMirror
---------------------------------------------------------------------------- */
SCRIPT(selectBest);

params ["_array", "_criteria", "_return"];

private _bestScore = -1e99;
{
private _xScore = _x call _criteria;
if (_xScore > _bestScore) then {
_return = _x;
_bestScore = _xScore;
};
} forEach _array;

RETNIL(_return)

0 comments on commit e99f079

Please sign in to comment.