Jump to content
NeoArmageddon

[Script] Copy a units loadout

Recommended Posts

Heyho,

yesterday I was in need for a script to copy a units complete loadout to another. I searched the forums and didn't find anything so I wrote a script myself. Because I think this script could be quite handy I want to share it with everyone.

The script is able to copy the complete loadout with all vest/uniform/backpack content, loaded magazines and attachments to another unit.

The script takes three arguments:

A unit to paste the gear, a unit to copy the gear from and an optional parameter for restoring the magazines ammocount.

Here is the script:

/*
Author: NeoArmageddon
Website: www.modfact.net

Description:
Copy the complete loadout of a unit to another. Allcontainers, items, gear and magazines (with ammocount) are preserved.


License:
Feel free to use this script in your mission/addon/script. But be fair and give me some credits of you release something using this script or a derivative of it.

Parameter(s):
	_this select 0: UNIT - Unit to receive the loadout
	_this select 1: UNIT - Unit to copy the loadout
	_this select 2: BOOL (optional) - false: Container of magazines is preserved but ammocount is resetted
									  true: The ammocount of every magazine is preserved but the information about the container is lost

Returns:
Nothing
*/


private["_u1","_u2","_weapons","_assigned_items","_primary","_array","_blacklist","_magazinesAmmoFull","_keep_ammocount"];

_u1 = [_this,0,objnull,[objnull]] call bis_fnc_param;
_u2 = [_this,1,objnull,[objnull]] call bis_fnc_param;
_keep_ammocount = [_this,2,false,[true]] call bis_fnc_param;

if (isnull _u1) exitwith {
["Missing first parameter for gear copy!"] call BIS_fnc_error;
};
if (isnull _u2) exitwith {
["Missing second parameter for gear copy!"] call BIS_fnc_error;
};

_primary = primaryWeapon _u2;
_weapons = weaponsItems _u2;

removeAllAssignedItems _u1;
removeAllContainers _u1;
removeAllWeapons _u1;

removeHeadgear _u1;
if((headgear _u2)!="") then {
_u1 addHeadgear (headgear _u2);
};
removeGoggles _u1;
if((goggles _u2)!="") then {
_u1 addGoggles (goggles _u2);
};
//if(true) exitwith {};
if((uniform _u2)!="") then {
_u1 adduniform(uniform _u2);
};
if((vest _u2)!="") then {
_u1 addvest (vest _u2);
};
if((backpack _u2)!="") then {
_u1 addbackpack (backpack _u2);
};

//This keeps the correct amount of ammo in every magazine but the correct distribution will be lost
if(_keep_ammocount) then {
{
	_u1 addmagazine [(_x select 0),(_x select 1)];
} foreach magazinesAmmoFull _u2;
};


_blacklist = ["Rangefinder","Binocular"];
{

if((_x select 0)!="" && !((_x select 0) in _blacklist)) then {

	//This will add the current loaded magazine in the weapon
	if(!_keep_ammocount) then {
		if(count(_x)>4) then {
			_u1 addmagazine [(_x select 4) select 0,(_x select 4) select 1];
		};
		if(count(_x)>5) then {
			_u1 addmagazine [(_x select 5) select 0,(_x select 5) select 1];
		};
	};
	_u1 addweapon (_x select 0);
	_u1 linkitem (_x select 1);
	_u1 linkitem (_x select 2);
	_u1 linkitem (_x select 3);
};
} foreach (weaponsItems _u2);

_array = getItemCargo uniformContainer _u2;
if(count(_array)>0) then {
{
	for[{_i=0},{_i<((_array) select 1) select _forEachIndex},{_i=_i+1}] do {
		_u1 addItemToUniform _x;
	};
} foreach ((_array) select 0);
};
//This will restore the the correct magazine amount in each container
if(!_keep_ammocount) then {
_array = getMagazineCargo uniformContainer _u2;
if(count(_array)>0) then {
	{
		for[{_i=0},{_i<((_array) select 1) select _forEachIndex},{_i=_i+1}] do {
			_u1 addItemToUniform _x;
		};
	} foreach ((_array) select 0);
};
};
_array = getWeaponCargo uniformContainer _u2;
if(count(_array)>0) then {
{
	for[{_i=0},{_i<((_array) select 1) select _forEachIndex},{_i=_i+1}] do {
		_u1 addItemToUniform _x;
	};
} foreach ((_array) select 0);
};

_array = getItemCargo vestContainer _u2;
if(count(_array)>0) then {
{
	for[{_i=0},{_i<((_array) select 1) select _forEachIndex},{_i=_i+1}] do {
		_u1 addItemToVest _x;
	};
} foreach ((_array) select 0);
};
if(!_keep_ammocount) then {
_array = getMagazineCargo vestContainer _u2;
if(count(_array)>0) then {
	{
		for[{_i=0},{_i<((_array) select 1) select _forEachIndex},{_i=_i+1}] do {
			_u1 addItemToVest _x;
		};
	} foreach ((_array) select 0);
};
};
_array = getWeaponCargo vestContainer _u2;
if(count(_array)>0) then {
{
	for[{_i=0},{_i<((_array) select 1) select _forEachIndex},{_i=_i+1}] do {
		_u1 addItemToBackpack _x;
	};
} foreach ((_array) select 0);
};


