ACE3/addons/interaction/functions/fnc_switchWeaponAttachment.sqf
johnb432 4db0f7de24
Interaction - Improve FUNC(switchWeaponAttachment) (#10145)
* Improve swtichWeaponAttachment

* change interact_SWA to use common_SAM (#10176)

* change interact_SWA to use common_SAM

* Directly call

* exit on bad arg

* Update fnc_switchAttachmentMode.sqf

* Minor optimisations

* Cleanup leftover code, use unit instead of target

---------

Co-authored-by: johnb432 <58661205+johnb432@users.noreply.github.com>

* Update addons/interaction/functions/fnc_switchWeaponAttachment.sqf

* Revert some unnecessary changes

* Remove sound, as it was part of the CBA attachment switching & pass missing parameter

* Add sound back, to indicate new attachment

---------

Co-authored-by: PabstMirror <pabstmirror@gmail.com>
2024-08-09 14:08:07 -07:00

76 lines
2.1 KiB
Plaintext

#include "..\script_component.hpp"
/*
* Author: mharis001, Dystopian
* Switches weapon attachment.
*
* Arguments:
* 0: Target (not used) <OBJECT>
* 1: Player <OBJECT>
* 2: Action params <ARRAY>
* - 0: Weapon <STRING>
* - 1: New Attachment <STRING>
* - 2: Old Attachment <STRING>
*
* 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);
private _currWeaponType = [_unit, _weapon] call EFUNC(common,getWeaponIndex);
if (_currWeaponType == -1) exitWith {};
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;
};
};
[_unit, "Gear"] call EFUNC(common,doGesture);
if (_removeOld) then {
[{
params ["_unit", "_currWeaponType", "_oldAttachment"];
switch (_currWeaponType) do {
case 0: {_unit removePrimaryWeaponItem _oldAttachment};
case 1: {_unit removeSecondaryWeaponItem _oldAttachment};
case 2: {_unit removeHandgunItem _oldAttachment};
default {};
};
_unit addItem _oldAttachment;
}, [_unit, _currWeaponType, _oldAttachment], 0.3] call CBA_fnc_waitAndExecute;
};
if (!_addNew) exitWith {};
[{
params ["_unit", "_weapon", "_newAttachment"];
_unit addWeaponItem [_weapon, _newAttachment];
if (_unit != ACE_player) exitWith {};
[[getText (configFile >> "CfgWeapons" >> _newAttachment >> "picture"), 4], true] call CBA_fnc_notify;
playSound "click";
}, [_unit, _weapon, _newAttachment], 1] call CBA_fnc_waitAndExecute;