Jump to content
Sign in to follow this  
HKFlash

Unit gear based on classname

Recommended Posts

Hello,

I'm currently creating a script for my mission to create gear based on the unit's classname.

{
switch {typeOf _x} do 
{

	case "I_soldier_F": 

	removeAllWeapons _x;
	removeAllItems _x;
	removeAllAssignedItems _x;
	removeUniform _x;
	removeVest _x;
	removeBackpack _x;
	removeHeadgear _x;
	removeGoggles _x;

	_x forceAddUniform "U_I_CombatUniform";
	for "_i" from 1 to 2 do {_x addItemToUniform "Chemlight_green";};
	for "_i" from 1 to 2 do {_x addItemToUniform "30Rnd_556x45_Stanag";};
	_x addVest "V_PlateCarrierIA1_dgtl";
	_x addItemToVest "MiniGrenade";
	for "_i" from 1 to 2 do {_x addItemToVest "SmokeShell";};
	_x addItemToVest "HandGrenade";
	for "_i" from 1 to 8 do {_x addItemToVest "30Rnd_556x45_Stanag";};
	_x addHeadgear "H_HelmetIA";

	_x addWeapon "arifle_Mk20_F";
	_x addPrimaryWeaponItem "optic_Aco";

	_x linkItem "ItemMap";
	_x linkItem "ItemCompass";
	_x linkItem "ItemWatch";
	_x linkItem "ItemRadio";

	case "I_Soldier_GL_F";

	removeAllWeapons _x;
	removeAllItems _x;
	removeAllAssignedItems _x;
	removeUniform _x;
	removeVest _x;
	removeBackpack _x;
	removeHeadgear _x;
	removeGoggles _x;

	_x forceAddUniform "U_I_CombatUniform";
	for "_i" from 1 to 2 do {_x addItemToUniform "Chemlight_green";};
	for "_i" from 1 to 2 do {_x addItemToUniform "30Rnd_556x45_Stanag";};
	_x addVest "V_PlateCarrierIA1_dgtl";
	for "_i" from 1 to 8 do {_x addItemToVest "30Rnd_556x45_Stanag";};
	for "_i" from 1 to 4 do {_x addItemToVest "1Rnd_HE_Grenade_shell";};
	for "_i" from 1 to 2 do {_x addItemToVest "1Rnd_Smoke_Grenade_shell";};
	_x addItemToVest "HandGrenade";
	_x addItemToVest "MiniGrenade";
	_x addHeadgear "H_HelmetIA";

	_x addWeapon "arifle_Mk20_GL_F";
	_x addPrimaryWeaponItem "optic_MRCO";

	_x linkItem "ItemMap";
	_x linkItem "ItemCompass";
	_x linkItem "ItemWatch";
	_x linkItem "ItemRadio";



}
} forEach allUnits;

However the problem is that it is applying the last loadout to every unit that I spawn. What is the issue here? allUnits?

Share this post


Link to post
Share on other sites

Syntax error with your cases.

They should be

case "I_Soldier_GL_F":
           {
             //Loadout stuff
           };

You're currently missing the {} for each case.

Share this post


Link to post
Share on other sites

Hi and thanks for the reply,

I did some fixes but the script is not having any effect on the units:

