Jump to content
davidoss

[Code Snippet] - Grab all dropped gear into nearest vehicle

Recommended Posts

Function which allow to grab all dropped gear into  nearest vehicle.

fnc_collector = {

	private [
		"_veh", "_holders", "_nearDead", "_droppedguns", "_droppedbags", "_eqweapons",
		 "_eqvests", "_eqbackpacks", "_eqitems", "_eqhelmets", "_mags", "_unnest",
		 "_remEmpty", "_weapons", "_vests", "_backpacks", "_items", "_guns", "_helmets",
		 "_stealedBags", "_torem"
	 ];

	_veh =  nearestObject [player, "LandVehicle"];
	if (
		isNull _veh ||
		{(_veh distance player) > 50 ||
			{!(getNumber (configFile >> "CfgVehicles" >> typeOf _veh >> "hasDriver") isEqualTo 1)}
		}
	) exitWith {hint "You need any land vehicle near to collect gear"};
		
	_holders = [];
	_nearDead = [];
	_droppedguns = [];
	_droppedbags = [];
	_eqweapons = [];
	_eqvests = [];
	_eqbackpacks = [];
	_eqitems = [];
	_eqhelmets = [];
	_mags = [];

	_unnest = {
		private ["_unnested", "_ignored"];       
		_ignored = ["ItemMap", "ItemCompass", "ItemWatch"];
		_unnested = [];                
		{           
			{
				_unnested pushBack _x;
			} forEach _x;                
		} forEach _this;
		_unnested = _unnested - _ignored;        
		_unnested
	};
			
	_remEmpty = {
		private ["_unnested", "_ignored"];           
		_ignored = [""];
		_unnested = _this - _ignored;
	_unnested
	};
			
	{_holders pushBack _x} forEach (nearestObjects [player, ["WeaponHolder", "WeaponHolderSimulated", "GroundWeaponHolder"], 800]);
	{if ((_x distance player) <= 800) then {_nearDead pushBack _x}} forEach allDeadMen;
	
	if (_holders isEqualTo [] && _nearDead isEqualTo []) exitWith {hint "There is nothing to collect"};
	
	{
		_droppedguns pushBack ((getWeaponCargo _x) select 0);
		if ((count (getBackpackCargo _x select 0)) == 1) then {
			_droppedbags pushBack ((getBackpackCargo _x) select 0);
		};
	} forEach _holders;
	{
		_eqweapons pushBack ((weapons _x) select 0);
		_eqvests pushBack (vest _x);
		_eqbackpacks pushBack (backpack _x);
		_eqitems pushBack (assignedItems _x);
		_eqhelmets pushBack (headgear _x);
	} forEach _nearDead;
	_weapons = _eqweapons call _remEmpty;
	_vests = _eqvests call _remEmpty;
	_backpacks = _eqbackpacks call _remEmpty;
	_items = _eqitems call _unnest;
	_guns = _droppedguns call _unnest;
	_helmets = _eqhelmets call _remEmpty;
	_stealedBags = _droppedbags call _unnest;
	{{_mags pushBack _x} forEach getArray (configFile >> "CfgWeapons" >> _x >> "magazines")} forEach _guns;
	{_veh addItemCargoGlobal [_x,1]} forEach (_items + _helmets + _vests);
	{_veh addWeaponCargoGlobal [_x,1]} forEach (_weapons + _guns);
	{_veh addBackpackCargoGlobal [_x,1]} forEach (_backpacks + _stealedBags);
	{_veh addMagazineCargoGlobal [_x,3]} forEach _mags;

	_torem = _nearDead + _holders;
	{deleteVehicle _x} forEach _torem;
	hint format ["Dropped gear collected into nearest vehicle: %1", typeOf _veh];
};
  • Like 5
  • Thanks 1

Share this post


Link to post
Share on other sites

great bit of code this - was looking to write something similar but stumbled on this - good thing it is in your signature on the forums!!

Share this post


Link to post
Share on other sites

You might want to look into using count instead of forEach. That way you could improve the performance of this script instantaneously.

If the code inside the count-loop should needlessly return anything, just add a nil; at the end of the loop.

 

Additionally: Using the private keyword instead of private ["_abc","_xyz"]; saves up on additional execution time and thus performance.

 

Something that helped me a lot is this code optimisation guide.

Share this post


Link to post
Share on other sites

Thanks but I do some tests and its not that bad

there are the result:

 

172 holder objects
148 dead bodies

code execution time 62ms which is 0,062 s

  • Like 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

×