Jump to content
Blitzen88

SpawnAI Module Loadout Script Creates Duplicate Backpacks..?

Recommended Posts

Having some issues with a loadout script I use in combination with the Vanilla Spawn AI Module. The script redresses and re-equips units with RHS equipment (in this case M16s) once they spawn. However, the script seems to create duplicate/multiple backpacks for units - the unused backpack is left to sit on the ground. The script is run in singleplayer. 
 

How can I fix this?

 

Loadout Script:

Spoiler

/*==========================================================================================

 

                                                                Arma III - RHS M16 Loadout

 

Created by Blitzen

 

===========================================================================================

 

* Infantry units spawned by an AI Spawn Module which is linked to the Sector Tactics Module are stripped of their loadout/weapon attachments

 

* Infantry units spawned by an AI Spawn Module which is NOT linked to the Sector Tactics Module are NOT stripped of their loadout/weapon attachments

 

* This script re-applys the unit's default loadout and weapon attachments

 

* After the unit's default loadout is applied, the script re-arms the unit with a different weapon

 

* Loadouts are only applied to units types which are defined below

 

* Compatible Units:

 

                - Squadleader                                                    - Rifleman AT

 

                - Team Leader                                                   - Ammobearer

 

                - Marksman                                                        - Autorifleman

 

                - Medic                                                                 - Grenadier

 

                - Assistant Missle Specialist                          - Missle Specialist

 

                - Heavy Gunner                                                - Crewman

 

                - Rifleman                                                           - Assistant Gunner

 

                - Gunner (GMG)                                                               - Gunner (HMG)

 

* Call with (Unit Init): {_x execVM "Scripts\Loadouts\RHS\Loadout_RHS_M16.sqf"} foreach units group this;

 

* Call with (Sector Module): _this execVM "Scripts\Loadouts\RHS\Loadout_RHS_M16.sqf";

 

==============================================================*/

 

//Check to see if the Loadout Init file has run

If (isNil "LoadOutInitLoaded") then {

 

_LoadoutInitCheck = [] execVM "Scripts\Loadouts\Loadout_Init.sqf";

                

WaitUntil { scriptDone _LoadoutInitCheck };

 

};

 

//Define Parameters

params[ "_group", "_module", "_groupData" ];

 

 

 

//START

