Jump to content
Sign in to follow this  
Woodpeckersam

Grabbing another unit's and checking if it does not have a part.

Recommended Posts

Hello, I have been stuck with this problem most of the day.

On the mission the player's team starts in enemy infested waters as they have left a submarine (long gone at this stage of the mission as there are no models of one yet).

Upon arriving to land there are a few enemy divers preparing to get into the waters to lay mines, they have left a Nato Ammo Crate and a truck behind, but the player's team must kill them to carry on the mission.

The player is the leader controlling 3 AI units.

I really wanted to simulate AI getting out of the Diver's suit and changing into gear that is inside the Ammo Crate. But since this may never be an official feature, I tried to script it the best looking way I can.

Seeing that the AI will never be able to get gear out of Ammo crates, I had an array of 4 different types of soldiers. I put each of their gear into an array and made it so that when a teammate gets to the Ammo Crate, it will randomly choose Uniform, HeadGear, BackPack and Vests out of the units in the array (they are well off map so they will never be seen).

However I have a problem. I have no way, and know no way of checking if the randomly chosen unit has lets say a backpack or not.

Because when this scripts runs, randomly it will work 100% but maybe on the 3rd unit it will fail as it would appear as "Undefined variable given for _backpack" for example, which leads me to suggest maybe the unit does not have a backpack, therefore it errors out and stops the code from continuing.

Here is the code, can anyone help?

_unitArray = [Marksman,Rifleman,Grenadier,Engineer]; //4 units off map used for randomly getting gear/clothing for use in variables to equip teammates

//Loop from 1 to 3 to be used with selecting individual group members. Changing it from 1 to 0 would select the leader too.
For "_i" from 1 to 3 do {

//Select a group member in sequence from unit 1 to unit 3 NOT unit 0 to unit 3 as unit 0 is the group leader (player)
_team = units group player select _i;

//Get the current clothing that _unitGuy is wearing
_clothes = uniform (_unitArray select (random(count _unitArray)));
_vest = vest (_unitArray select (random (count _unitArray)));
_head = headgear (_unitArray select (random (count _unitArray)));
_backpack = backpack (_unitArray select (random (count _unitArray)));

_team commandMove getPos Crate;
waitUntil {_team distance Crate <= 5};
sleep 2;

//Remove the teammates uniform and weapons
removeAllWeapons _team;
removeUniform _team;
sleep (random 0.3);

removeAllContainers _team;
sleep (random 0.3);

removeAllAssignedItems _team;
sleep (random 0.3);

_team addUniform _clothes;
sleep 0.5;
_team addVest _vest;
sleep 0.5;
_team addBackpack _backpack;
sleep 0.5;
_team addHeadGear _head;
sleep 0.5;
};

Edited by ArmASalt3

Share this post


Link to post
Share on other sites

If the unit does not have a backpack, the backpack command should return an empty String (""). What you could do is check for this before calling addBackpack. Like this:

if (_backpack != "") then {_team addBackpack _backpack;};

EDIT: This will probably be even more secure, since it makes sure that _backpack is a non-zero-length string.

if ((typeName _backpack) == "STRING" && _backpack != "") then {_team addBackpack _backpack;};

Edited by MadDogX

Share this post


Link to post
Share on other sites

Thanks man, I did try it similar that way, except having "" i added nil and isNil. I should have the brains to know that "" works. The updated code you posted works even better. Many thanks man.

---------- Post added at 15:24 ---------- Previous post was at 14:21 ----------

I thought it works, it usually does so these errors are pretty random. I dont even understand what its telling me, this is what I get on random occasions, not every play through, but quite often.

Error: Undefined variable in expressions (either _mags or _backpack)

This is my code now

_unitArray = [Marksman,Rifleman,Grenadier,Engineer];
_uniformArray = ["U_B_CombatUniform_mcam","U_B_CombatUniform_mcam_tshirt","U_B_CombatUniform_mcam_vest"];
_wpncfg = configFile >> "CfgWeapons";

