Jump to content
dreadedentity

[CODE SNIPPET] Throw magazines to your teammates

Recommended Posts

Hello! I was watching videos of milsim clans playing the game, people were out of ammo, they were being overwhelmed by enemies. In the heat of battle, a guy dropped a magazine on the ground and his teammate had to look down at the ground to pick it up. This isn't realistic! (well in a real battle you probably wouldn't throw mags)
 
"Never again", I said and I got to work. 2 or 3 hours later, here's what I made.

 
Code:

//required so the "throw" can reload, otherwise the first press won't work
player addMagazine "HandGrenade_stone";

//keydown UI EH - Adds another hotkey to the main "game" display to get the keypress
//note 35 == "H" key
(findDisplay 46) displayAddEventHandler ["KeyDown",
{
	if (!(missionNamespace getVariable ["DE_var_throwPressed", false])) then
	{
		if ((_this select 1) == 35) then
		{
			DE_var_throwPressed = true;
			_magName = "";
			_magName = {
				_find = (getArray (configFile >> "cfgWeapons" >> currentWeapon player >> "magazines")) find _x;
				if (_find > -1) exitWith {_x};
			} count ((backpackItems player) + (vestItems player) + (uniformItems player));

			if (typeName _magName == "STRING" && {_magName != ""}) then
			{
				DE_var_throwMag = _magName;
				player addMagazine "HandGrenade_stone";
				player setWeaponReloadingTime [player, "HandGrenade_Stone", 0]; //this doesn't work, it would be the key to solving all of my headaches with this code
				player forceWeaponFire ["HandGrenade_Stone","HandGrenade_Stone"];
			} else
			{
				hint "You do not have an extra magazine for this weapon";
			}
		};
	};
}];

//keyup UI EH - Creates a switch. Prevents the keydown code from spamming if the player holds the key down
(findDisplay 46) displayAddEventHandler ["KeyUp",
{
	if ((_this select 1) == 35) then
	{
		DE_var_throwPressed = false;
	};
}];

//fired EH - Does all the cool stuff
player addEventHandler ["Fired",
{
	_unit = _this select 0;
	_projectile = _this select 6;
	if ((_this select 1) == "Throw") then
	{
		if (missionNamespace getVariable ["DE_var_throwMag", ""] != "") then
		{
			_pos = getPosATL _projectile;
			_vel = velocity _projectile;
			_obj = createVehicle ["WeaponHolderSimulated", _pos, [], 0, "CAN_COLLIDE"];
			_obj addMagazineAmmoCargo [DE_var_throwMag, 1, 30];
			player removeItem DE_var_throwMag;
			_obj setVelocity _vel;
			deleteVehicle _projectile;

			_num = missionNamespace getVariable ["DE_var_throwEvents", 0];
			_event = "DE_throwEvent_" + (str _num);
			[_event, "onEachFrame",
			{
				_near = (nearestObjects [_this select 1, ["MAN"], 3]) - [player];
				if (count _near > 0) then
				{
					[_this select 0, "onEachFrame"] call BIS_fnc_removeStackedEventHandler;
					deleteVehicle (_this select 1);
					(_near select 0) addMagazine [_this select 2, 30];
				};
				if (speed (_this select 1) == 0) then
				{
					[_this select 0, "onEachFrame"] call BIS_fnc_removeStackedEventHandler;
				};
			}, [_event, _obj, DE_var_throwMag]] call BIS_fnc_addStackedEventHandler;
			DE_var_throwEvents = _num + 1;
			DE_var_throwMag = "";
		};
	};
}];
  • Like 9
  • Thanks 2

Share this post


Link to post
Share on other sites

Had to fix a slight error with the "Throw" EH, nothing that would break the script. Code has been updated

  • Like 1

Share this post


Link to post
Share on other sites

Fantastic peace of code! Can you turn this into a MOD? It would be great! Great Job!

Thanks! But I've never done modding for Arma, just scripting. Eventually I'll probably figure out how to do it. I guess just consider this post an open invitation for somebody to put this into a mod (just give credit).

 

