Jump to content
.Marti

Sort Weapons/Item/Backpacks/Magazines HELP

Recommended Posts

Hey there! 

 

I need to separate an array of various items in weapons/magazines/backpacks/items to load them to a box with addWeaponCargo, addItemCargo, etc.

 

Example of the array:

 

Quote

[["U_O_CombatUniform_ocamo",4],["U_O_CombatUniform_oucamo",4],["U_O_OfficerUniform_ocamo",4];

 

I got them with getWeaponCargo, etc. Maybe the easiest solution would be to store them in different variables when i got them?

I would be surprised if I can't classify them but i can't find nothing about it.

 

Any idea how to proceed? Thanks!

 

-------------------

 

Edit: I'm not storing the arrays directly on different variables because they are suposed to be stored in the profileNamespace of a server, and i wanted to mantain a clean enviroment, but i will do it if I need to.

Edited by .Marti
specify

Share this post


Link to post
Share on other sites

addItemCargo can also add magazines and weapons. Not sure about backpacks and the like. But no seperation neccessary.

  • Like 1

Share this post


Link to post
Share on other sites

Hey there, I have another mini-question. 

 

It is possible to sort all the types of grenades? Since its parents are ["CA_Magazine","Default"] like all the mags. 

Share this post


Link to post
Share on other sites
16 hours ago, 7erra said:

addItemCargo can also add magazines and weapons.

Suppose it depends on if you want to store weapons and their current attachments, mags and ammo count and magazines with ammo count.

Spoiler


//_container = myBox; //Some container name given in editor
_container = cursorObject;

TAG_fnc_getContents = {
	params[ "_container" ];
	
	private _cargo = [ 
		weaponsItemsCargo _container call BIS_fnc_consolidateArray,
		( itemCargo _container select{ !( toLowerANSI( _x call BIS_fnc_itemType select 1 ) in [ "backpack", "uniform", "vest" ] ) } ) call BIS_fnc_consolidateArray,
		magazinesAmmoCargo _container call BIS_fnc_consolidateArray
	];
	
	private _containerCargo = everyContainer _container;
	{
		_x params[ "_type", "_object" ];
		
		_containerCargo set[ _forEachIndex, [ _type, _object call TAG_fnc_getContents ] ];
	}forEach _containerCargo;
	
	[ _cargo, _containerCargo ]
};

//Get the contents
_containerContents = _container call TAG_fnc_getContents;

//Clear out cargo
clearItemCargoGlobal _container;
clearWeaponCargoGlobal _container;
clearBackpackCargoGlobal _container;
clearMagazineCargoGlobal _container;

TAG_fnc_setContents = {
	params[ "_container", "_contents" ];
	_contents params[ "_cargo", "_containers" ];
	
	{
		_x params[ "_cont", "_contents" ];
		
		_every = everyContainer _container;
		if ( _cont call BIS_fnc_itemType select 1 == "backpack" ) then {
			_container addBackpackCargoGlobal[ _cont, 1 ];
		}else{
			_container addItemCargoGlobal[ _cont, 1 ];
		};
		_addedContainer = ( everyContainer _container - _every ) select 0 select 1;
		
		clearItemCargoGlobal _addedContainer;
		clearWeaponCargoGlobal _addedContainer;
		clearBackpackCargoGlobal _addedContainer;
		clearMagazineCargoGlobal _addedContainer;
		
		[ _addedContainer, _contents ] call TAG_fnc_setContents;
	}forEach _containers;
	
	_fnc_addContents = {
		params[ "_index", "_details", "_count" ];
		
		switch ( _index ) do {
			//weapons
			case ( 0 ) : {
				_container addWeaponWithAttachmentsCargoGlobal[ _details, _count ];
			};
			//items
			case ( 1 ) : {
				_container addItemCargoGlobal[ _details, _count ];
			};
			//magazines
			case ( 2 ) : {
				_details params[ "_mag", "_ammo" ];
				
				_container addMagazineAmmoCargo[ _mag, _count, _ammo ];
			};
		};
	};
	
	{
		private _index = _forEachIndex;
		{
			_x params[ "_item", "_count" ];

			[ _index, _item, _count ] call _fnc_addContents;
		}forEach _x;
	}forEach _cargo;
	
};

//Add cargo back in
[ _container, _containerContents ] call TAG_fnc_setContents;

Needs thoroughly testing.

 

  • Like 3
  • Thanks 2

Share this post


Link to post
Share on other sites
On 8/23/2020 at 9:16 PM, 7erra said:

maybe this function could be useful: https://community.bistudio.com/wiki/BIS_fnc_itemType

 

 thanks another time for the tip. Guidance is always helpful!

 

On 8/23/2020 at 9:39 PM, Larrow said:

Suppose it depends on if you want to store weapons and their current attachments, mags and ammo count and magazines with ammo count.

  Reveal hidden contents



//_container = myBox; //Some container name given in editor
_container = cursorObject;

TAG_fnc_getContents = {
	params[ "_container" ];
	
	private _cargo = [ 
		weaponsItemsCargo _container call BIS_fnc_consolidateArray,
		( itemCargo _container select{ !( toLowerANSI( _x call BIS_fnc_itemType select 1 ) in [ "backpack", "uniform", "vest" ] ) } ) call BIS_fnc_consolidateArray,
		magazinesAmmoCargo _container call BIS_fnc_consolidateArray
	];
	
	private _containerCargo = everyContainer _container;
	{
		_x params[ "_type", "_object" ];
		
		_containerCargo set[ _forEachIndex, [ _type, _object call TAG_fnc_getContents ] ];
	}forEach _containerCargo;
	
	[ _cargo, _containerCargo ]
};

//Get the contents
_containerContents = _container call TAG_fnc_getContents;

//Clear out cargo
clearItemCargoGlobal _container;
clearWeaponCargoGlobal _container;
clearBackpackCargoGlobal _container;
clearMagazineCargoGlobal _container;

TAG_fnc_setContents = {
	params[ "_container", "_contents" ];
	_contents params[ "_cargo", "_containers" ];
	
	{
		_x params[ "_cont", "_contents" ];
		
		_every = everyContainer _container;
		if ( _cont call BIS_fnc_itemType select 1 == "backpack" ) then {
			_container addBackpackCargoGlobal[ _cont, 1 ];
		}else{
			_container addItemCargoGlobal[ _cont, 1 ];
		};
		_addedContainer = ( everyContainer _container - _every ) select 0 select 1;
		
		clearItemCargoGlobal _addedContainer;
		clearWeaponCargoGlobal _addedContainer;
		clearBackpackCargoGlobal _addedContainer;
		clearMagazineCargoGlobal _addedContainer;
		
		[ _addedContainer, _contents ] call TAG_fnc_setContents;
	}forEach _containers;
	
	_fnc_addContents = {
		params[ "_index", "_details", "_count" ];
		
		switch ( _index ) do {
			//weapons
			case ( 0 ) : {
				_container addWeaponWithAttachmentsCargoGlobal[ _details, _count ];
			};
			//items
			case ( 1 ) : {
				_container addItemCargoGlobal[ _details, _count ];
			};
			//magazines
			case ( 2 ) : {
				_details params[ "_mag", "_ammo" ];
				
				_container addMagazineAmmoCargo[ _mag, _count, _ammo ];
			};
		};
	};
	
	{
		private _index = _forEachIndex;
		{
			_x params[ "_item", "_count" ];

			[ _index, _item, _count ] call _fnc_addContents;
		}forEach _x;
	}forEach _cargo;
	
};

//Add cargo back in
[ _container, _containerContents ] call TAG_fnc_setContents;

Needs thoroughly testing.

 

 

Thanks so much,  Larrow! Much appreciated, will test it out. 

 

That kind of sensation when you spent whole days trying to do something and it's clearly worst done than your code there. At least i learned a lot hahah

 

 

EDIT: Tested and working propperly.

 

Share this post


Link to post
Share on other sites

I have tried to implement this into a BECTI mission to save crates to profilenamespace making them persistant.

The TAG functions work perfectly BTW.  Thanks for sharing.

my problem is making eventhandlers public while keeping the variables private.

 

I am running this code each time a crate it made

//this works fine with one crate but due to public variable 
//it gets messed up when i add more
//When i try to make variables private it fails
//any ideas?
if (_defense isKindOf "B_supplyCrate_F") then {
    clearBackpackCargoGlobal _defense;
    clearItemCargoGlobal _defense;
    clearMagazineCargoGlobal _defense;
    clearWeaponCargoGlobal _defense;


    container=_defense;

    //create array to hold the contents of all containers
    //add each container to it as they are made
    allcontainercontents pushBack [container, nil];


    //add event to all computers to report when contents are changed
    //yes i know this should be remoteexec but having syntax issues.
    container addEventHandler ["ContainerClosed", {["A_ContainerClosed", container] call CBA_fnc_serverEvent;}];
    //["ContainerClosed", {["A_ContainerClosed", container] call CBA_fnc_serverEvent;}] remoteExec ["addEventHandler", 0, true];


    //server watches for event and saves data to array
    ["A_ContainerClosed", {
        params ["container", "_unit"];
        // find the contents in container
        containerContents = container call TAG_fnc_getContents;
        //find container in array and add the contents to it

        {
            if (allcontainercontents select _foreachindex select 0 == container) then
            {
                allcontainercontents set [_foreachindex, [container, containerContents]];
            };
        } foreach allcontainercontents;
    }] call CBA_fnc_addEventHandler;
};

Share this post


Link to post
Share on other sites
On 8/23/2020 at 9:39 PM, Larrow said:

Suppose it depends on if you want to store weapons and their current attachments, mags and ammo count and magazines with ammo count.

  Hide contents



//_container = myBox; //Some container name given in editor
_container = cursorObject;

TAG_fnc_getContents = {
	params[ "_container" ];
	
	private _cargo = [ 
		weaponsItemsCargo _container call BIS_fnc_consolidateArray,
		( itemCargo _container select{ !( toLowerANSI( _x call BIS_fnc_itemType select 1 ) in [ "backpack", "uniform", "vest" ] ) } ) call BIS_fnc_consolidateArray,
		magazinesAmmoCargo _container call BIS_fnc_consolidateArray
	];
	
	private _containerCargo = everyContainer _container;
	{
		_x params[ "_type", "_object" ];
		
		_containerCargo set[ _forEachIndex, [ _type, _object call TAG_fnc_getContents ] ];
	}forEach _containerCargo;
	
	[ _cargo, _containerCargo ]
};

//Get the contents
_containerContents = _container call TAG_fnc_getContents;

//Clear out cargo
clearItemCargoGlobal _container;
clearWeaponCargoGlobal _container;
clearBackpackCargoGlobal _container;
clearMagazineCargoGlobal _container;

TAG_fnc_setContents = {
	params[ "_container", "_contents" ];
	_contents params[ "_cargo", "_containers" ];
	
	{
		_x params[ "_cont", "_contents" ];
		
		_every = everyContainer _container;
		if ( _cont call BIS_fnc_itemType select 1 == "backpack" ) then {
			_container addBackpackCargoGlobal[ _cont, 1 ];
		}else{
			_container addItemCargoGlobal[ _cont, 1 ];
		};
		_addedContainer = ( everyContainer _container - _every ) select 0 select 1;
		
		clearItemCargoGlobal _addedContainer;
		clearWeaponCargoGlobal _addedContainer;
		clearBackpackCargoGlobal _addedContainer;
		clearMagazineCargoGlobal _addedContainer;
		
		[ _addedContainer, _contents ] call TAG_fnc_setContents;
	}forEach _containers;
	
	_fnc_addContents = {
		params[ "_index", "_details", "_count" ];
		
		switch ( _index ) do {
			//weapons
			case ( 0 ) : {
				_container addWeaponWithAttachmentsCargoGlobal[ _details, _count ];
			};
			//items
			case ( 1 ) : {
				_container addItemCargoGlobal[ _details, _count ];
			};
			//magazines
			case ( 2 ) : {
				_details params[ "_mag", "_ammo" ];
				
				_container addMagazineAmmoCargo[ _mag, _count, _ammo ];
			};
		};
	};
	
	{
		private _index = _forEachIndex;
		{
			_x params[ "_item", "_count" ];

			[ _index, _item, _count ] call _fnc_addContents;
		}forEach _x;
	}forEach _cargo;
	
};

//Add cargo back in
[ _container, _containerContents ] call TAG_fnc_setContents;

Needs thoroughly testing.

 

Just thank you, @Larrow You're a mastermind. 😄

Lil' beer incoming.

  • Thanks 1

Share this post


Link to post
Share on other sites

I have another question here that has me racking my brains... @Larrow's code copies the subcontainers (uniforms, vests, backpacks) and their content to the end container (crate). The stuff stays in the subcontainers.

 

How would the code have to look like when you want to empty ("pour out", so to speak) the subcontainers into the crate, so that the contents of the uniforms, vests and backpacks are directly in the crate, not in the subcontainers anymore?

 

I hope my question is understandable. 😅

 

 

 

Share this post


Link to post
Share on other sites
On 8/23/2020 at 8:39 PM, Larrow said:

[ _addedContainer, _contents ] call TAG_fnc_setContents;

Just change this line in TAG_fnc_setContents to...

[ _container, _contents ] call TAG_fnc_setContents;

So the original container ( the crate ) rather than the added sub-container.

  • Thanks 1

Share this post


Link to post
Share on other sites

 

2 hours ago, Larrow said:

Just change this line in TAG_fnc_setContents to...


[ _container, _contents ] call TAG_fnc_setContents;

So the original container ( the crate ) rather than the added sub-container.

 

It worked like magic! Thanks a lot, man! 

I would never have thought that only one variable had to be changed. 😄

Share this post


Link to post
Share on other sites

Update to allow emptying of sub-containers and weapons attachments into the main container, rather than copying them over as is.

Spoiler


//_container = myBox; //Some container name given in editor
_container = cursorObject;

TAG_fnc_getContents = {
	params[ "_container" ];

	private _cargo = [
		weaponsItemsCargo _container call BIS_fnc_consolidateArray,
		( itemCargo _container select{ !( toLowerANSI( _x call BIS_fnc_itemType select 1 ) in [ "backpack", "uniform", "vest" ] ) } ) call BIS_fnc_consolidateArray,
		magazinesAmmoCargo _container call BIS_fnc_consolidateArray
	];

	private _containerCargo = everyContainer _container;
	{
		_x params[ "_type", "_object" ];

		_containerCargo set[ _forEachIndex, [ _type, _object call TAG_fnc_getContents ] ];
	}forEach _containerCargo;

	[ _cargo, _containerCargo ]
};

//Get the contents
_containerContents = _container call TAG_fnc_getContents;

//Clear out cargo
clearItemCargoGlobal _container;
clearWeaponCargoGlobal _container;
clearBackpackCargoGlobal _container;
clearMagazineCargoGlobal _container;

TAG_fnc_setContents = {
	params[ "_container", "_contents", [ "_dis", [ false, false ], [ false, [] ], 2 ] ];
	_contents params[ "_cargo", "_containers" ];
	if ( _dis isEqualType false ) then {
		_dis = [ _dis, _dis ];
	};
	_dis params[ "_disContainers", "_disWeapons" ];

	{
		_x params[ "_cont", "_contents" ];

		_every = everyContainer _container;
		if ( _cont call BIS_fnc_itemType select 1 == "backpack" ) then {
			_container addBackpackCargoGlobal[ _cont, 1 ];
		}else{
			_container addItemCargoGlobal[ _cont, 1 ];
		};
		_addedContainer = ( everyContainer _container - _every ) select 0 select 1;

		clearItemCargoGlobal _addedContainer;
		clearWeaponCargoGlobal _addedContainer;
		clearBackpackCargoGlobal _addedContainer;
		clearMagazineCargoGlobal _addedContainer;

		[ [ _addedContainer, _container ] select _disContainers, _contents, _dis ] call TAG_fnc_setContents;
	}forEach _containers;

	_fnc_addContents = {
		params[ "_index", "_details", "_count" ];

		switch ( _index ) do {
			//weapons
			case ( 0 ) : {
				if ( _disWeapons ) then {
					_details params[ "_weapon", "_muzzle", "_flashlight", "_optics", "_priMuzzle", "_secMuzzle", "_bipod" ];

					_baseWeapon = [ _weapon ] call BIS_fnc_baseWeapon;
					//Add base weapon with no mags
					_container addWeaponWithAttachmentsCargoGlobal[ [ _baseWeapon, "", "", "", [], [], "" ], _count ];

					//Add magazines
					{
						_x params[ "_mag", "_ammo" ];

						_container addMagazineAmmoCargo[ _mag, _count, _ammo ];
					}forEach [ _priMuzzle, _secMuzzle ];

					//Add any weapon items that do not come with base weapon
					_baseWeaponItems = configFile >> "CfgWeapons" >> _baseWeapon >> "LinkedItems";
					{
						_x params[ "_item", "_linkSlot" ];

						if ( !( _item == "" ) && { !( _item == getText( _baseWeaponItems >> _linkSlot >> "item" )) } ) then {
							_container addItemCargoGlobal[ _item, _count ];
						};
					}forEach [
						[ _muzzle, "LinkedItemsMuzzle" ],
						[ _flashlight, "LinkedItemsAcc" ],
						[ _optics, "LinkedItemsOptic" ],
						[ _bipod, "LinkedItemsUnder" ]
					];

				}else{
					_container addWeaponWithAttachmentsCargoGlobal[ _details, _count ];
				};
			};
			//items
			case ( 1 ) : {
				_container addItemCargoGlobal[ _details, _count ];
			};
			//magazines
			case ( 2 ) : {
				_details params[ "_mag", "_ammo" ];

				_container addMagazineAmmoCargo[ _mag, _count, _ammo ];
			};
		};
	};

	{
		private _index = _forEachIndex;
		{
			_x params[ "_item", "_count" ];

			[ _index, _item, _count ] call _fnc_addContents;
		}forEach _x;
	}forEach _cargo;

};

//Add cargo back in
[ _container, _containerContents, false ] call TAG_fnc_setContents;

/*
[ container, contents, [ dis sub containers, dis weapon attachments ] ] call TAG_fnc_setContents

[ _container, _containerContents, true ] call TAG_fnc_setContents; //Dissasemble all items from sub-containers and weapon attachments into the container
[ _container, _containerContents, [ true, false ] ] call TAG_fnc_setContents; //Dissasemble all items from sub-containers but not weapon attachments into the container
/*

I think that works out correctly, untested.

  • Like 2

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

×