//Loop from 1 to 3 to be used with selecting individual group members. Changing it from 1 to 0 would select the leader too.
For "_i" from 1 to 3 do {

_wpncount = (random (count _wpncfg));
_wpnselect = _wpncfg select _wpncount;
_wpnname = configName _wpnselect;

//Select a group member in sequence from unit 1 to unit 3 NOT unit 0 to unit 3 as unit 0 is the group leader (player)
_team = units group player select _i;

//Get the current clothing that _unitGuy is wearing
_vest = vest (_unitArray select (random (count _unitArray)));
_head = headgear (_unitArray select (random (count _unitArray)));
_backpack = backpack (_unitArray select (random (count _unitArray)));

_team doMove (getMarkerPos "Crate");
waitUntil {_team distance (getMarkerPos "Crate") <= 4};
sleep 2;

//Remove the teammates uniform and weapons
removeAllWeapons _team;
removeUniform _team;
sleep (random 0.3);

removeAllContainers _team;
sleep (random 0.3);

removeAllAssignedItems _team;
sleep (random 0.3);

_team addUniform (_uniformArray select (random(count _uniformArray)));
sleep 0.5;
if ((typeName _vest) == "STRING" && _vest != "") then {_team addVest _vest};
sleep 0.5;
if ((typeName _backpack) == "STRING" && _backpack != "") then {_team addBackpack _backpack};
sleep 0.5;
if ((typeName _head) == "STRING" && _head != "") then {_team addHeadGear _head};
sleep 0.5;
_team addWeapon _wpnname;
sleep 0.5;

_curweap = currentWeapon _team;

_ammocfg = getArray (configFile >> "CfgWeapons" >> _curweap >> "magazines");
_mags = _ammocfg select 0;
for "_i" from 0 to 5 do {_team addMagazine _mags};

};

Does anyone have any idea what is going on?

Share this post


Link to post
Share on other sites

I have changed the script heavily and I am still receiving the

Error: Undefined variable in expressions (this time its _ammo or _backpackselect)

This time it is not getting info out of a config, but straight from an array itself. Can someone explain to me why I am getting this error?

Here is the updated script. (Too long I know, but the best I can do)

_uniform = [
		"U_B_CombatUniform_mcam",
		"U_B_CombatUniform_mcam_tshirt",
		"U_B_CombatUniform_mcam_vest",
		"U_B_CombatUniform_mcam_worn",
		"U_B_GhillieSuit"
		];

_backpack = [
		"B_AssaultPack_khk",
		"B_AssaultPack_khk_holder",
		"B_AssaultPack_khk_Ammo",
		"B_AssaultPack_khk_Medic",
		"B_AssaultPack_dgtl",
		"B_AssaultPack_dgtl_AAR",
		"B_AssaultPack_dgtl_Spotter",
		"B_AssaultPack_rgr",
		"B_AssaultPack_rgr_AT",
		"B_AssaultPack_rgr_Medic",
		"B_AssaultPack_rgr_ReconMedic",
		"B_AssaultPack_rgr_Repair",
		"B_AssaultPack_cbr"
		];

_vest = [
	"V_Rangemaster_belt", 
	"V_BandollierB_khk", 
	"V_BandollierB_rgr", 
	"V_BandollierB_cbr", 
	"V_PlateCarrier1_rgr", 
	"V_PlateCarrier1_cbr",  
	"V_PlateCarrier2_rgr", 
	"V_PlateCarrier2_cbr", 
	"V_PlateCarrierGL_cbr",  
	"V_PlateCarrierSpec_rgr", 
	"V_PlateCarrierSpec_cbr", 
	"V_Chestrig_khk", 
	"V_ChestrigB_rgr", 
	"V_ChestrigB_khk", 
	"V_Chestrig_camo", 
	"V_TacVest_khk", 
	"V_TacVest_brn", 
	"V_TacVest_camo", 
	"V_TacVest_oli", 
	"V_TacVestIR_blk",
	"V_HarnessO_brn", 
	"V_HarnessOGL_brn", 
	"V_HarnessOSpec_brn", 
	"V_TacVestCamo_khk", 
	"V_PlateCarrierIA1_khk", 
	"V_PlateCarrierIA2_khk", 
	"V_PlateCarrierIAGL_khk"
	];

_helmet = ["H_HelmetB",
	"H_HelmetB_paint",
	"H_HelmetB_light",
	"H_HelmetB_plain_mcamo", 
	"H_HelmetB_plain_blk", 
	"H_HelmetIA", 
	"H_HelmetI_net", 
	"H_HelmetI_camo", 
	"H_MilCap_ocamo", 
	"H_MilCap_oucamo", 
	"H_MilCap_rucamo", 
	"H_MilCap_mcamo", 
	"H_MilCap_gry", 
	"H_MilCap_dgtl", 
	"H_MilCap_blue", 
	"H_MilCap_chck1", 
	"H_MilCap_chck2", 
	"H_MilCap_chck3"
	];

