ACE3/addons/interaction/functions/fnc_switchWeaponAttachment.sqf
Dystopian 69c3d2bb65
Add Weapon Attachments interaction (#7706)
* Add Weapon Attachments interaction

* Apply recommended changes

* Format addPEH

Co-authored-by: mharis001 <34453221+mharis001@users.noreply.github.com>

* Remove toLower

* Update required CBA version

* Rename category to Accessories

* Change setting var name

* Add setting description

* Fix notification size

* Optimize with new commands

Co-authored-by: mharis001 <34453221+mharis001@users.noreply.github.com>
2021-03-04 11:43:11 -06:00

60 lines
1.8 KiB
Plaintext

#include "script_component.hpp"
/*
* Author: mharis001, Dystopian
* Switches weapon attachment.
*
* Arguments:
* 0: Target <OBJECT>
* 1: Player (not used) <OBJECT>
* 2: Action params <ARRAY>
*
* Return Value:
* None
*
* Example:
* [player, player, [currentWeapon player, "acc_flashlight", ""]] call ace_interaction_fnc_switchWeaponAttachment
*
* Public: No
*/
params ["_unit", "", "_actionParams"];
_actionParams params ["_weapon", "_newAttachment", "_oldAttachment"];
TRACE_3("Switching attachment",_weapon,_newAttachment,_oldAttachment);
[_unit, "Gear"] call EFUNC(common,doGesture);
private _addNew = _newAttachment isNotEqualTo "";
private _removeOld = _oldAttachment isNotEqualTo "";
if (_addNew) then {
_unit removeItem _newAttachment;
};
if (_removeOld && {!([_unit, _oldAttachment] call CBA_fnc_canAddItem)}) exitWith {
LOG("no space");
[LELSTRING(common,Inventory_Full)] call EFUNC(common,displayTextStructured);
if (_addNew) then {
_unit addItem _newAttachment;
};
};
if (_removeOld) then {
[{
params ["_unit", "_weapon", "_oldAttachment"];
switch (_weapon) do {
case (primaryWeapon _unit): {_unit removePrimaryWeaponItem _oldAttachment;};
case (handgunWeapon _unit): {_unit removeHandgunItem _oldAttachment;};
default {_unit removeSecondaryWeaponItem _oldAttachment;};
};
_unit addItem _oldAttachment;
}, [_unit, _weapon, _oldAttachment], 0.3] call CBA_fnc_waitAndExecute;
};
if (!_addNew) exitWith {};
[{
params ["_unit", "_weapon", "_newAttachment"];
_unit addWeaponItem [_weapon, _newAttachment];
[[getText (configFile >> "CfgWeapons" >> _newAttachment >> "picture"), 4], true] call CBA_fnc_notify;
}, [_unit, _weapon, _newAttachment], 1] call CBA_fnc_waitAndExecute;