{
switch {typeOf _x} do 
{	
	case "I_soldier_F": 
	{

		removeAllWeapons _x;
		removeAllItems _x;
		removeAllAssignedItems _x;
		removeUniform _x;
		removeVest _x;
		removeBackpack _x;
		removeHeadgear _x;
		removeGoggles _x;

		_x forceAddUniform "U_I_CombatUniform";
		for "_i" from 1 to 2 do {_x addItemToUniform "Chemlight_green";};
		for "_i" from 1 to 2 do {_x addItemToUniform "30Rnd_556x45_Stanag";};
		_x addVest "V_PlateCarrierIA1_dgtl";
		_x addItemToVest "MiniGrenade";
		for "_i" from 1 to 2 do {_x addItemToVest "SmokeShell";};
		_x addItemToVest "HandGrenade";
		for "_i" from 1 to 8 do {_x addItemToVest "30Rnd_556x45_Stanag";};
		_x addHeadgear "H_HelmetIA";

		_x addWeapon "arifle_Mk20_F";
		_x addPrimaryWeaponItem "optic_Aco";

		_x linkItem "ItemMap";
		_x linkItem "ItemCompass";
		_x linkItem "ItemWatch";
		_x linkItem "ItemRadio";

	};

	case "I_Soldier_GL_F";
	{

		removeAllWeapons _x;
		removeAllItems _x;
		removeAllAssignedItems _x;
		removeUniform _x;
		removeVest _x;
		removeBackpack _x;
		removeHeadgear _x;
		removeGoggles _x;

		_x forceAddUniform "U_I_CombatUniform";
		for "_i" from 1 to 2 do {_x addItemToUniform "Chemlight_green";};
		for "_i" from 1 to 2 do {_x addItemToUniform "30Rnd_556x45_Stanag";};
		_x addVest "V_PlateCarrierIA1_dgtl";
		for "_i" from 1 to 8 do {_x addItemToVest "30Rnd_556x45_Stanag";};
		for "_i" from 1 to 4 do {_x addItemToVest "1Rnd_HE_Grenade_shell";};
		for "_i" from 1 to 2 do {_x addItemToVest "1Rnd_Smoke_Grenade_shell";};
		_x addItemToVest "HandGrenade";
		_x addItemToVest "MiniGrenade";
		_x addHeadgear "H_HelmetIA";

		_x addWeapon "arifle_Mk20_GL_F";
		_x addPrimaryWeaponItem "optic_MRCO";

		_x linkItem "ItemMap";
		_x linkItem "ItemCompass";
		_x linkItem "ItemWatch";
		_x linkItem "ItemRadio";

	};
}
} forEach allUnits;

Share this post


Link to post
Share on other sites

In your second case:

case "I_Soldier_GL_F";

//should be

case "I_Soldier_GL_F": //<<You used ";" instead of ":"

If that does't work, I would venture to guess that "_x" within the cases is not defined properly, but like I said, just a guess.

I would also recommend throwing in a default case with some sort of error message, just good practice, and helps you see if the switch is just jumping out with an improperly defined variable.

And like Iceman said, they are case sensitive, and you do have a couple extra lowercases in there :p.

Edited by JShock

Share this post


Link to post
Share on other sites

As Iceman said the strings are case sensitive, I've just checked and you need to replace

"I_soldier_F"

with

"I_Soldier_F"

and

"I_Soldier_GL_F"

with

"I_soldier_GL_F"

If in doubt copy and paste the name from the top right hand corner of the unit placement box.

Share this post


Link to post
Share on other sites

I'm sorry guys but I'm using the correct classname for the units, I've just checked it (and I did copy it directly from the unit placement window).

Even fixing the commas does nothing. I will throw in the default but I can't figure this out. I would advise you to try it yourselves to check it better.

Appreciate all your help. Thanks.

Share this post


Link to post
Share on other sites

Fuck and Bollocks, I've only just noticed there's various discrepancies in the classnames for units across the three sides, I assumed they would be the same, the classnames I just gave you were based on the NATO faction since that was the first one I looked at. I'll have a go at debugging your code now.

Share this post


Link to post
Share on other sites
Fuck and Bollocks, I've only just noticed there's various discrepancies in the classnames for units across the three sides, I assumed they would be the same, the classnames I just gave you were based on the NATO faction since that was the first one I looked at. I'll have a go at debugging your code now.

Got to love consistency huh :D.

Share this post


Link to post
Share on other sites

You're using the wrong brackets. Should be:

   switch ( typeOf _x ) do   
       // . . . //

Also, I recommend using toLower command.

Share this post


Link to post
Share on other sites
You're using the wrong brackets. Should be:

   switch ( typeOf _x ) do   
       // . . . //