_goggle = [
		"None",
		"G_Shades_Black",
		"None",
		"G_Shades_Blue",
		"None",
		"G_Sport_Blackred",
		"None",
		"G_Tactical_Clear",
		"None"
		];

_weapon = [
	//EBR Sniper Rifles
		"srifle_EBR_F",
		"srifle_EBR_ACO_grip_F",
		"srifle_EBR_ACO_point_grip_F",
		"srifle_EBR_Holo_grip_F",
		"srifle_EBR_Holo_point_grip_F",
		"srifle_EBR_ACOg_grip_F",
		"srifle_EBR_ACOg_point_grip_F",
		"srifle_EBR_ARCO_grip_F",
		"srifle_EBR_ARCO_point_grip_F",

	//Light Machine Guns
		"LMG_Mk200_F",
		"LMG_Mk200_ARCO_bipod_F",
		"LMG_Mk200_ARCO_pointer_bipod_F",
		"LMG_Mk200_COWS_pointer_bipod_F",
		"LMG_Mk200_ACO_grip_F",
		"LMG_Mk200_ACO_point_gripf_F",

	//MX
		"arifle_MXC_F",
		"arifle_MX_F",
		"arifle_MX_GL_F",
		"arifle_MX_SW_F",
		"arifle_MXM_F",
		"arifle_MX_ARCO_point_grip_F",
		"arifle_MX_Hamr_point_grip_F",
		"arifle_MX_ACO_point_gripod_mzls_F",
		"arifle_MX_ACOg_point_gripod_mzls_F",
		"arifle_MX_ACO_point_grip_F",
		"arifle_MX_ACOg_point_grip_F",
		"arifle_MX_GL_ACO_point_F",
		"arifle_MX_GL_ACOg_point_F",
		"arifle_MX_GL_ARCO_point_mzls_F",
		"arifle_MX_GL_Hamr_point_mzls_F",
		"arifle_MXC_Holo_F",
		"arifle_MXC_Holo_point_grip_F",
		"arifle_MXC_Holo_point_grip_snds_F",
		"arifle_MXC_ACO_point_grip_mzls_F",
		"arifle_MXC_ACOg_point_grip_mzls_F",
		"arifle_MXC_ACO_point_grip_F",
		"arifle_MXC_ACOg_point_grip_F",
		"arifle_MXC_ACO_flash_grip_mzls_F",
		"arifle_MXC_ACOg_flash_grip_mzls_F",
		"arifle_MX_SW_ARCO_point_gripod_F",
		"arifle_MX_SW_Hamr_point_gripod_F",
		"arifle_MXM_ARCO_point_gripod_F",
		"arifle_MXM_Hamr_point_gripod_F"
		];

_ammo = [
	//EBR Sniper Rifles
		"20Rnd_762x45_Mag",
		"20Rnd_762x45_Mag",
		"20Rnd_762x45_Mag",
		"20Rnd_762x45_Mag",
		"20Rnd_762x45_Mag",
		"20Rnd_762x45_Mag",
		"20Rnd_762x45_Mag",
		"20Rnd_762x45_Mag",
		"20Rnd_762x45_Mag",

	//Light Machine Guns
		"5000Rnd_762x45_Belt",
		"5000Rnd_762x45_Belt",
		"5000Rnd_762x45_Belt",
		"5000Rnd_762x45_Belt",
		"5000Rnd_762x45_Belt",
		"5000Rnd_762x45_Belt",

	//MX
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green",
		"30Rnd_65x39_case_mag_green"
	];

_alive = count units group player;

//Loop from 1 to 3 to be used with selecting individual group members. Changing it from 1 to 0 would select the leader too.
For "_i" from 1 to _alive do {

_weaponcount = (random (count _weapon));
_weaponselect = _weapon select _weaponcount;

_uniformcount = (random (count _uniform));
_uniformselect = _uniform select _uniformcount;

_backpackcount = (random (count _backpack));
_backpackselect = _backpack select _backpackcount;

_vestcount = (random (count _vest));
_vestselect = _vest select _vestcount;

_helmetcount = (random (count _helmet));
_helmetselect = _helmet select _helmetcount;

_gogglecount = (random (count _goggle));
_goggleselect = _goggle select _gogglecount;

//Select a group member in sequence from unit 1 to unit 3 NOT unit 0 to unit 3 as unit 0 is the group leader (player)
_team = units group player select _i;

_team doMove (getMarkerPos "Crate");
waitUntil {_team distance (getMarkerPos "Crate") <= 4};
sleep 2;

//Remove the teammates uniform and weapons
removeAllWeapons _team;
removeUniform _team;
sleep (random 0.3);

removeAllContainers _team;
sleep (random 0.3);

removeAllAssignedItems _team;
sleep (random 0.3);

_team addUniform _uniformselect;
sleep 0.5;
_team addVest _vestselect;
sleep 0.5;
_team addBackpack _backpackselect;
sleep 0.5;
_team addHeadGear _helmetselect;
sleep 0.5;
_team addWeapon _weaponselect;
sleep 0.5;
_team addGoggles _goggleselect;
sleep 0.5;
_team addMagazine (_ammo select _weaponcount);

};

