Jump to content
wyattwic

Adding a weapon with attachments to a box

Recommended Posts

Hello all!

 

I am working on writing a script that saves a box's inventory in its entirety, then restores it.

 

My biggest stall out is weapons, their attachments and loaded mags.  Right now I am saving the weapons in the box using weaponsItems - I am unsure how to restore the weapon and its attachments/mags (including the number of rounds in that loaded mag).  Tips would be appreciated!

 

 

Share this post


Link to post
Share on other sites

Not sure if there's already an easier way, but you can spawn a dummy unit at [0,0,0], give him the weapon, items and magazine with custom ammo count, then let the dummy drop the weapon into the crate using:

_unit action ["PutWeapon", _YourCrate, currentWeapon _unit];

Put a 1-2 second sleep after the action, so the unit has time to put the weapon into the crate (doesn't have to be anywhere near) and finally delete the dummy.

 

Cheers

  • Like 1

Share this post


Link to post
Share on other sites

I have written functions to do this before. Ill just say, this is a painful thing to do. 

These functions will only work together. I don't know if these are the optimal way to make it work, but this is what I had to do, and from memory there is one or two edge case bugs. Maybe you can improve on this.
 

Spoiler

params ["_box"];

//Get items
private _weapons = getWeaponCargo _box; 
private _items = getItemCargo _box; 
private _magazines = magazinesAmmoCargo _box; 
private _backpacks = getbackpackcargo _box;

private _c_weapons = [];
private _c_items = [];
private _c_magazines = [];

//Condenses magazines into 2 arrays, full and partial
private _fn_condenseMagazines = {

	params ["_magazines"];
	
	_magazines sort true;
	private _prevSelected = "";
	private _magazinesPartial = [];
	private _fullMagazines = [[],[]];
	
	{
		
		private _selected = _x select 0;
	
		
		if !(_selected isEqualto _prevSelected) then {
			
			
			private _ammo = _x select 1;
			private _capacity = getNumber (configFile >> "CfgMagazines" >> _selected >> "count");
				
			private _count = {_x isEqualto [_selected, _capacity]} count _magazines;
		
			_fullMagazines select 0 pushback _selected;
			_fullMagazines select 1 pushback _count;
			
		};
		
		_prevSelected = _x select 0;
			
	} foreach _magazines;
	
	{
		
		private _capacity = getNumber (configFile >> "CfgMagazines" >> _x select 0 >> "count");
		
		if (_x select 1 isEqualto  _capacity) then {_magazines = _magazines - [_x]};
	
	} foreach _magazines;

	[_magazines, _fullMagazines];
	
};

//Returns all relevant weapon attachments
private _fn_getWeaponItems = {

	params ["_weaponsArray"];
	
	private _weaponItems = [];
	private _weaponCased = [];

	{ 

		private _weapon = _x; 
		private _typeOf = _x select 0; 
		
		//if magazine exists
		if !((_weapon select 4) isEqualTo []) then {
		
			//Separate magazine
			(_condensedMagazines select 0) pushback (_weapon select 4);
		
		};
		
		//Remove magazine and empty attachments
		_weapon deleteAt 4; 
		_weapon deleteAt 0; 
		_weapon = _weapon - [""]; 
		
		//Stop case sensitive errors
		{
		
			private _selected = toLower _x;
			
			_weaponCased pushback _selected;
		
		} foreach _weapon;
		
		//Retrieve config items to compare
		private _configuredItems = ["LinkedItemsOptic", "LinkedItemsMuzzle", "LinkedItemsAcc", "LinkedItemsUnder"] apply { 
		
			getText (configFile >> "CfgWeapons" >> _typeOf >> "LinkedItems" >> _x >> "Item") 
		
		};
		
		//get config values of linked items, remove them if theyre configured
		private _config =  (configFile >> "CfgWeapons" >> _typeOf >> "LinkedItems"); 
		
		//This ones a mind fuck, see ian
		private _configuredItems = ["LinkedItemsOptic", "LinkedItemsMuzzle", "LinkedItemsAcc", "LinkedItemsUnder"]
		apply { (_config >> _x >> "item") } select { isText _x && { getText _x != "" } } apply { getText _x };

		{
			//Stop case sensitive errors
			private _selected = tolower _x;
			
			if (_selected in _weaponCased) then {_weaponCased = _weaponCased - [_selected]};
		
		} foreach _configuredItems;
		
		{_weaponitems pushback _x} foreach _weaponCased;

	} foreach _weaponsArray; 
	
	_weaponItems; 

};

//Setup array
private _containerArray = [[[],[]], [[],[]], [], [[],[]], [[],[]]]; 
private _ObjectArray = []; 

//Sort and append all contained items
private _containers = everyContainer _box;

if !(count _containers isEqualto 0) then {

	{

		private _object = _x select 1; 
			
		_c_weapons = getWeaponCargo _object; 
		_c_items = getItemCargo _object; 
		_c_magazines = MagazinesAmmoCargo _object;
	
		//Condense magazines, get weapon items
		private _c_condensedMagazines = [_c_magazines] call _fn_condenseMagazines;
		private _c_weaponItems = [weaponsItemsCargo _Object] call _fn_getWeaponItems;

		if !(_c_weaponItems isEqualto []) then {

			{
				_c_items select 0 pushback _x;
				_c_items select 1 pushback 1;

			} foreach _c_weaponItems;

		};
		
		_objectArray set [0, _c_weapons];
		_objectArray set [1, _c_items];
		_objectArray set [2, _c_condensedMagazines select 0];
		_objectArray set [3, _c_condensedMagazines select 1];
	
		{ 
			
			if (_x isequalto 2) then { 

				_containerArray set [2,_objectArray select 2]; 

			} else { 

				(_containerArray select _x select 0) append (_objectArray select _x select 0);

				(_containerArray select _x select 1) append (_objectArray select _x select 1);

			 
			}; 

		} foreach [0,1,2,3]; 
 
	} foreach _containers; 
					
};

//Condense normal magazines into [magazines, fullmagazines] & get weapon items
_condensedMagazines = [_magazines] call _fn_condenseMagazines;
private _weaponItems = [weaponsItemsCargo _box] call _fn_getWeaponItems;

if !(_weaponItems isEqualto []) then {

	{
			_items select 0 pushback _x;
			_items select 1 pushback 1;

	} foreach _weaponItems;

};

private _contentsArray = [_weapons, _items, _condensedMagazines select 0, _condensedMagazines select 1, _backpacks]; 

//Return
[_contentsArray, _containerArray];

 

Spoiler

params ["_box", "_totalArray", "_erase"];

if (isnil "_erase") then {_erase = true};

if !(_erase isEqualType true) then {_erase = true};

//Clear box, this is default
if (_erase) then {

	clearItemCargoGlobal _box;
	clearweaponCargoGlobal _box;
	clearmagazineCargoGlobal _box;
	clearbackpackCargoGlobal _box;
	
};

//Split array
private _contentsArray = _totalArray select 0;
private _c_Array = _totalArray select 1;

//Normal items
private _weapons = _contentsArray select 0;
private _items = _contentsArray select 1;
private _magazines = _contentsArray select 2;
private _fullMagazines = _contentsArray select 3;
private _backpacks = _contentsArray select 4;

//Contained Items
private _c_Weapons = _c_Array select 0;
private _c_Items = _c_Array select 1;
private _c_Magazines = _c_Array select 2;
private _c_FullMagazines = _c_Array select 3;

private _fn_addweapons = { 
	
	params ["_array", "_box"];
	
	private _contentsArray = _array select 0; 
	private _number = _array select 1; 

	{ 

		_box addweaponcargoGlobal [_x, _number select _foreachindex]; 

	} foreach _contentsArray; 

};

private _fn_addItems = { 

	params ["_array", "_box"];
	
	private _contentsArray = _array select 0; 
	private _number = _array select 1; 

	{ 

		_box additemcargoGlobal [_x, _number select _foreachindex]; 

	} foreach _contentsArray; 

};

private _fn_addmagazines = { 

	params ["_magazines", "_fullMagazines_array", "_box", "_isFull"];
	
	//If fulls mags, add
	if !((_fullMagazines_array select 0) isEqualTo []) then {
	
		private _fullMagazines = _fullMagazines_array select 0;
		private _number = _fullMagazines_array select 1;
	
		{ 
						
			_box addMagazineCargo [_x, _number select _foreachindex]; 

		} foreach _fullMagazines; 
	
	};
	
	//If partial mags, add
	if !(_magazines isEqualTo []) then {
	
		{
		
			_box addMagazineAmmoCargo [(_x select 0), 1, (_x select 1)]; 

		} foreach _magazines; 
	
	};
	
};

private _fn_addbackpacks = { 

	params ["_array", "_box"];
	
	private _contentsArray = _array select 0; 
	private _number = _array select 1; 

	{ 

		_box addbackpackcargoGlobal [_x, _number select _foreachindex]; 

	} foreach _contentsArray; 

};

//Add normal items
[_weapons, _box] call _fn_addweapons;
[_items, _box] call _fn_additems;
[_magazines, _fullMagazines, _box] call _fn_addmagazines;
[_backpacks, _box] call _fn_addbackpacks;

//Add contained Items
[_c_Weapons, _box] call _fn_addweapons;
[_c_Items, _box] call _fn_additems;
[_c_Magazines, _c_fullMagazines, _box] call _fn_addmagazines;

//Clear all configured containers
{

	private _container = _x select 1;

	clearItemCargoGlobal _container; 
	clearweaponCargoGlobal _container; 
	clearmagazineCargoGlobal _container; 
	clearbackpackCargoGlobal _container; 
	
} foreach (everycontainer _box);




 


 

 

Share this post


Link to post
Share on other sites

Thanks guys!  Focusing on adding configured weapons back to the box I am following grump's suggestion.

 

The code below works, but for some reason intermittently doesn't do the dropWeapon action.  I've tried Put and Drop Weapon variations, I've also tried waiting up to 10 seconds.  Any ideas?

 

 

This code is spawned.  Coming in as _this is "[unit,[container,weaponclass,[array of attachments]]]".

params ["_thread","_process"];
		
		removeAllWeapons _thread;
		removeAllItems _thread;
		
		_process params ["_container","_weapon","_attachments"];
		
		_thread addWeapon _weapon;
		{
			_thread addWeaponItem [_weapon,_x];
		} forEach _attachments;
		
		_thread action ["PutWeapon", _container, _weapon];
		sleep 5;

 

Share this post


Link to post
Share on other sites

Only two things i can think of would be 1. Arma or 2. Locality of the AI.

Leaning more towards 1, have you tried running a loop such as..
 

while {(currentWeapon _unit != "")} do {sleep 1; //put in box again};

Just to see if its just the AI ignoring you?

  • 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

×