Skip to content

Commit 738a32d

Browse files
authored
Gunbag - Optimise weapon taking/storing code (#10053)
Optimise gunbag code
1 parent 06f47e6 commit 738a32d

3 files changed

Lines changed: 43 additions & 77 deletions

File tree

addons/gunbag/functions/fnc_offGunbagCallback.sqf

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* None
1212
*
1313
* Example:
14-
* [player, target] call ace_gunbag_fnc_offGunbagCallback
14+
* [player, cursorObject] call ace_gunbag_fnc_offGunbagCallback
1515
*
1616
* Public: No
1717
*/
@@ -23,39 +23,28 @@ private _gunbag = backpackContainer _target;
2323
private _state = _gunbag getVariable [QGVAR(gunbagWeapon), []];
2424

2525
if (_state isEqualTo []) exitWith {
26-
[localize LSTRING(empty)] call EFUNC(common,displayTextStructured);
26+
[LLSTRING(empty)] call EFUNC(common,displayTextStructured);
2727
};
2828

2929
_state params ["_weapon", "_items", "_magazines"];
3030

31-
_unit addWeapon _weapon;
31+
[_unit, _weapon, true, _magazines] call EFUNC(common,addWeapon);
3232

33-
// Game will auto add magazines from player's inventory, put these back in player inventory as they will be overwritten
34-
([_unit, _weapon] call EFUNC(common,getWeaponState)) params ["", "", "_addedMags", "_addedAmmo"];
33+
// Add attachments
3534
{
36-
if (((_x select 0) != "") && {(_addedMags select _forEachIndex) != ""}) then {
37-
TRACE_2("Re-adding mag",_x,_addedMags select _forEachIndex);
38-
_unit addMagazine [_addedMags select _forEachIndex, _addedAmmo select _forEachIndex];
39-
};
40-
} forEach _magazines;
41-
42-
removeAllPrimaryWeaponItems _unit;
43-
44-
{
45-
_unit addWeaponItem [_weapon, _x];
46-
} forEach (_items + _magazines);
35+
_unit addWeaponItem [_weapon, _x, true];
36+
} forEach (_items select {_x != ""});
4737

4838
_unit selectWeapon _weapon;
4939

50-
_magazines = _magazines apply {_x select 0};
40+
private _mass = [_weapon, _items, _magazines apply {_x select 0}] call FUNC(calculateMass);
5141

52-
private _mass = [_weapon, _items, _magazines] call FUNC(calculateMass);
53-
54-
// remove virtual load
42+
// Remove virtual load
5543
[_target, _gunbag, -_mass] call EFUNC(movement,addLoadToUnitContainer);
44+
5645
_gunbag setVariable [QGVAR(gunbagWeapon), [], true];
5746

58-
// play sound
47+
// Play sound
5948
if (["ace_backpacks"] call EFUNC(common,isModLoaded)) then {
6049
[_target, _gunbag] call EFUNC(backpacks,backpackOpened);
6150
};
Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "..\script_component.hpp"
22
/*
3-
* Author: Ir0n1E and mjc4wilton
3+
* Author: Ir0n1E, mjc4wilton
44
* Swap primary weapon and weapon in gunbag.
55
*
66
* Arguments:
@@ -11,66 +11,49 @@
1111
* None
1212
*
1313
* Example:
14-
* [player, target] call ace_gunbag_fnc_swapGunbagCallback
14+
* [player, cursorObject] call ace_gunbag_fnc_swapGunbagCallback
1515
*
1616
* Public: No
1717
*/
1818

1919
params ["_unit", "_target"];
20-
private _currentWeapon = primaryWeapon _unit; //Get Current Weapon
21-
private _gunbag = backpackContainer _target;
22-
23-
24-
//---Set up current weapon for storing
25-
private _currentWeaponState = [_unit, _currentWeapon] call EFUNC(common,getWeaponState); //Gets weapon attachments
2620

27-
/*
28-
* example return value _state
29-
* [["","","optic_Aco",""],["arifle_MX_GL_ACO_F","GL_3GL_F"],["30Rnd_65x39_caseless_mag","1Rnd_HE_Grenade_shell"],[30,1]]
30-
*/
21+
// Set up current weapon for storing
22+
private _gunbag = backpackContainer _target;
23+
private _currentItems = (getUnitLoadout _unit) select 0;
3124

32-
_currentWeaponState params ["_currentWeaponItems", "", "_currentWeaponMagazines", "_currentWeaponAmmo"]; //Extract Weapon Attachments to separate arrays
25+
private _currentMagazines = _currentItems select [4, 2];
26+
_currentItems deleteRange [4, 2];
3327

34-
private _currentWeaponMass = [_currentWeapon, _currentWeaponItems, _currentWeaponMagazines] call FUNC(calculateMass);
28+
private _currentWeapon = _currentItems deleteAt 0;
3529

36-
{
37-
_currentWeaponMagazines set [_forEachIndex, [_x, _currentWeaponAmmo select _forEachIndex]];
38-
} forEach _currentWeaponMagazines;
30+
private _currentMass = [_currentWeapon, _currentItems, _currentMagazines apply {_x select 0}] call FUNC(calculateMass);
3931