(ps. I know I have duplicated the ammo many times, I just dont know how to match ammo with a gun, so I copied the amount of ammo to the amount of guns so the correct ammo can be selected with the same _weaponcount as the _weapon array) But it does not work :/

edit: Now it is saying Error: Undefined variable in expressions _team... this is getting weird and out of hand lol.

Edited by ArmASalt3

Share this post


Link to post
Share on other sites

I've got it working. If anyone is interested, here is the script. Feel free to change it around it and use it.

This could be useful for a rearming script in a mission, and using eventHandlers you may be able to change the script to allow a unit to rearm from anywhere.

private ["_unit","_crate","_uniform","_backpack","_vest","_helmet","_goggle","_weapon",
	"_weaponcount","_weaponselect","_uniformcount","_uniformselect","_backpackcount",
	"_backpackselect","_vestcount","_vestselect","_helmetcount","_helmetselect","_gogglecount",
	"_goggleselect","_wearing"];

_unit = _this select 0;
_crate = _this select 1;

//If the unit is equalled to the player, then exit. This is because the player can change weapons/ 
//Gear/Items themselves
if (_unit == player) exitWith {};

//The Uniform is from the UK Special Forces Mod
_uniform = [
		"U_mas_uk_B_CombatUniform_mcam_tshirt",
		"U_mas_uk_B_CombatUniform_mcam_vest",
		"U_mas_uk_B_CombatUniform_mcam"
		];

//Copied the Backpacks that the UK special forces uses by looking at the config in the editor (CTRL+G)		
_backpack = [
		"B_AssaultPack_rgr_Medic",
		"B_Bergen_sgg_Exp",
		"B_AssaultPack_rgr_Repair"
		];

//Copied over the vests the UK Special Forces uses from the config			
_vest = [
	"V_mas_uk_PlateCarrier1_rgr",
	"V_mas_uk_PlateCarrierGL_rgr",
	"V_mas_uk_PlateCarrier2_rgr",
	"V_mas_uk_BandollierB_rgr",
	"V_mas_uk_ChestrigB_rgr"
	];

//Same above with the helmets			
_helmet = [
	"",
	"H_HelmetB",
	"H_mas_uk_HelmetB",
	"H_mas_uk_Cap_headphones",
	"H_mas_uk_HelmetB_paint",
	"H_mas_uk_HelmetB_light",
	"H_mas_uk_MilCap_mcamo",
	""
	];

//An array of Goggles already in the game alongside "none" I dont want my units to always wear 
//goggles.		
_goggle = [
		"None",
		"G_Shades_Black",
		"None",
		"G_Shades_Blue",
		"None",
		"G_Sport_Blackred",
		"None",
		"G_Tactical_Clear",
		"None"
		];

