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_compatibleMagazines that supports mag wells #965

Merged
merged 4 commits into from
Aug 25, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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/common/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class CfgFunctions {
PATHTO_FNC(addWeapon);
PATHTO_FNC(addMagazine);
PATHTO_FNC(addItem);
PATHTO_FNC(compatibleMagazines);
PATHTO_FNC(removeWeapon);
PATHTO_FNC(removeMagazine);
PATHTO_FNC(removeItem);
Expand Down
54 changes: 54 additions & 0 deletions addons/common/fnc_compatibleMagazines.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_compatibleMagazines

Description:
Retrievs a list of magazines that are compatible with a weapon.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retrieves


Parameters:
_weapon - Weapon configName or config

Example:
(begin example)
_mags = ["arifle_MX_SW_F"] call CBA_fnc_compatibleMagazines
(end)

Returns:
Array of magazine classnames <ARRAY>

Author:
PabstMirror, based on code from Dedmen
---------------------------------------------------------------------------- */
SCRIPT(compatibleMagazines);

params [["_weapon", "", ["", configNull]]];

if (_weapon isEqualType "") then {
_weapon = configFile >> "CfgWeapons" >> _weapon;
};

private _cacheKey = str _weapon;
if (_cacheKey == "") exitWith { ERROR_1("Weapon Does Not Exist %1",_this); [] };

if (isNil QGVAR(magNamespace)) then { GVAR(magNamespace) = call CBA_fnc_createNamespace; };

private _returnMags = GVAR(magNamespace) getVariable _cacheKey;

if (isNil "_returnMags") then {
Copy link
Contributor

@Dystopian Dystopian Aug 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be replaced with early return
if (!isNil "_returnMags") exitWith {_returnMags};
if (!isNil "_returnMags") exitWith {+_returnMags};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to copy the returned array, otherwise someone will mess up the cache by editing it by reference.

_returnMags = getArray (_weapon >> "magazines");
{
private _wellConfig = configFile >> "CfgMagazineWells" >> _x;
{
_returnMags append getArray _x;
} forEach configProperties [_wellConfig, "isArray _x", true];
} forEach (getArray (_weapon >> "magazineWell"));

private _cfgMagazines = configFile >> "CfgMagazines";
_returnMags = _returnMags select {isClass (_cfgMagazines >> _x)};
_returnMags = _returnMags apply {configName (_cfgMagazines >> _x)};
_returnMags = _returnMags arrayIntersect _returnMags;

GVAR(magNamespace) setVariable [_cacheKey, _returnMags];
};

+_returnMags