40-
//---Set up weapon in gunbag
41-
private _newWeaponState = _gunbag getVariable [QGVAR(gunbagWeapon), []];
32+
// Set up weapon in gunbag
33+
private _newState = _gunbag getVariable [QGVAR(gunbagWeapon), []];
4234

43-
if (_newWeaponState isEqualTo []) exitWith {
35+
if (_newState isEqualTo []) exitWith {
4436
[LLSTRING(empty)] call EFUNC(common,displayTextStructured);
4537
};
4638

47-
_newWeaponState params ["_newWeapon", "_newWeaponItems", "_newWeaponMagazines"];
39+
_newState params ["_newWeapon", "_newItems", "_newMagazines"];
4840

49-
//---Swap Weapons
41+
// Swap Weapons
5042
_unit removeWeapon _currentWeapon;
51-
_unit addWeapon _newWeapon;
52-
53-
// Game will auto add magazines from player's inventory, put these back in player inventory as they will be overwritten
54-
([_unit, _newWeapon] call EFUNC(common,getWeaponState)) params ["", "", "_addedMags", "_addedAmmo"];
55-
{
56-
if (((_x select 0) != "") && {(_addedMags select _forEachIndex) != ""}) then {
57-
TRACE_2("Re-adding mag",_x,_addedMags select _forEachIndex);
58-
_unit addMagazine [_addedMags select _forEachIndex, _addedAmmo select _forEachIndex];
59-
};
60-
} forEach _newWeaponMagazines;
6143

62-
removeAllPrimaryWeaponItems _unit;
44+
[_unit, _newWeapon, true, _newMagazines] call EFUNC(common,addWeapon);
6345

46+
// Add attachments
6447
{
65-
_unit addWeaponItem [_newWeapon, _x];
66-
} forEach (_newWeaponItems + _newWeaponMagazines);
48+
_unit addWeaponItem [_newWeapon, _x, true];
49+
} forEach (_newItems select {_x != ""});
6750

6851
_unit selectWeapon _newWeapon;
6952

70-
_newWeaponMagazines = _newWeaponMagazines apply {_x select 0};
53+
private _newMass = [_newWeapon, _newItems, _newMagazines apply {_x select 0}] call FUNC(calculateMass);
7154

72-
private _newWeaponMass = [_newWeapon, _newWeaponItems, _newWeaponMagazines] call FUNC(calculateMass);
55+
// Update virtual load
56+
[_target, _gunbag, _currentMass - _newMass] call EFUNC(movement,addLoadToUnitContainer);
7357

74-
// update virtual load
75-
[_target, _gunbag, _currentWeaponMass - _newWeaponMass] call EFUNC(movement,addLoadToUnitContainer);
76-
_gunbag setVariable [QGVAR(gunbagWeapon), [_currentWeapon, _currentWeaponItems, _currentWeaponMagazines], true]; //Replace weapon in gunbag
58+
// Replace weapon in gunbag
59+
_gunbag setVariable [QGVAR(gunbagWeapon), [_currentWeapon, _currentItems, _currentMagazines], true];

addons/gunbag/functions/fnc_toGunbagCallback.sqf

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,32 @@
1111
* None
1212
*
1313
* Example:
14-
* [player, target] call ace_gunbag_fnc_toGunbagCallback
14+
* [player, cursorObject] call ace_gunbag_fnc_toGunbagCallback
1515
*
1616
* Public: No
1717
*/
1818

1919
params ["_unit", "_target"];
2020

21-
private _weapon = primaryWeapon _unit;
21+
// Set up current weapon for storing
2222
private _gunbag = backpackContainer _target;
23+
private _items = (getUnitLoadout _unit) select 0;
2324

24-
private _state = [_unit, _weapon] call EFUNC(common,getWeaponState);
25+
private _magazines = _items select [4, 2];
26+
_items deleteRange [4, 2];
2527

26-
/*
27-
* example return value _state
28-
* [["","","optic_Aco",""],["arifle_MX_GL_ACO_F","GL_3GL_F"],["30Rnd_65x39_caseless_mag","1Rnd_HE_Grenade_shell"],[30,1]]
29-
*/
30-
31-
_state params ["_items", "", "_magazines", "_ammo"];
28+
private _weapon = _items deleteAt 0;
3229

33-
private _mass = [_weapon, _items, _magazines] call FUNC(calculateMass);
34-
35-
{
36-
_magazines set [_forEachIndex, [_x, _ammo select _forEachIndex]];
37-
} forEach _magazines;
30+
private _mass = [_weapon, _items, _magazines apply {_x select 0}] call FUNC(calculateMass);
3831

3932
_unit removeWeapon _weapon;
4033

41-
// add virtual load
34+
// Add virtual load
4235
[_target, _gunbag, _mass] call EFUNC(movement,addLoadToUnitContainer);
36+
4337
_gunbag setVariable [QGVAR(gunbagWeapon), [_weapon, _items, _magazines], true];
4438

45-
// play sound
39+
// Play sound
4640
if (["ace_backpacks"] call EFUNC(common,isModLoaded)) then {
4741
[_target, _gunbag] call EFUNC(backpacks,backpackOpened);
4842
};

0 commit comments

Comments
 (0)