//An array of some of the weapons and their attachments that are currently in the game, mainly 
//BLUFOR related weapons.			
_weapon = [
	//EBR Sniper Rifles
		"srifle_EBR_F",
		"srifle_EBR_ACO_grip_F",
		"srifle_EBR_ACO_point_grip_F",
		"srifle_EBR_Holo_grip_F",
		"srifle_EBR_Holo_point_grip_F",
		"srifle_EBR_ACOg_grip_F",
		"srifle_EBR_ACOg_point_grip_F",
		"srifle_EBR_ARCO_grip_F",
		"srifle_EBR_ARCO_point_grip_F",

	//Light Machine Guns
		"LMG_Mk200_F",
		"LMG_Mk200_ARCO_bipod_F",
		"LMG_Mk200_ARCO_pointer_bipod_F",
		"LMG_Mk200_COWS_pointer_bipod_F",
		"LMG_Mk200_ACO_grip_F",
		"LMG_Mk200_ACO_point_gripf_F",

	//MX
		"arifle_MXC_F",
		"arifle_MX_F",
		"arifle_MX_GL_F",
		"arifle_MX_SW_F",
		"arifle_MXM_F",
		"arifle_MX_ARCO_point_grip_F",
		"arifle_MX_Hamr_point_grip_F",
		"arifle_MX_ACO_point_gripod_mzls_F",
		"arifle_MX_ACOg_point_gripod_mzls_F",
		"arifle_MX_ACO_point_grip_F",
		"arifle_MX_ACOg_point_grip_F",
		"arifle_MX_GL_ACO_point_F",
		"arifle_MX_GL_ACOg_point_F",
		"arifle_MX_GL_ARCO_point_mzls_F",
		"arifle_MX_GL_Hamr_point_mzls_F",
		"arifle_MXC_Holo_F",
		"arifle_MXC_Holo_point_grip_F",
		"arifle_MXC_Holo_point_grip_snds_F",
		"arifle_MXC_ACO_point_grip_mzls_F",
		"arifle_MXC_ACOg_point_grip_mzls_F",
		"arifle_MXC_ACO_point_grip_F",
		"arifle_MXC_ACOg_point_grip_F",
		"arifle_MXC_ACO_flash_grip_mzls_F",
		"arifle_MXC_ACOg_flash_grip_mzls_F",
		"arifle_MX_SW_ARCO_point_gripod_F",
		"arifle_MX_SW_Hamr_point_gripod_F",
		"arifle_MXM_ARCO_point_gripod_F",
		"arifle_MXM_Hamr_point_gripod_F"
		];

//Randomly selects a Backpack in the array and assigns the index number of that Backpack to a
//variable which then can be used for a unit to wear.
_backpackcount = (random (count _backpack));
_backpackselect = _backpack select _backpackcount;

//Randomly selects a Vest in the array and assigns the index number of that Vest to a
//variable which then can be used for a unit to wear.
_vestcount = (random (count _vest));
_vestselect = _vest select _vestcount;

//Randomly selects a Helmet in the array and assigns the index number of that Helmet to a
//variable which then can be used for a unit to wear.
_helmetcount = (random (count _helmet));
_helmetselect = _helmet select _helmetcount;

//Randomly selects Goggles/Glasses in the array and assigns the index number of the Goggles/Glasses 
//to a variable which then can be used for a unit to wear.
_gogglecount = (random (count _goggle));
_goggleselect = _goggle select _gogglecount;

//Commands a unit to move to the crate specified. Depending on if there is a leader available or not
//It will issue different commands to get the unit to move to a specified crate
if (count alive (group _unit) > 1) then {_unit commandMove getPos _crate};
if (count alive (group _unit) == 1) then {_unit doMove getPos _crate};

//Waits until the unit is at a certain distance from the crate.
waitUntil {_unit distance _crate <= 6};
sleep 2;

//Remove the teammates uniform and weapons
removeAllWeapons _unit;
sleep (random 0.3);

removeUniform _unit;
sleep (random 0.3);

removeAllContainers _unit;
sleep (random 0.3);

removeAllAssignedItems _unit;
sleep (random 0.3);

/////////////////////////////////// Uniform Loop ///////////////////////////////////////////////////
_nouniform = uniform _unit; //Gets the current non-uniform of the unit
sleep 0.1;

while {uniform _unit == _nouniform} do //Loop back here if the unit has no uniform. 
{
_uniformcount = (random (count _uniform));
_uniformselect = _uniform select _uniformcount;
_unit addUniform _uniformselect; //Adds uniform from Random Uniform Select
sleep 0.5;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

_unit addVest _vestselect;
sleep 0.5;
_unit addBackpack _backpackselect;
sleep 0.5;
_unit addHeadGear _helmetselect;
sleep 0.5;

/////////////////////////////////// Weapon Loop /////////////////////////////////////////////////////
_noweapon = primaryWeapon _unit; //Gets the current non-weapon of the unit
sleep 0.1;

while {primaryWeapon _unit == _noweapon} do //Loop back here if the unit has no weapon.
{
_weaponcount = (random (count _weapon));
_weaponselect = _weapon select _weaponcount;
_unit addWeapon _weaponselect; //Adds a random weapon from the Weapon Select Array
sleep 0.5;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

_unit addGoggles _goggleselect;
sleep 0.5;

_unit doMove getPos _crate;
_unit setDir (getDir _crate) - (getDir _unit);
sleep 1;

//Rearm at the specified crate
_unit action ["rearm", _crate];

exit;

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  

×