Some notes:

  • This code is minimally tested (I basically just made sure it worked with MX's, though in theory it should work with all weapons
  • No MP testing done (again, I only tested this enough to make sure it worked for the video. Actually, I can probably guarantee you that the way it is now it won't work in MP)
  • I wanted to create a way to lower the outward velocity for magazines with higher bullet counts (more bullets = heavier magazine = less velocity = doesn't get thrown as far), but I didn't
  • Removes the first magazine from your inventory (no matter the bullet count) and adds a magazine with 30 bullets to the target (even if the mag can hold more). Could be abused
  • One thing that is pretty cool though, is that if you miss, the magazine just falls to the ground where it can then be picked up the slow way
  • For some reason, you cannot throw if you are moving (even though you can throw other thrown items)

All of these things will probably need to be fixed before this turns into something amazing instead of "Oh, that's cool"

  • Like 2

Share this post


Link to post
Share on other sites

Where do you put that code snippet?

init.sqf is fine for SP and player-hosted MP, but for dedicated server you'll need to put it in initPlayerLocal.sqf because it uses the "player" command (player does not exist on dedicated server so it will throw an error)

  • Like 1

Share this post


Link to post
Share on other sites

Fantastic peace of code! Can you turn this into a MOD? It would be great! Great Job!

Enviado do meu X5 através de Tapatalk

 

Why poeple asks to turn any script, even a small sinpet,  into a mod thinking it's better for anything??? Don't underestimate yourself, you are good enough to use this as script and you won't need to be constantly checking if everyone has the same file than you!!!

 

@dreadedentity

 

Super nice script!

 

BTW a side comment: You need a new PC and you know it ;)

  • Like 1

Share this post


Link to post
Share on other sites

Super nice script!

 

BTW a side comment: You need a new PC and you know it ;)

Thanks!

 

I actually just need a better CPU and graphics card! I have an i5-4670k and an MSI R9 270x

  • Like 1

Share this post


Link to post
Share on other sites

Good job there DE.   :)

Thanks! Certainly it can be modified to throw your gun (I even originally tested the code by adding guns to the weapon holders because adding mags wasn't working), but I didn't see any guns in that clip  ;)

This is really, really cool, so thank you, can't convey how much thanks.

Thanks! I'm not sure what you mean by units under remote control though

 

NOTE: another issue I forgot to mention, if you press the button while "throw" is still reloading, it adds another stone to your inventory. Eventually it could fill you up

Share this post


Link to post
Share on other sites

That script is damn nice. A very nice implementation indeed. Having to drop items for others to come and pick up is a pita.

Share this post


Link to post
Share on other sites

Writed fast, so not perfect.

Number of bullets in throwed magazines are right.

Press | h |.

Check action menu.

(maybe hold for a second, maybe press once more)

Another press will remove the actions.

Working with stones's problem.

Think will add items too.

Execute locally on clients.

//action on units throw to for r.c. u.;
//note 35 == "H" key
//required so the "throw" can reload, otherwise the first press won't work
player addMagazine "HandGrenade_stone";

aath=[false];
stath=true;
nath=0;
//_n=0;
findDisplay 46 displayAddEventHandler ["KeyUp",{
		if((_this select 1) == 35)then{stath=true;};
}];

findDisplay 46 displayAddEventHandler ["KeyDown",{
	if((stath)and((vehicle player)==player))then{
	stath=false;
	if((_this select 1) == 35)then{
		if(aath select 0)then{{player removeAction _x;}forEach(aath select 1);aath=[false];}else{
//str
		aath=[true];
		_a=[];
		_la=[];
		{
		if(!(_x select 0 in _la)and(if(_x select 3!=0)then{!(_x select 2)}else{true}))then{
//w w;
		//_n=_n+1;
		_li=(player addAction ["Throw "+(_x select 0),{
			laath=[_this select 3 select 0,_this select 3 select 1];
			player addMagazine "HandGrenade_stone";
			laath=laath+[(player addEventHandler ["Fired",{if(((_this select 1) == "Throw")and((_this select 5)=="HandGrenade_stone"))then{
				{player removeAction _x;}forEach(aath select 1);
				player removeEventHandler ["Fired",laath select 2];
				aath=[false];
				_ci=laath select 0;
				_ln=laath select 1;
				_m=_this select 6;
				_ve = velocity _m;
				systemchat str _ve;
				_lo = createVehicle ["WeaponHolderSimulated", getPosATL _m, [], 0, "CAN_COLLIDE"];
				_lo addMagazineAmmoCargo [_ci,1,_ln];
				player removeItem _ci;
				_lo setVelocity _ve;
				deleteVehicle _m;
				_lst=true;
				nath=nath+1;
				["ath"+str nath, "onEachFrame",{
					_lo=_this select 0;
					_la = (nearestObjects [_lo,["MAN"],3]) - [player];
					if(count _la > 0)then{
						["ath"+str (_this select 3), "onEachFrame"] call BIS_fnc_removeStackedEventHandler;
						deleteVehicle _lo;
						_la select 0 addMagazine [_this select 1,_this select 2];
					};
					if (speed _lo == 0) then{
						["ath"+str (_this select 3), "onEachFrame"] call BIS_fnc_removeStackedEventHandler;
					};
				},[_lo,_ci,_ln,nath]] call BIS_fnc_addStackedEventHandler;
			};}])];
			player forceWeaponFire ["HandGrenade_Stone","HandGrenade_Stone"];
		},_x,0]);
		//[,_n]
		_la=_la+[_x select 0];
		_a=_a+[_li];
		};
		}forEach magazinesAmmoFull player;
		aath=aath+[_a];
		};
	};
	};
}];