{

                _x params[ "_unit" ];

 

                _unitType = typeOf _unit;

                

                _PrimWpn = primaryWeapon _unit;

 

                _PossibleScopes = selectRandom ["rhsusf_acc_ACOG3_USMC"];

                

if ( (_unitType in Loadouts_AllCrew) && (vehicle _unit) == _unit) then {deletevehicle _x};

 

//East

//--------------------------------------- 

if ( side _group isEqualTo East ) exitwith {

 

Systemchat "RHS M16 Loadout - This LoadOut Script Only Applies to West/NATO Units!";

 

};

 

//West

//--------------------------------------- 

if ( side _group isEqualTo West ) then {

 

_PossibleLaunchers = selectRandom ["rhs_weap_M136_hedp", "", "", ""];

 

_PossibleGrips = selectRandom ["rhsusf_acc_kac_grip", "", ""];

 

if (_unitType in Loadouts_SquadLeader) then {[_unit, configfile >> "CfgVehicles" >> "rhsusf_army_ocp_squadleader"] call BIS_fnc_loadinventory;};

 

if (_unitType in Loadouts_RiflemanAT) then {[_unit, configfile >> "CfgVehicles" >> "rhsusf_army_ocp_maaws"] call BIS_fnc_loadinventory;};                                              // AT Unit (MAAWS)

 

if (_unitType in Loadouts_TeamLeader) then {[_unit, configfile >> "CfgVehicles" >> "rhsusf_army_ocp_teamleader"] call BIS_fnc_loadinventory;};

 

if (_unitType in Loadouts_Ammobearer) then {[_unit, configfile >> "CfgVehicles" >> "rhsusf_army_ocp_rifleman"] call BIS_fnc_loadinventory;};                                              // Ammobearer Loadout (Rifleman)

 

if (_unitType in Loadouts_Rifleman) then {[_unit, configfile >> "CfgVehicles" >> "rhsusf_army_ocp_rifleman"] call BIS_fnc_loadinventory;};

 

if (_unitType in Loadouts_Marksman) then {[_unit, configfile >> "CfgVehicles" >> "rhsusf_army_ocp_marksman"] call BIS_fnc_loadinventory;};

 

if (_unitType in Loadouts_Autorifleman) then {[_unit, configfile >> "CfgVehicles" >> "rhsusf_army_ocp_autorifleman"] call BIS_fnc_loadinventory;};

 

if (_unitType in Loadouts_Medic) then {[_unit, configfile >> "CfgVehicles" >> "rhsusf_army_ocp_medic"] call BIS_fnc_loadinventory;};

 

if (_unitType in Loadouts_Grenadier) then {[_unit, configfile >> "CfgVehicles" >> "rhsusf_army_ocp_grenadier"] call BIS_fnc_loadinventory;};

 

if (_unitType in Loadouts_MissleSpecialist) then {[_unit, configfile >> "CfgVehicles" >> "rhsusf_army_ocp_maaws"] call BIS_fnc_loadinventory;};                                              // Missle Specialist (MAAWS)    

 

if (_unitType in Loadouts_AsstMissleSpecialist) then {[_unit, configfile >> "CfgVehicles" >> "rhsusf_army_ocp_riflemanat"] call BIS_fnc_loadinventory;};      // Asst Missle Specialist (AT4)

 

if (_unitType in Loadouts_HeavyGunner) then {[_unit, configfile >> "CfgVehicles" >> "rhsusf_army_ocp_machinegunner"] call BIS_fnc_loadinventory;};

 

if (_unitType in Loadouts_GunnerGMG) then {[_unit, configfile >> "CfgVehicles" >> "rhsusf_army_ocp_rifleman"] call BIS_fnc_loadinventory;};                                              // GMG Gunner (Rifelman)

 

if (_unitType in Loadouts_GunnerHMG) then {[_unit, configfile >> "CfgVehicles" >> "rhsusf_army_ocp_rifleman"] call BIS_fnc_loadinventory;};                                              // HMG Gunner (Rifelman)

 

if (_unitType in Loadouts_AsstGunner) then {[_unit, configfile >> "CfgVehicles" >> "rhsusf_army_ocp_rifleman"] call BIS_fnc_loadinventory;};                                              // Assistant Gunner (Rifelman)

 

if (_unitType in Loadouts_GroundCrew) then {[_unit, configfile >> "CfgVehicles" >> "rhsusf_army_ocp_crewman"] call BIS_fnc_loadinventory;};

 

if (_unitType in Loadouts_HeliCrew) then {[_unit, configfile >> "CfgVehicles" >> "rhsusf_airforce_jetpilot"] call BIS_fnc_loadinventory;};

 

if (_unitType in Loadouts_AircraftCrew) then {[_unit, configfile >> "CfgVehicles" >> "rhsusf_army_ucp_helipilot"] call BIS_fnc_loadinventory;};

 

if (_unitType in Loadouts_AllRiflemen) then { 

                

                _unit call Loadout_fnc_ClearAmmo;

                

                _unit removeweapon _PrimWpn;

                

                [_unit, "rhs_weap_m16a4_carryhandle", 6] call BIS_fnc_addWeapon;

                

                _unit addPrimaryWeaponItem _PossibleScopes;

 

                _unit addPrimaryWeaponItem "rhsusf_acc_anpeq15_top";

 

                _unit addPrimaryWeaponItem _PossibleGrips;

                

                _unit selectweapon (primaryweapon _unit);

 

                };

 

if (_unitType in Loadouts_AllGrenadiers) then { 

                

                _unit call Loadout_fnc_ClearAmmo;

                

                _unit removeweapon _PrimWpn;

                

                for "_i" from 1 to 5 do {_unit addItemToVest "rhs_mag_M433_HEDP"};

                

                for "_i" from 1 to 10 do {_unit addItemToBackpack "rhs_mag_M433_HEDP"};

                

                [_unit, "rhs_weap_m16a4_carryhandle_M203", 6] call BIS_fnc_addWeapon;

                

                _unit addPrimaryWeaponItem _PossibleScopes;

 

                _unit addPrimaryWeaponItem "rhsusf_acc_anpeq15_top";

                

                _unit selectweapon (primaryweapon _unit);

 

                };

 

if (_unitType in Loadouts_Ammobearer) then { 

 

                _unit addBackpack "rhsusf_assault_eagleaiii_ocp";

 

                _unit call Loadout_fnc_AmmoBearerFix_RHS;

 

                backpackContainer _unit addMagazineCargoGlobal ["rhs_mag_30Rnd_556x45_M855A1_Stanag", 5];

 

                backpackContainer _unit addMagazineCargoGlobal ["rhsusf_100Rnd_556x45_soft_pouch", 1];

 

                };

 

if (_unitType in Loadouts_Rifleman) then {

 

                _unit addWeapon _PossibleLaunchers;

 

                };

 

};

 

//Independent

//--------------------------------------- 

if ( side _group isEqualTo Independent ) exitwith {

 

Systemchat "RHS Loadout - Independent Units not Supported Yet!";

 

};

 

[_x, (primaryWeaponItems _x) select 3] call Loadout_fnc_RHSGripFix;

 

Sleep 1;

 

} forEach units _group;

 