Also, I recommend using toLower command.

Bingo. I did use () at first but was told it was {} No issues though! Thank you, thank you very much!

Share this post


Link to post
Share on other sites

There's also a ; on the second case instead of a :

Should be

case "I_Soldier_GL_F":

Share this post


Link to post
Share on other sites
There's also a ; on the second case instead of a :

Should be

case "I_Soldier_GL_F":

I caught that one earlier Iceman :D.

Share this post


Link to post
Share on other sites
just to add a note when struggling with script syntax. Use -showScriptErrors in startup parameters.

Explained more here: https://community.bistudio.com/wiki/Arma_3_Startup_Parameters#Developer_Options

I was using that and I always do when creating missions. The option is available on the launcher now, under Author. Thanks anyway.

Share this post


Link to post
Share on other sites
Bingo. I did use () at first but was told it was {} No issues though! Thank you, thank you very much!

RangerThunder are you able to post the working script and details of how you are calling it? I'm trying to do something very similar both as a script called from init.sqf and as a function called per unit. It isn't working for me...

Share this post


Link to post
Share on other sites

You can execute the script however you like and whenever you like. eg;

init.sqf

_nul=execVM "someScript.sqf";

someScipt.sqf

{
   switch (typeOf _x) do 
   {    
       case "I_soldier_F": 
       {
		// code
       };

       case "I_Soldier_GL_F";
       {        
          // code
       };
   }
} forEach allUnits;  

And it's not working for you because you probably copy and pasted the snippet in the OP... which doesn't work... which is why this thread exists.

Share this post


Link to post
Share on other sites
You can execute the script however you like and whenever you like. eg;

init.sqf

_nul=execVM "someScript.sqf";

someScipt.sqf

{
   switch (typeOf _x) do 
   {    
       case "I_soldier_F": 
       {
		// code
       };

       case "I_Soldier_GL_F";
       {        
          // code
       };
   }
} forEach allUnits;  

And it's not working for you because you probably copy and pasted the snippet in the OP... which doesn't work... which is why this thread exists.

There's a reason why it's not working but it has nothing to do with copy/pasting the script at the start of this thread. I wrote my own which looks similar to what you've pasted in, but I get an error that typeOf is expecting a string not an array.

Share this post


Link to post
Share on other sites

Hi gruukh,

My script is fully functional, here's the code. I might actually turn it into a function.