_array = getItemCargo backpackContainer _u2;
if(count(_array)>0) then {
{
	for[{_i=0},{_i<((_array) select 1) select _forEachIndex},{_i=_i+1}] do {
		_u1 addItemToBackpack _x;
	};
} foreach ((_array) select 0);
};
if(!_keep_ammocount) then {
_array = getMagazineCargo backpackContainer _u2;
if(count(_array)>0) then {
	{
		for[{_i=0},{_i<((_array) select 1) select _forEachIndex},{_i=_i+1}] do {
			_u1 addItemToBackpack _x;
		};
	} foreach ((_array) select 0);
};
};
_array = getWeaponCargo backpackContainer _u2;
if(count(_array)>0) then {
{
	for[{_i=0},{_i<((_array) select 1) select _forEachIndex},{_i=_i+1}] do {
		_u1 addItemToBackpack _x;
	};
} foreach ((_array) select 0);
};

{
_u1 linkItem _x;
} foreach assignedItems _u2;

_u1 selectWeapon (currentWeapon _u2);

Feel free to use it for your mission/addon/script. I just want to ask for some credits if you release something using this script.

Thr script can be executed with execVM or loaded as a function.

Feedback is highly appreciated!

Share this post


Link to post
Share on other sites

Thank you for this, it will go down well with creating loadouts for my singleplayer missions. :)

I've been getting tired of the havoc of manually coding in new loadouts, LEA is too complicated for me in Arma 3 and I wanted a script I could use to copy a loadout after I custom equip my unit using VAS and/or other ammo boxes. I like equiping my unit in game with the visual aspect of it, easily testing weapons and getting a feel etc.

Share this post


Link to post
Share on other sites

I tried your script and i dont get any of the attachments for for the weapons, but just added the command "primaryWeaponItems"  and some other stuff to complement the script.

My script:  (Unstuck AI)

Spoiler

_group = (group player);


    _CloneDude= 
    {    
        _dude                 = _this select 0;
        _Currentpos           = getpos _dude;
        _Callingname          = nameSound _dude;
        _TheName              = name _dude;
        _TheFace              = face _dude;
        _TheVoce              = speaker _dude;
        _TheClass             = typeOf _dude;
        _clonePos             = [(random [-100,0,100]),(random [-100,0,100]),0];
        _TheattachmentsMain   = primaryWeaponItems _dude;
        _TheattachmentsSecond = secondaryWeaponItems _dude;
        
        _newunit = group player createUnit [_TheClass, _clonePos, [], 0, "FORM"];
        _newunit setNameSound _Callingname;
        _newunit setName _TheName;
        _newunit setFace _TheFace;
        _newunit setSpeaker _TheVoce;
        
        [_newunit,_dude,true] call fn_CopyLoadout;                       //Calls your script
        sleep 0.2;
        
        for "_i" from 0 to ((count _TheattachmentsMain) - 1) do                //Then Adds attachments
        {
        _Test1 = _TheattachmentsMain select _i;                    
        _newunit addPrimaryWeaponItem _Test1;
        };
    
        for "_i" from 0 to ((count _TheattachmentsSecond) - 1) do
        {
        _Test2 = _TheattachmentsSecond select _i;
        _newunit addSecondaryWeaponItem _Test2;
        sleep 0.1;
        };

    
        deleteVehicle _dude;
        _newunit setpos [(_Currentpos select 0)+(random [-10,0,10]),(_Currentpos select 1)+(random [-10,0,10]),(_Currentpos select 2)];
        sleep 0.1;
        [_newunit] joinSilent (group player);
    };


for "_i" from 1 to ((count units _group) -1) do
{
    _dude = (units _group) select _i;
    [_dude] spawn _CloneDude;
    sleep 0.5;
};


hint "Unstuck used";
 


Because i am too lazy to try and integrate those commands in your script i am just adding them after you script is done.

Share this post


Link to post
Share on other sites

Question, what's the difference between your script and doing the following:

player setUnitLoadout (getUnitLoadout Bob);

Just wondering is all. Nice work though.

Share this post


Link to post
Share on other sites
1 minute ago, HazJ said:

Question, what's the difference between your script and doing the following:


player setUnitLoadout (getUnitLoadout Bob);

Well my script is mostly for use when you want to unstuck AI squad mates, especially when the AI refuses to move or react.

But damn if google hade found command "setUnitLoadout" then i would have saved about 4 hours. But i guess i learnd something new.

Share this post


Link to post
Share on other sites

I meant the code I shown and NeoArmageddon's script. (:

Share this post


Link to post
Share on other sites

Oh sorry

More or less does the same thing as "setUnitLoadout", atleast acording to the commands wiki.
Going to try use tomorrow and see what happens with "setUnitLoadout".

Share this post


Link to post
Share on other sites
4 hours ago, FederalRazer89 said:

But damn if google hade found command "setUnitLoadout" then i would have saved about 4 hours. But i guess i learnd something new.

 

You may save even more time learning if you don't dig up threads over 3 years old

  • Like 1

Share this post


Link to post
Share on other sites

Damn... I didn't even realise that. That explains why he made the function. I don't think those commands were around then.

Share this post


Link to post
Share on other sites
On 14.8.2017 at 10:43 AM, HazJ said:

Damn... I didn't even realise that. That explains why he made the function. I don't think those commands were around then.

 

Exactly. But thanks for bringing "setUnitLoadout" to my attention^^

*Goes updating some missions*

  • 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

×