Skip to content

Commit 8435e93

Browse files
Add Switch Weapon context menu actions for infantry (#520)
* Add context menu actions under Loadout to switch an AI unit's current weapon * Disabled on players, and AI which are currently holding their single available weapon * https://youtu.be/5hq_pCK9_UM Co-authored-by: mharis001 <[email protected]>
1 parent d08a6e7 commit 8435e93

File tree

7 files changed

+120
-0
lines changed

7 files changed

+120
-0
lines changed

addons/common/XEH_postInit.sqf

+5
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@
173173
_vehicle engineOn _state;
174174
}] call CBA_fnc_addEventHandler;
175175

176+
[QGVAR(selectWeapon), {
177+
params ["_unit", "_muzzle"];
178+
_unit selectWeapon _muzzle;
179+
}] call CBA_fnc_addEventHandler;
180+
176181
[QGVAR(setPilotLight), {
177182
params ["_vehicle", "_lights"];
178183
_vehicle setPilotLight _lights;

addons/context_actions/CfgContext.hpp

+22
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,28 @@ class EGVAR(context_menu,actions) {
250250
statement = QUOTE(_hoveredEntity setUnitLoadout configOf _hoveredEntity);
251251
icon = "\a3\3den\Data\Displays\Display3DEN\ToolBar\undo_ca.paa";
252252
};
253+
class SwitchWeapon {
254+
displayName = "$STR_A3_Switch1";
255+
icon = "\a3\ui_f\data\IGUI\Cfg\Actions\reammo_ca.paa";
256+
class Primary {
257+
displayName = "$STR_A3_RSCDisplayArsenal_Tab_PrimaryWeapon";
258+
condition = QUOTE([ARR_2(_hoveredEntity,_args)] call FUNC(canSwitchWeapon));
259+
statement = QUOTE([ARR_2(_hoveredEntity,_args)] call FUNC(switchWeapon));
260+
modifierFunction = QUOTE(call FUNC(switchWeaponModifier));
261+
icon = "\a3\ui_f\data\GUI\Rsc\RscDisplayArsenal\primaryWeapon_ca.paa";
262+
args = 0;
263+
};
264+
class Handgun: Primary {
265+
displayName = "$STR_A3_RSCDisplayArsenal_Tab_Handgun";
266+
icon = "\a3\ui_f\data\GUI\Rsc\RscDisplayArsenal\handgun_ca.paa";
267+
args = 1;
268+
};
269+
class Binoculars: Primary {
270+
displayName = "$STR_A3_RSCDisplayArsenal_Tab_Binoculars";
271+
icon = "\a3\ui_f\data\GUI\Rsc\RscDisplayArsenal\binoculars_ca.paa";
272+
args = 2;
273+
};
274+
};
253275
};
254276
class Inventory {
255277
displayName = "$STR_A3_Gear1";

addons/context_actions/XEH_PREP.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ PREP(canPasteVehicleAppearance);
66
PREP(canRearmVehicles);
77
PREP(canRefuelVehicles);
88
PREP(canRepairVehicles);
9+
PREP(canSwitchWeapon);
910
PREP(canToggleSurrender);
1011
PREP(canUnloadViV);
1112
PREP(copyVehicleAppearance);
@@ -25,6 +26,8 @@ PREP(setCombatMode);
2526
PREP(setFormation);
2627
PREP(setSpeedMode);
2728
PREP(setStance);
29+
PREP(switchWeaponModifier);
30+
PREP(switchWeapon);
2831
PREP(teleportPlayers);
2932
PREP(teleportZeus);
3033
PREP(toggleCaptive);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "script_component.hpp"
2+
/*
3+
* Author: Ampersand
4+
* Checks if the given unit can switch to the selected weapon.
5+
*
6+
* Arguments:
7+
* 0: Unit <OBJECT>
8+
* 1: Weapon Index <NUMBER>
9+
*
10+
* Return Value:
11+
* Can Switch Weapon <BOOL>
12+
*
13+
* Example:
14+
* [_unit, 0] call zen_context_actions_fnc_canSwitchWeapon
15+
*
16+
* Public: No
17+
*/
18+
19+
params ["_unit", "_weaponIndex"];
20+
21+
private _weapon = [primaryWeapon _unit, handgunWeapon _unit, binocular _unit] select _weaponIndex;
22+
23+
alive _unit
24+
&& {!isPlayer _unit}
25+
&& {_weapon != ""}
26+
&& {_weapon != currentWeapon _unit}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "script_component.hpp"
2+
/*
3+
* Author: Ampersand
4+
* Switches the given unit's weapon to the selected one.
5+
*
6+
* Arguments:
7+
* 0: Unit <OBJECT>
8+
* 1: Weapon Index <NUMBER>
9+
*
10+
* Return Value:
11+
* None
12+
*
13+
* Example:
14+
* [_unit, _weaponIndex] call zen_context_actions_fnc_switchWeapon
15+
*
16+
* Public: No
17+
*/
18+
19+
params ["_unit", "_weaponIndex"];
20+
21+
private _weapon = [primaryWeapon _unit, handgunWeapon _unit, binocular _unit] select _weaponIndex;
22+
[QEGVAR(common,selectWeapon), [_unit, _weapon], _unit] call CBA_fnc_targetEvent;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "script_component.hpp"
2+
/*
3+
* Author: Ampersand
4+
* Modifies the switch weapon action based on the hovered entity.
5+
*
6+
* Arguments:
7+
* 0: Action <ARRAY>
8+
* 1: Action Parameters <ARRAY>
9+
*
10+
* Return Value:
11+
* None
12+
*
13+
* Example:
14+
* [_action, _actionParams] call zen_context_actions_fnc_switchWeaponModifier
15+
*
16+
* Public: No
17+
*/
18+
19+
params ["_action", "_actionParams"];
20+
_action params ["", "", "", "", "", "", "_args"];
21+
_actionParams params ["", "", "", "", "", "_hoveredEntity"];
22+
23+
private _weapon = [
24+
primaryWeapon _hoveredEntity,
25+
handgunWeapon _hoveredEntity,
26+
binocular _hoveredEntity
27+
] select _args;
28+
29+
private _cfgWeapons = configFile >> "CfgWeapons" >> _weapon;
30+
31+
private _displayName = getText (_cfgWeapons >> "displayName");
32+
33+
if (_displayName != "") then {
34+
_action set [1, _displayName];
35+
};
36+
37+
private _picture = getText (_cfgWeapons >> "picture");
38+
39+
if (_picture != "") then {
40+
_action set [2, _picture];
41+
};

docs/user_guide/context_actions.md

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ The main action acts as a shortcut for the "Edit" sub-action.
5757

5858
Opens the preferred arsenal type (specified in CBA settings) on the hovered unit.
5959
Sub-actions allow for copying and pasting the hovered unit's loadout onto another and resetting the unit's loadout to the config defined one.
60+
Furthermore, the unit's current weapon can be switched between their rifle, handgun, or binoculars.
6061
The main action acts as a shortcut for the "Edit" sub-action.
6162

6263
## Remote Control

0 commit comments

Comments
 (0)