{
switch (typeOf _x) do 
{	
	//SQUAD LEADER
	case "I_Soldier_SL_F":
	{
		removeAllWeapons _x;
		removeAllItems _x;
		removeAllAssignedItems _x;
		removeUniform _x;
		removeVest _x;
		removeBackpack _x;
		removeHeadgear _x;
		removeGoggles _x;

		_x forceAddUniform "U_I_CombatUniform";
		for "_i" from 1 to 4 do {_x addItemToUniform "Chemlight_green";};
		for "_i" from 1 to 2 do {_x addItemToUniform "30Rnd_556x45_Stanag";};
		_x addItemToUniform "I_IR_Grenade";
		_x addVest "V_PlateCarrierIA1_dgtl";
		_x addItemToVest "MiniGrenade";
		for "_i" from 1 to 2 do {_x addItemToVest "SmokeShell";};
		_x addItemToVest "HandGrenade";
		for "_i" from 1 to 8 do {_x addItemToVest "30Rnd_556x45_Stanag";};
		_x addItemToVest "SmokeShellGreen";
		_x addItemToVest "SmokeShellRed";
		_x addHeadgear "H_HelmetIA";

		_x addWeapon "arifle_Mk20_F";
		_x addPrimaryWeaponItem "acc_flashlight";
		_x addPrimaryWeaponItem "optic_MRCO";
		_x addWeapon "Binocular";

		_x linkItem "ItemMap";
		_x linkItem "ItemCompass";
		_x linkItem "ItemWatch";
		_x linkItem "ItemRadio";
		_x linkItem "ItemGPS";
	};
	//TEAM LEADER
	case "I_Soldier_TL_F":
	{
		removeAllWeapons _x;
		removeAllItems _x;
		removeAllAssignedItems _x;
		removeUniform _x;
		removeVest _x;
		removeBackpack _x;
		removeHeadgear _x;
		removeGoggles _x;

		_x forceAddUniform "U_I_CombatUniform";
		for "_i" from 1 to 2 do {_x addItemToUniform "Chemlight_green";};
		for "_i" from 1 to 2 do {_x addItemToUniform "30Rnd_556x45_Stanag";};
		_x addItemToUniform "I_IR_Grenade";
		_x addVest "V_PlateCarrierIA1_dgtl";
		_x addItemToVest "MiniGrenade";
		for "_i" from 1 to 2 do {_x addItemToVest "SmokeShell";};
		_x addItemToVest "HandGrenade";
		for "_i" from 1 to 8 do {_x addItemToVest "30Rnd_556x45_Stanag";};
		_x addItemToVest "SmokeShellRed";
		_x addItemToVest "SmokeShellGreen";
		_x addHeadgear "H_HelmetIA";

		_x addWeapon "arifle_Mk20_F";
		_x addPrimaryWeaponItem "acc_flashlight";
		_x addPrimaryWeaponItem "optic_Aco";

		_x linkItem "ItemMap";
		_x linkItem "ItemCompass";
		_x linkItem "ItemWatch";
		_x linkItem "ItemRadio";
		_x linkItem "ItemGPS";
	};
	//RIFLEMAN
	case "I_soldier_F": 
	{
		removeAllWeapons _x;
		removeAllItems _x;
		removeAllAssignedItems _x;
		removeUniform _x;
		removeVest _x;
		removeBackpack _x;
		removeHeadgear _x;
		removeGoggles _x;

		_x forceAddUniform "U_I_CombatUniform";
		for "_i" from 1 to 2 do {_x addItemToUniform "Chemlight_green";};
		for "_i" from 1 to 2 do {_x addItemToUniform "30Rnd_556x45_Stanag";};
		_x addVest "V_PlateCarrierIA1_dgtl";
		_x addItemToVest "MiniGrenade";
		for "_i" from 1 to 2 do {_x addItemToVest "SmokeShell";};
		_x addItemToVest "HandGrenade";
		for "_i" from 1 to 8 do {_x addItemToVest "30Rnd_556x45_Stanag";};
		_x addHeadgear "H_HelmetIA";

		_x addWeapon "arifle_Mk20_F";
		_x addPrimaryWeaponItem "optic_Aco";

		_x linkItem "ItemMap";
		_x linkItem "ItemCompass";
		_x linkItem "ItemWatch";
		_x linkItem "ItemRadio";
	};
	//GRENADIER
	case "I_Soldier_GL_F":
	{		
		removeAllWeapons _x;
		removeAllItems _x;
		removeAllAssignedItems _x;
		removeUniform _x;
		removeVest _x;
		removeBackpack _x;
		removeHeadgear _x;
		removeGoggles _x;

		_x forceAddUniform "U_I_CombatUniform";
		for "_i" from 1 to 2 do {_x addItemToUniform "Chemlight_green";};
		for "_i" from 1 to 2 do {_x addItemToUniform "30Rnd_556x45_Stanag";};
		_x addVest "V_PlateCarrierIA1_dgtl";
		for "_i" from 1 to 8 do {_x addItemToVest "30Rnd_556x45_Stanag";};
		for "_i" from 1 to 4 do {_x addItemToVest "1Rnd_HE_Grenade_shell";};
		for "_i" from 1 to 2 do {_x addItemToVest "1Rnd_Smoke_Grenade_shell";};
		_x addItemToVest "HandGrenade";
		_x addItemToVest "MiniGrenade";
		_x addHeadgear "H_HelmetIA";

		_x addWeapon "arifle_Mk20_GL_F";
		_x addPrimaryWeaponItem "optic_MRCO";

		_x linkItem "ItemMap";
		_x linkItem "ItemCompass";
		_x linkItem "ItemWatch";
		_x linkItem "ItemRadio";
	};
	//AUTOMATIC RIFLEMAN
	case "I_Soldier_AR_F":
	{
		removeAllWeapons _x;
		removeAllItems _x;
		removeAllAssignedItems _x;
		removeUniform _x;
		removeVest _x;
		removeBackpack _x;
		removeHeadgear _x;
		removeGoggles _x;

		_x forceAddUniform "U_I_CombatUniform";
		for "_i" from 1 to 2 do {_x addItemToUniform "Chemlight_green";};
		for "_i" from 1 to 2 do {_x addItemToUniform "SmokeShell";};
		_x addVest "V_PlateCarrierIA1_dgtl";
		for "_i" from 1 to 2 do {_x addItemToVest "200Rnd_65x39_cased_Box";};
		_x addItemToVest "MiniGrenade";
		_x addItemToVest "HandGrenade";
		_x addHeadgear "H_HelmetIA";

		_x addWeapon "LMG_Mk200_F";
		_x addPrimaryWeaponItem "optic_Aco";

		_x linkItem "ItemMap";
		_x linkItem "ItemCompass";
		_x linkItem "ItemWatch";
		_x linkItem "ItemRadio";
	};
	//RIFLEMAN [AT]
	case "I_Soldier_LAT_F":
	{
		removeAllWeapons _x;
		removeAllItems _x;
		removeAllAssignedItems _x;
		removeUniform _x;
		removeVest _x;
		removeBackpack _x;
		removeHeadgear _x;
		removeGoggles _x;

		_x forceAddUniform "U_I_CombatUniform";
		for "_i" from 1 to 2 do {_x addItemToUniform "Chemlight_green";};
		for "_i" from 1 to 2 do {_x addItemToUniform "30Rnd_556x45_Stanag";};
		for "_i" from 1 to 2 do {_x addItemToUniform "SmokeShell";};
		_x addVest "V_PlateCarrierIA1_dgtl";
		for "_i" from 1 to 6 do {_x addItemToVest "30Rnd_556x45_Stanag";};
		for "_i" from 1 to 2 do {_x addItemToVest "SmokeShell";};
		_x addBackpack "B_AssaultPack_dgtl";
		for "_i" from 1 to 2 do {_x addItemToBackpack "NLAW_F";};
		_x addHeadgear "H_HelmetIA";

		_x addWeapon "arifle_Mk20_F";
		_x addPrimaryWeaponItem "optic_Aco";
		_x addWeapon "launch_NLAW_F";

		_x linkItem "ItemMap";
		_x linkItem "ItemCompass";
		_x linkItem "ItemWatch";
		_x linkItem "ItemRadio";
	};
	//AMMO BEARER
	case "I_Soldier_A_F":
	{
		removeAllWeapons _x;
		removeAllItems _x;
		removeAllAssignedItems _x;
		removeUniform _x;
		removeVest _x;
		removeBackpack _x;
		removeHeadgear _x;
		removeGoggles _x;

		_x forceAddUniform "U_I_CombatUniform";
		for "_i" from 1 to 2 do {_x addItemToUniform "Chemlight_green";};
		for "_i" from 1 to 2 do {_x addItemToUniform "30Rnd_556x45_Stanag";};
		_x addVest "V_PlateCarrierIA1_dgtl";
		for "_i" from 1 to 2 do {_x addItemToVest "SmokeShell";};
		for "_i" from 1 to 6 do {_x addItemToVest "30Rnd_556x45_Stanag";};
		_x addItemToVest "HandGrenade";
		_x addItemToVest "MiniGrenade";
		_x addBackpack "B_Carryall_oli";
		for "_i" from 1 to 4 do {_x addItemToBackpack "30Rnd_556x45_Stanag";};
		for "_i" from 1 to 2 do {_x addItemToBackpack "200Rnd_65x39_cased_Box";};
		for "_i" from 1 to 1 do {_x addItemToBackpack "NLAW_F";};
		_x addHeadgear "H_HelmetIA";

		_x addWeapon "arifle_Mk20_F";
		_x addPrimaryWeaponItem "optic_Aco";

		_x linkItem "ItemMap";
		_x linkItem "ItemCompass";
		_x linkItem "ItemWatch";
		_x linkItem "ItemRadio";
	};
	//COMBAT LIFE SAVER
	case "I_medic_F":
	{
		removeAllWeapons _x;
		removeAllItems _x;
		removeAllAssignedItems _x;
		removeUniform _x;
		removeVest _x;
		removeBackpack _x;
		removeHeadgear _x;
		removeGoggles _x;

		comment "Add containers";
		_x forceAddUniform "U_I_CombatUniform";
		for "_i" from 1 to 2 do {_x addItemToUniform "Chemlight_green";};
		for "_i" from 1 to 2 do {_x addItemToUniform "30Rnd_556x45_Stanag";};
		for "_i" from 1 to 2 do {_x addItemToUniform "SmokeShell";};
		_x addVest "V_PlateCarrierIA1_dgtl";
		_x addItemToVest "MiniGrenade";
		_x addItemToVest "HandGrenade";
		for "_i" from 1 to 5 do {_x addItemToVest "30Rnd_556x45_Stanag";};
		for "_i" from 1 to 2 do {_x addItemToVest "SmokeShell";};
		_x addBackpack "B_AssaultPack_dgtl";
		_x addItemToBackpack "Medikit";
		for "_i" from 1 to 8 do {_x addItemToBackpack "FirstAidKit";};
		_x addHeadgear "H_HelmetIA";

		comment "Add weapons";
		_x addWeapon "arifle_Mk20_F";
		_x addPrimaryWeaponItem "optic_Aco";

		comment "Add items";
		_x linkItem "ItemMap";
		_x linkItem "ItemCompass";
		_x linkItem "ItemWatch";
		_x linkItem "ItemRadio";
	};
	//HELICOPTER PILOT
	case "I_helipilot_F":
	{
		removeAllWeapons _x;
		removeAllItems _x;
		removeAllAssignedItems _x;
		removeUniform _x;
		removeVest _x;
		removeBackpack _x;
		removeHeadgear _x;
		removeGoggles _x;

		_x forceAddUniform "U_I_HeliPilotCoveralls";
		for "_i" from 1 to 3 do {_x addItemToUniform "9Rnd_45ACP_Mag";};
		_x addVest "V_TacVest_khk";
		_x addItemToVest "SmokeShellGreen";
		for "_i" from 1 to 2 do {_x addItemToVest "Chemlight_green";};
		for "_i" from 1 to 4 do {_x addItemToVest "30Rnd_556x45_Stanag";};
		for "_i" from 1 to 2 do {_x addItemToVest "SmokeShellPurple";};
		_x addHeadgear "H_PilotHelmetHeli_I";

		_x addWeapon "arifle_Mk20C_plain_F";
		_x addPrimaryWeaponItem "acc_flashlight";
		_x addPrimaryWeaponItem "optic_Aco";
		_x addWeapon "hgun_ACPC2_F";

		_x linkItem "ItemMap";
		_x linkItem "ItemCompass";
		_x linkItem "ItemWatch";
		_x linkItem "ItemRadio";
		_x linkItem "ItemGPS";
		_x linkItem "NVGoggles_INDEP";
	};
}
} forEach allUnits;

Regards

Share this post


Link to post
Share on other sites

I don't mean to shamelessly advertise but the functions in my signature include a template and a script for producing your own functions quicker, it's almost exactly the same idea, even if you just use the template you can keep your layout but it'll save you looking up all the classnames (some of the independent ones might need checking).

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
Sign in to follow this  

×