Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CBA_fnc_selectBest function #1244

Merged
merged 2 commits into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)