//END

 


Edit: Changed how the script was called and that fixed the issue. 

Share this post


Link to post
Share on other sites

Ensure the units don't already have backpacks.

Triple check you're executing on the server!

Share this post


Link to post
Share on other sites
11 hours ago, Maff said:

Ensure the units don't already have backpacks.

Triple check you're executing on the server!

I think I tried that before and it didnt work. I will try it again

Share this post


Link to post
Share on other sites

Does it happen for all units? BIS_fnc_loadout should automatically handle removing backpacks before adding new ones.

 

If it's happening to just _unitType in Loadouts_Ammobearer it's because you do not remove the backpack(if it has one) before adding a new one...

On 10/14/2022 at 8:09 PM, Blitzen88 said:

if (_unitType in Loadouts_Ammobearer) then { 
                _unit addBackpack "rhsusf_assault_eagleaiii_ocp";
 

 

Share this post


Link to post
Share on other sites
On 10/14/2022 at 4:09 PM, Blitzen88 said:

How can I fix this?

 

 

Hey, @Blitzen88

 

This down below is my current solution for the CSWR script where you are able to change backpacks, vests and uniforms without losing the items in the original unit gears. Perhaps you can find out inspiration to run through your needs:

 

Spoiler

// CSWR v2.6.1
// File: your_mission\CSWRandomizr\fn_CSWR_globalFunctions.sqf
// by thy (@aldolammel)

THY_fnc_CSWR_loadout = {
	// This function: define the faction loadout details for each unit spawned by CSWR. 
	// Returns nothing.
	
	params ["_faction", "_unit"];
	
	switch (_faction) do {

		case BLUFOR: {
			// Exclusively for things to unlink and remove:
				//_unit unlinkItem "NVGoggles";    // unlink the night vision from the unit head;
				//_unit removeItem "NVGoggles";    // remove the night vision from the unit backpack/vest/uniform;
				//_unit removeItems "FirstAidKit";    // it removes all (removeItemS) firstAidKits in unit's inventory;
				_unit unlinkItem "ItemWatch";
				_unit removeItem "ItemWatch";
				_unit unlinkItem "ItemCompass";
				_unit removeItem "ItemCompass";
				_unit unlinkItem "ItemRadio";
				_unit removeItem "ItemRadio";
				_unit unlinkItem "ItemGPS";
				_unit removeItem "ItemGPS";
				_unit unlinkItem "ItemMap";
				_unit removeItem "ItemMap";
				_unit removeWeapon "Binocular";
				//removeHeadgear _unit;    // removes the helmet and facewear from everyone in the faction.

			// Exclusively for Uniform replacement:
				[_unit, "U_C_Driver_2"] call THY_fnc_CSWR_uniformRepacker;  // add a new uniform to the unit.
			
			// Exclusively for Vest replacement:
				[_unit, "V_TacVest_blk_POLICE"] call THY_fnc_CSWR_vestRepacker;  // add a new vest to the unit.
			
			// Exclusively for Backpack replacement:
				[_unit, "B_Bergen_mcamo_F"] call THY_fnc_CSWR_backpackRepacker;  // add a new backpack to the unit.
			
			// Exclusively for things to add or link:
				//_unit addHeadgear "H_HelmetB";    // add the same helmet or facewear to faction units.
				//_unit addItem "arifle_MXM_Hamr_pointer_F";    // create the item and store it somewhere in the unit inventory.
				//_unit linkItem "arifle_MXM_Hamr_pointer_F";   // assign the item to its specific space in the unit inventory.
				//_unit addItem "FirstAidKit";    // create just one bandage in unit inventory.
				//_unit addItem "FirstAidKit";    // create another bandage in unit inventory.
				//_unit addItem "ItemGPS";    // create item to unit inventary.
				//_unit linkItem "ItemGPS";    // assign the  item to the correct slot in unit inventory.
		};

		case OPFOR: {
			// Exclusively for things to unlink and remove:
				//_unit unlinkItem "NVGoggles_OPFOR";
				//_unit removeItem "NVGoggles_OPFOR";
				//_unit removeItems "FirstAidKit";
				_unit unlinkItem "ItemWatch";
				_unit removeItem "ItemWatch";
				_unit unlinkItem "ItemCompass";
				_unit removeItem "ItemCompass";
				_unit unlinkItem "ItemRadio";
				_unit removeItem "ItemRadio";
				_unit unlinkItem "ItemGPS";
				_unit removeItem "ItemGPS";
				_unit unlinkItem "ItemMap";
				_unit removeItem "ItemMap";
				_unit removeWeapon "Binocular";

			// Exclusively for Uniform replacement:
				//[_unit, "U_C_Driver_1_red"] call THY_fnc_CSWR_uniformRepacker;  // add a new uniform to the unit.
			
			// Exclusively for Vest replacement:
				//[_unit, "V_TacVest_brn"] call THY_fnc_CSWR_vestRepacker;  // add a new vest to the unit.
			
			// Exclusively for Backpack replacement:
				//[_unit, "B_Kitbag_rgr"] call THY_fnc_CSWR_backpackRepacker;  // add a new backpack to the unit.

			// Exclusively for things to add or link:
				// xxxxxxx
		};

		case INDEPENDENT: {
			// Exclusively for things to unlink and remove:
				//_unit unlinkItem "NVGoggles_INDEP";
				//_unit removeItem "NVGoggles_INDEP";
				//_unit removeItems "FirstAidKit";
				_unit unlinkItem "ItemWatch";
				_unit removeItem "ItemWatch";
				_unit unlinkItem "ItemCompass";
				_unit removeItem "ItemCompass";
				_unit unlinkItem "ItemRadio";
				_unit removeItem "ItemRadio";
				_unit unlinkItem "ItemGPS";
				_unit removeItem "ItemGPS";
				_unit unlinkItem "ItemMap";
				_unit removeItem "ItemMap";
				_unit removeWeapon "Binocular";

			// Exclusively for Uniform replacement:
				//[_unit, "U_C_Driver_1"] call THY_fnc_CSWR_uniformRepacker;  // add a new uniform to the unit.
			
			// Exclusively for Vest replacement:
				//[_unit, "V_TacVest_camo"] call THY_fnc_CSWR_vestRepacker;  // add a new vest to the unit.
			
			// Exclusively for Backpack replacement:
				//[_unit, "B_Kitbag_sgg"] call THY_fnc_CSWR_backpackRepacker;  // add a new backpack to the unit.
			
			// Exclusively for things to add or link:
				// xxxxxxx
		};

		case CIVILIAN: {
			// Exclusively for things to unlink and remove:
				_unit unlinkItem "ItemWatch";
				_unit removeItem "ItemWatch";
				_unit unlinkItem "ItemCompass";
				_unit removeItem "ItemCompass";
				_unit unlinkItem "ItemMap";
				_unit removeItem "ItemMap";
				_unit removeItems "FirstAidKit";

			// Exclusively for Uniform replacement:
				//[_unit, "U_C_Poloshirt_stripped"] call THY_fnc_CSWR_uniformRepacker;  // add a new uniform to the unit.
			
			// Exclusively for Vest replacement:
				//[_unit, "xxxxxx"] call THY_fnc_CSWR_vestRepacker;  // add a new vest to the unit.
			
			// Exclusively for Backpack replacement:
				//[_unit, "xxxxxx"] call THY_fnc_CSWR_backpackRepacker;  // add a new backpack to the unit.

			// Exclusively for things to add or link:
				// xxxxxxx
		};
	};

	true
};


// CSWR CORE / TRY TO CHANGE NOTHING BELOW!!! --------------------------------------------------------------------


THY_fnc_CSWR_uniformScanner = {
	// This function checks the unit's uniform to understand its current contents in case the mission editor chooses to replace the uniform by a new one.
	// Returns _uniformContent.

	params ["_unit"];
	private ["_uniform", "_uniformContent"];

	_uniform = uniform _unit;
	_uniformContent = [];

	// if there's an uniform, then save all its original content:
	if ( _uniform != "" ) then { _uniformContent = uniformItems _unit };

	_uniformContent  //Returning.
};


THY_fnc_CSWR_vestScanner = {
	// This function checks the unit's vest to understand its current contents in case the mission editor chooses to replace the vest by a new one.
	// Returns _vestContent.

	params ["_unit"];
	private ["_vest", "_vestContent"];

	_vest = vest _unit;
	_vestContent = [];

	// if there's a backpack, then save all its original content:
	if ( _vest != "" ) then { _vestContent = vestItems _unit };

	_vestContent  //Returning.
};


THY_fnc_CSWR_backpackScanner = {
	// This function checks the unit's backpack to understand its current contents in case the mission editor chooses to replace the backpack by a new one.
	// Returns _backpackContent.

	params ["_unit"];
	private ["_backpack", "_backpackContent"];

	_backpack = backpack _unit;
	_backpackContent = [];

	// if there's a backpack, then save all its original content:
	if ( _backpack != "" ) then { _backpackContent = backpackItems _unit };

	_backpackContent  //Returning.
};


THY_fnc_CSWR_uniformRepacker = {
	// This function add to the new uniform all the old unit's uniform original content.
	// Returns nothing.

	params ["_unit", "_newUniform"];
	private ["_oldUniform", "_oldUniformContent"];

	_oldUniform = uniform _unit;
	_oldUniformContent = [_unit] call THY_fnc_CSWR_uniformScanner;

	// if there's an uniform, then...
	if ( _oldUniform != "" ) then 
	{
		removeUniform _unit;
		_unit forceAddUniform _newUniform;
		{ _unit addItemToUniform _x } forEach _oldUniformContent;
	};

	true
};


THY_fnc_CSWR_vestRepacker = {
	// This function add to the new vest all the old unit's vest original content.
	// Returns nothing.

	params ["_unit", "_newVest"];
	private ["_oldVest", "_oldVestContent"];

	_oldVest = vest _unit;
	_oldVestContent = [_unit] call THY_fnc_CSWR_vestScanner;

	// if there's a vest, then...
	if ( _oldVest != "" OR CSWR_vestForAll ) then 
	{
		removeVest _unit;
		_unit addVest _newVest;
		{ _unit addItemToVest _x } forEach _oldVestContent;
	};

	true
};


THY_fnc_CSWR_backpackRepacker = {
	// This function add to the new backpack all the old unit's backpack original content.
	// Returns nothing.

	params ["_unit", "_newBackpack"];
	private ["_oldBackpack", "_oldBackpackContent"];

	_oldBackpack = backpack _unit;
	_oldBackpackContent = [_unit] call THY_fnc_CSWR_backpackScanner;

	// if there's a backpack, then...
	if ( _oldBackpack != "" OR CSWR_backpackForAll ) then 
	{
		removeBackpack _unit;
		_unit addBackpack _newBackpack;
		{ _unit addItemToBackpack _x } forEach _oldBackpackContent;
	};

	true
};

 

 

Share this post


Link to post
Share on other sites
18 hours ago, Larrow said:

Does it happen for all units? BIS_fnc_loadout should automatically handle removing backpacks before adding new ones.

 

If it's happening to just _unitType in Loadouts_Ammobearer it's because you do not remove the backpack(if it has one) before adding a new one...

 

Believe it happens for all units (any unit with a backpack). Script is being run on vanilla units.

 

Im not sure whats causing it - sometimes the script runs and does not create duplicate backpacks (that fall to the ground) but most of the times it does. 
 

I think I tried removing backpacks before exec’ing the loadout changes but it didnt change anything. 

 

 

Share this post


Link to post
Share on other sites

Had some time this weekend to fool around with this. Added a snippet which removed backpacks before the loadout changes but it still resulted in duplicate backpacks.

 

I could almost swear the script is running multiple times per unit instead of one time per unit but I cant figure it out. 

Share this post


Link to post
Share on other sites
On 10/14/2022 at 8:09 PM, Blitzen88 said:

* Infantry units spawned by an AI Spawn Module which is linked to the Sector Tactics Module are stripped of their loadout/weapon attachments

* Infantry units spawned by an AI Spawn Module which is NOT linked to the Sector Tactics Module are NOT stripped of their loadout/weapon attachments

 

* Call with (Unit Init): {_x execVM "Scripts\Loadouts\RHS\Loadout_RHS_M16.sqf"} foreach units group this;

* Call with (Sector Module): _this execVM "Scripts\Loadouts\RHS\Loadout_RHS_M16.sqf";

How exactly are you using this script?

I see nothing in the script that would differentiate between the first two statements above. ie whether or not they are spawned by a Spawn AI Module that is linked/or not to a Sector Tactics Module.

Unit Init line is wrong, if you placed this in a units init then the script would run for each unit in the group multiplied by number of units in the group who have the same init, in MP this would be further compounded by being multiplied by the number of current clients. This should really be Call with (Group Init) and foreach units this and a isServer check to ensure it is only happening in one place.

I can only presume (Sector Module) is wrong and means AI Spawn Module Expression? As that would make sense for the params you are taking.

 

If I use a Spawn AI Module and put...

{
	if !( isNull backpackContainer _x ) then {
		systemChat format[ "%1 has back pack", typeOf _x ];
	};
	[ _x, configfile >> "CfgVehicles" >> "B_Soldier_A_F" ] call BIS_fnc_loadInventory;
}forEach units ( _this#0 );

...in the module Expression, I see no duplicated backpacks dropping when replacing inventory with a class that has a backpack(B_Solider_A_F) as part of their inventory, even for those that originally spawned with a backpack(systemChat).

Share this post


Link to post
Share on other sites
On 10/25/2022 at 11:28 AM, Larrow said:

How exactly are you using this script?

 

Unit Init line is wrong, if you placed this in a units init then the script would run for each unit in the group multiplied by number of units in the group who have the same init, in MP this would be further compounded by being multiplied by the number of current clients. This should really be Call with (Group Init) and foreach units this and a isServer check to ensure it is only happening in one place.

 

I can only presume (Sector Module) is wrong and means AI Spawn Module Expression? As that would make sense for the params you are taking.

 

 


I wanted the script to work with being called via a SpawnAI module or a unit’s init line; thats the overall goal. 
 

The script is being called via a SpawnAI module’s expression line - calling it a sector module is poor and incorrect wording by me.

 

I can’t remember if the issue exists with units spawned via a SpawnAI module but I do know that duplicate backpacks occur when the script is called via a unit’s init. Based off of what you wrote, I am assuming I am calling/execing it wrong..? Whats the correct way to exec it?

 

 

Edit: After looking it over again, and looking at the params wiki entry, I think I know what the problem is. 
 

Edit #2: Changed how the script was called and that fixed the issue

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

×