pierremgi 4850 Posted November 20, 2018 ADAPT SILENCERS TO COMBAT/STEALTH SITUATION Hi all, Did you remark that AIs don't adapt their muzzle sound suppressor (silencer) with the ordered situation (STEALTH / COMBAT)?. Here is a little code for adding/removing existing silencer on current weapon. Notes: - by "existing", I means the silencer must be on weapon or inside unit's gear. Furthermore, as you can start with an already equipped silencer and no more room in gear, this script will record that in a variable and will be able to re-fit the silencer on weapon instead of losing him due to the lack of room. (a kind of extra pocket). - MP compatible but I intentionally worked at idle for better performance. If your unit's localization (PC owning it) is supposed to change, You'll need to make the variable broadcast (adding true as 3rd parameter). And, that's for the specific case your unit need the "extra pocket". - players are not concerned, but it's easy, for automatic adapted muzzle, to remove the isPlayer condition. - for performance, this script is not "on each framed". It could be. I chose to fire the code using the "animChanged" EH. In some cases, the unit can start to fire without changing its animation. So, try to order the new behavior in advance (Anims often change anyway). - some silencer can be non-removable, hard-coded with model, or simply don't exist! There is nothing to do with that. It's up to you to choose the right weapon. Tested on Vanilla and CUP. For other mods, have your own test. This will depend on how silencer classes are named. version Nov. 20th 2018 parameters: NONE { _x addEventHandler ["AnimChanged", { params ["_unit","","_silencer"]; if (!isplayer _unit) then { if (isnil {_unit getVariable "MGI_silencer"}) then { _unit setVariable ["MGI_silencer",[]] }; _silencers = (items _unit select {["_snds",_x] call bis_fnc_instring}) + (weapons _unit apply {_unit weaponAccessories _x param [0, ""]} select {_x != ""}) + (_unit getVariable "MGI_silencer"); call { if (behaviour _unit == "COMBAT" && {isNull objectParent _unit} && {_unit weaponAccessories currentMuzzle _unit param [0, ""] != ""}) exitWith { _silencer = _unit weaponAccessories currentMuzzle _unit param [0, ""]; call { if (currentWeapon _unit isEqualTo primaryWeapon _unit) exitWith { _unit removePrimaryWeaponItem _silencer; }; if (currentWeapon _unit isEqualTo handGunWeapon _unit) exitWith { _unit removeHandGunItem _silencer; }; }; call { if (_unit canAddItemToBackpack _silencer) exitWith { _unit addItemToBackpack _silencer; }; if (_unit canAddItemToVest _silencer) exitWith { _unit addItemToVest _silencer; }; if (_unit canAddItemToUniform _silencer) exitWith { _unit addItemToUniform _silencer; }; (_unit getVariable "MGI_silencer") pushBackUnique _silencer; }; }; if (behaviour _unit == "stealth" && {isNull objectParent _unit} && {_unit weaponAccessories currentMuzzle _unit param [0, ""] == ""}) exitWith { {_unit addWeaponItem [currentWeapon _unit,_x]} count _silencers; _silencer = _unit weaponAccessories currentMuzzle _unit param [0, ""]; if (_silencer in (_unit getVariable ["MGI_silencer",[]])) then { _unit setVariable ["MGI_silencer", (_unit getVariable "MGI_silencer") - [_silencer]]; } else { if (_silencer in items _unit) then {_unit removeitem _silencer}; }; }; }; }; }] } forEach (playableUnits + switchableUnits); 2 4 Share this post Link to post Share on other sites
johnnyboy 3789 Posted November 20, 2018 That is slick and useful! Thanks Pierre. 2 Share this post Link to post Share on other sites
GEORGE FLOROS GR 4207 Posted November 20, 2018 This is really great Pierre ! Thank you very much ! 1 Share this post Link to post Share on other sites
zagor64bz 1225 Posted November 21, 2018 .....a really needed one. 1 Share this post Link to post Share on other sites
M1ke_SK 230 Posted November 21, 2018 Nice. For those if (_unit canAddItemToBackpack _silencer) exitWith { _unit addItemToBackpack _silencer; }; if (_unit canAddItemToVest _silencer) exitWith { _unit addItemToVest _silencer; }; if (_unit canAddItemToUniform _silencer) exitWith { _unit addItemToUniform _silencer; }; you can use if (_unit canAddItem _silencer) exitWith { _unit addItem _silencer; }; 1 Share this post Link to post Share on other sites
pierremgi 4850 Posted November 21, 2018 3 minutes ago, M1ke_SK said: Nice. For those if (_unit canAddItemToBackpack _silencer) exitWith { _unit addItemToBackpack _silencer; }; if (_unit canAddItemToVest _silencer) exitWith { _unit addItemToVest _silencer; }; if (_unit canAddItemToUniform _silencer) exitWith { _unit addItemToUniform _silencer; }; you can use if (_unit canAddItem _silencer) exitWith { _unit addItem _silencer; }; i don't remember exactly, but it seemed to me, addItem can add the silencer straight on the weapon, what's I wanted to avoid of course. And addItemCargo was not accurate for units. I'm still fond of a better working solution. Share this post Link to post Share on other sites
M1ke_SK 230 Posted November 21, 2018 You mean to avoid "addWeaponItem". I don't think that "addItem" is adding attachments to weapon, it is adding magazines however. Share this post Link to post Share on other sites
dimon 32 Posted May 30 Thank you very much ! I've changed the code a bit to use any mods. _silencers = (items _unit select { ["_snds",_x] call bis_fnc_instring || (getnumber( (configfile >> "CfgWeapons") >> _x >> 'ItemInfo' >> 'type') == 101 && {(getnumber ((configfile >> "CfgWeapons") >> _x>> 'ItemInfo' >> 'AmmoCoef'>> 'audibleFire')) > 0}) } ) + (weapons _unit apply {_unit weaponAccessories _x param [0, ""]} select {_x != ""}) + (_unit getVariable "MGI_silencer"); 1 Share this post Link to post Share on other sites