Share this post


Link to post
Share on other sites

this should be made MP compatible..I contacted with DE via steam but seems he lost interest in a3 scripting.

I turned script into mod succesfully but no luck making it MP compatible. Hopefully some of you gurus here show me a way. This is too good to be left out...

add-on version source link https://drive.google.com/file/d/1Zb5Xo8drf1Ou97oXT6UWlgqeLTnKnxtc/view

Share this post


Link to post
Share on other sites
On 1/13/2018 at 9:53 PM, goko-- said:

this should be made MP compatible

Boom

Spoiler

sleep 0.1;

//required so the "throw" can reload, otherwise the first press won't work
player addMagazine "HandGrenade_stone";

//keydown UI EH - Adds another hotkey to the main "game" display to get the keypress
//note 35 == "H" key
(findDisplay 46) displayAddEventHandler ["KeyDown", {
	if ((_this select 1) == 35) then {
		if ((player weaponState "HandGrenade_Stone") # 6 == 0) then {
			if !(player getVariable ["DE_THROW_LOCK", false]) then {
				player setVariable ["DE_THROW_LOCK", true];
				private ["_acceptedMags","_playerMags","_magIndex"];
				_acceptedMags = getArray (configFile >> "cfgWeapons" >> currentWeapon player >> "magazines");
				_playerMags = magazinesAmmo player;
				_magIndex = _playerMags findIf { _acceptedMags find (_x # 0) > -1 };
				if (_magIndex > -1) then {
					player setVariable ["DE_THROWN_MAG",_playerMags # _magIndex];
					player forceWeaponFire ["HandGrenade_Stone","HandGrenade_Stone"];
					player addMagazine "HandGrenade_stone";
				} else {
					systemChat "You do not have an extra magazine for this weapon";
				};
			};
		};
	};
}];

//keyup UI EH - Creates a switch. Prevents the keydown code from spamming if the player holds the key down
(findDisplay 46) displayAddEventHandler ["KeyUp", {
	if ((_this select 1) == 35) then {
		player setVariable ["DE_THROW_LOCK", false];
	};
}];

//fired EH - Does all the cool stuff
//["B Alpha 1-1:1 (DreadedEntity)", "Throw", "HandGrenade_Stone", "HandGrenade_Stone", "GrenadeHand_stone", "HandGrenade_Stone", "1779995: handgrenade.p3d"];
player addEventHandler ["Fired", {
	params ["_unit", "_weapon", "_muzzle", "_mode", "_ammo", "_magazine", "_projectile", "_gunner"];
	if (_weapon == "Throw") then {
		if (_muzzle == "HandGrenade_Stone") then {
			private _thrownMag = player getVariable "DE_THROWN_MAG";
			if (!isNil "_thrownMag") then {
				//Only if all these are true can be assume mag was thrown
				private ["_pos","_vel","_mag"];
				_pos = getPosASL _projectile;
				_vel = velocity _projectile;
				deleteVehicle _projectile;
				_mag = createVehicle ["WeaponHolderSimulated", ASLToAGL _pos, [], 0, "CAN_COLLIDE"];
				_mag addMagazineAmmoCargo [_thrownMag # 0, 1, _thrownMag # 1];
				player removeMagazineGlobal (_thrownMag # 0);
				_mag setVelocity _vel;

				_id = addMissionEventHandler ["EachFrame", {
					private ["_mag","_thrownMag","_near","_manIndex"];
					_mag = _thisArgs # 0;
					_thrownMag = _thisArgs # 1;
					_near = (_mag nearEntities ["Man", 3]) select { _x != player };
					_manIndex = _near findIf { isNull objectParent _x }; //only throw to soldiers on foot :)
					if (_manIndex > -1) then {
						private _unit = _near # _manIndex;
						private _mass = getNumber (configFile >> "CfgMagazines" >> _thrownMag # 0 >> "mass");
						if ((loadAbs _unit) + _mass <= maxLoad _unit) then {
							{
								if (_x != objNull) then {
									if ((loadAbs _x) + _mass <= maxLoad _x) then {
										deleteVehicle _mag;
										_unit addMagazine [_thrownMag # 0, _thrownMag # 1];
										removeMissionEventHandler [_thisEvent, _thisEventHandler];
										break;
									};
								};
							} forEach [uniformContainer _unit, vestContainer _unit, backpackContainer _unit];
						};
					};
					if (speed _mag == 0) then {
						_mag setPosATL (ASLToATL (getPosASL _mag));
						removeMissionEventHandler [_thisEvent, _thisEventHandler];
					};
				}, [_mag, _thrownMag]];
			};
		};
	};
}];

 

Now with 100% more MP compatibility *Mic drop*

 

Github mirror

  • Like 3

Share this post


Link to post
Share on other sites
On 5/5/2022 at 1:45 AM, dreadedentity said:

Boom

  Reveal hidden contents


sleep 0.1;

//required so the "throw" can reload, otherwise the first press won't work
player addMagazine "HandGrenade_stone";

//keydown UI EH - Adds another hotkey to the main "game" display to get the keypress
//note 35 == "H" key
(findDisplay 46) displayAddEventHandler ["KeyDown", {
	if ((_this select 1) == 35) then {
		if ((player weaponState "HandGrenade_Stone") # 6 == 0) then {
			if !(player getVariable ["DE_THROW_LOCK", false]) then {
				player setVariable ["DE_THROW_LOCK", true];
				private ["_acceptedMags","_playerMags","_magIndex"];
				_acceptedMags = getArray (configFile >> "cfgWeapons" >> currentWeapon player >> "magazines");
				_playerMags = magazinesAmmo player;
				_magIndex = _playerMags findIf { _acceptedMags find (_x # 0) > -1 };
				if (_magIndex > -1) then {
					player setVariable ["DE_THROWN_MAG",_playerMags # _magIndex];
					player forceWeaponFire ["HandGrenade_Stone","HandGrenade_Stone"];
					player addMagazine "HandGrenade_stone";
				} else {
					systemChat "You do not have an extra magazine for this weapon";
				};
			};
		};
	};
}];

//keyup UI EH - Creates a switch. Prevents the keydown code from spamming if the player holds the key down
(findDisplay 46) displayAddEventHandler ["KeyUp", {
	if ((_this select 1) == 35) then {
		player setVariable ["DE_THROW_LOCK", false];
	};
}];

//fired EH - Does all the cool stuff
//["B Alpha 1-1:1 (DreadedEntity)", "Throw", "HandGrenade_Stone", "HandGrenade_Stone", "GrenadeHand_stone", "HandGrenade_Stone", "1779995: handgrenade.p3d"];
player addEventHandler ["Fired", {
	params ["_unit", "_weapon", "_muzzle", "_mode", "_ammo", "_magazine", "_projectile", "_gunner"];
	if (_weapon == "Throw") then {
		if (_muzzle == "HandGrenade_Stone") then {
			private _thrownMag = player getVariable "DE_THROWN_MAG";
			if (!isNil "_thrownMag") then {
				//Only if all these are true can be assume mag was thrown
				private ["_pos","_vel","_mag"];
				_pos = getPosASL _projectile;
				_vel = velocity _projectile;
				deleteVehicle _projectile;
				_mag = createVehicle ["WeaponHolderSimulated", ASLToAGL _pos, [], 0, "CAN_COLLIDE"];
				_mag addMagazineAmmoCargo [_thrownMag # 0, 1, _thrownMag # 1];
				player removeMagazineGlobal (_thrownMag # 0);
				_mag setVelocity _vel;

				_id = addMissionEventHandler ["EachFrame", {
					private ["_mag","_thrownMag","_near","_manIndex"];
					_mag = _thisArgs # 0;
					_thrownMag = _thisArgs # 1;
					_near = (_mag nearEntities ["Man", 3]) select { _x != player };
					_manIndex = _near findIf { isNull objectParent _x }; //only throw to soldiers on foot :)
					if (_manIndex > -1) then {
						private _unit = _near # _manIndex;
						private _mass = getNumber (configFile >> "CfgMagazines" >> _thrownMag # 0 >> "mass");
						if ((loadAbs _unit) + _mass <= maxLoad _unit) then {
							{
								if (_x != objNull) then {
									if ((loadAbs _x) + _mass <= maxLoad _x) then {
										deleteVehicle _mag;
										_unit addMagazine [_thrownMag # 0, _thrownMag # 1];
										removeMissionEventHandler [_thisEvent, _thisEventHandler];
										break;
									};
								};
							} forEach [uniformContainer _unit, vestContainer _unit, backpackContainer _unit];
						};
					};
					if (speed _mag == 0) then {
						_mag setPosATL (ASLToATL (getPosASL _mag));
						removeMissionEventHandler [_thisEvent, _thisEventHandler];
					};
				}, [_mag, _thrownMag]];
			};
		};
	};
}];

 

Now with 100% more MP compatibility *Mic drop*

 

Github mirror

dudeeeeeeeee!'!!!!!!!!

 

how are you? glad to hear from you.

Hope you doing good

<3

Share this post


Link to post
Share on other sites

this is for DE coming back,

real GURU

 

Share this post


Link to post
Share on other sites

Don't underestimate yourself, you are good enough to use this as script and you won't need to be constantly checking if everyone has the same file than you!!!

Speed Test

  • Thanks 1

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×