Jump to content
Sign in to follow this  
tharawdeal

Help naming units created by a createUnit forEach loop.

Recommended Posts

The Scenario:

I have a number of defensive sandbag positions that I want to be occupied by Taki Militiamen of random types.

The Setup:

Each sandbag position has a marker behind it where a defender will be placed. The names of the markers are stored in an array. I am spawning the defenders using a createUnit command within a forEach loop. This puts one defender of a random type at every marker in the array. So far, so good.

The Problem:

Not all the sandbags are facing the same way. Therefore, when the defenders spawn, I need a way to change the direction they are facing so that they don't have their backs turned to the places they are supposed to be watching. Also, I want to be able to give them other instructions after they are spawned. Now, in order to give these instructions to each defender, I need to give each man a name that I can reference. Using createUnit, I can give a created unit a name by putting, for example, Bob = this; in the createUnit init line. However, because I am creating each defender with a loop, I can't put Bob = this; in every init line, because the script would attempt to give each unit the same name. What I need is a way to give each created man a different name. I have tried a few different ways of doing this, none of which have worked so far. Here is what I have:

//Array of sandbag defense marker names.

_sbList = ["sb1", "sb2", "sb3", "sb4", "sb5", "sb6", "sb7", "sb8", "sb9", "sb10", "sb11", "sb12", "sb13", "sb14", "sb15", "sb16", "sb17", "sb18", "sb19", "sb20", "sb21", "sb22", "sb23", "sb24", "sb25", "sb26", "sb27", "sb28", "sb29"];

_i = 0;

{

//Names for the defenders.

_defNameList = ["sb1Def", "sb2Def", "sb3Def", "sb4Def", "sb5Def", "sb6Def", "sb7Def", "sb8Def", "sb9Def", "sb10Def", "sb11Def", "sb12Def", "sb13Def", "sb14Def", "sb15Def", "sb16Def", "sb17Def", "sb18Def", "sb19Def", "sb20Def", "sb21Def", "sb22Def", "sb23Def", "sb24Def", "sb25Def", "sb26Def", "sb27Def", "sb28Def", "sb29Def"];

_defType = ["TK_INS_Soldier_AR_EP1", "TK_INS_Bonesetter_EP1", "TK_INS_Soldier_2_EP1", "TK_INS_Soldier_EP1", "TK_INS_Soldier_4_EP1", "TK_INS_Soldier_3_EP1", "TK_INS_Soldier_AAT_EP1", "TK_INS_Soldier_AT_EP1"] call BIS_fnc_selectRandom;

defName = _defNameList select _i;

_defType createUnit [getMarkerPos _x, sbGroup, "this setPos (getMarkerPos _x); defName = this"];

_i = _i +1;

}

forEach _sbList;

This doesn't throw any errors until I try to tell one of the units to do something, like sb1Def setDir 90;. Then I get an error saying that sb1Def was not defined. The arma2oa.rpt also contains 29 instances of warnings saying that various units don't have names. These both point to the names not being assigned to the created units. I'm not sure where I'm going wrong, or if perhaps the createUnit init line won't accept naming anything other than in the style of unitName = this;. How can I do this without having to resort to brute force copy/paste for all 29 defenders? Thanks.

Share this post


Link to post
Share on other sites

The problem is that you're assigning defName as the unit's variable. Do this instead:

_defType createUnit [getMarkerPos _x, sbGroup, format ["this setPos (getMarkerPos _x); %1 = this", _defNameList select _i]];

Share this post


Link to post
Share on other sites

Yeah, I had tried formatting it before but I didn't do it correctly. Anyhow, it works! Thanks very much.

Share this post


Link to post
Share on other sites

If you already have an array for markers, why dont you put the units into an array as well? Either into the same, multidimensional, array or two separate and have the index of them match.

Share this post


Link to post
Share on other sites

CASE

Spawning AI units in a "for _i from 0 to XX do"-loop and giving each one a unique name so they can be called from a IE a trigger later.

PROBLEM

Cannot get the code to loop properly. Name not given, except as a variable (me thinks). It works when I give the unitname for each one manually.

SCRIPT THAT WORKS

convoySpawn.sqf (ran on HC if available, if not on server):

_pauseSmall = 3; 	//should just be a breather for the server in a scriptloop (pause between each spawn)
_Spawn1 = "cSpawn1"; 	//Spawn pos #1
_Spawn2 = "cSpawn2"; 	//Spawn pos #2
_typeOfVeh = "B_Truck_01_box_F"; // https://community.bistudio.com/wiki/Arma_3_CfgVehicles_EAST#O_MBT_02_arty_F

//Scriptstart:
//First vehicle:
_veh = createVehicle [_typeOfVeh, (getMarkerPos _Spawn1), [], 0, "NONE"];
createVehicleCrew _veh;
{
	diag_log [_x, faction _x, side _x, side group _x];
} forEach crew _veh;
c1 = _veh;
publicVariable "c1";
sleep _pauseSmall;

//Second vehicle:
_veh = createVehicle [_typeOfVeh, (getMarkerPos _Spawn2), [], 0, "NONE"];
createVehicleCrew _veh;
{
	diag_log [_x, faction _x, side _x, side group _x];
} forEach crew _veh;
c2 = _veh;
publicVariable "c2";
sleep _pauseSmall;

//etc....

SCRIPT THAT DOES NOT WORK (they spawn, but without names)

_pauseSmall = 3;
_typeOfVeh = "B_Truck_01_box_F"; // https://community.bistudio.com/wiki/Arma_3_CfgVehicles_EAST#O_MBT_02_arty_F
_unitNames = ["c1","c2","c3","c4","c5"];
//_unitNames = [c1,c2,c3,c4,c5];

//Scriptstart:
_countUnits = (count _unitNames)-1;
_unitCounter = 0;
for "_i" from 0 to _countUnits do
{
	_unitName = format [_unitNames select _unitCounter];
	Hint _unitName;
	sleep _pauseSmall;
	_veh = createVehicle [_typeOfVeh, (getMarkerPos _Spawn1), [], 0, "NONE"];
	createVehicleCrew _veh;
		{
			diag_log [_x, faction _x, side _x, side group _x];
		} forEach crew _veh;
	_unitName = _veh;
	_unitCounter = _unitCounter +1;
	Hint format ["%1 ",_unitName];
	publicVariable format ["%1 ",_unitName];
	sleep _pauseSmall;
};

SOLUTION

Maybe use format differently to be able to name the units correctly.

Note: I'm aware of the spawnlocations, that they will spawn on the same location. I'll solve that later.

Any help would be much appreciated.

Share this post


Link to post
Share on other sites
_pauseSmall = 3;
_typeOfVeh = "B_Truck_01_box_F"; // https://community.bistudio.com/wiki/Arma_3_CfgVehicles_EAST#O_MBT_02_arty_F
_unitNames = ["c1","c2","c3","c4","c5"];
//_unitNames = [c1,c2,c3,c4,c5];

//Scriptstart:
_countUnits = count _unitNames;
for "_i" from 0 to (_countUnits - 1) do
{
	hint (_unitNames select _i);
	sleep _pauseSmall;
	_veh = createVehicle [_typeOfVeh, (getMarkerPos _Spawn1), [], 0, "NONE"];
	createVehicleCrew _veh;
		{
			diag_log (format ["%1, %2, %3, %4", _x, faction _x, side _x, side (group _x)]);
		} forEach (crew _veh);
	unitName = str _veh;
	hint unitName;
	publicVariable "unitName";
	sleep _pauseSmall;
};

Share this post


Link to post
Share on other sites

Thanks a lot, but unfortunately it did not apply.

This produced the correct name (c1, c2 etc.):

hint (_unitNames select _i);

This provided the other names (B Alpha 1-2-1, B Alpha 1-3:1 etc.):

unitName = str _veh;
	hint unitName;
	publicVariable "unitName";

This given, the wrong names are being publicVariable'd.

Share this post


Link to post
Share on other sites
_pauseSmall = 3;
_typeOfVeh = "B_Truck_01_box_F"; // https://community.bistudio.com/wiki/Arma_3_CfgVehicles_EAST#O_MBT_02_arty_F
_unitNames = ["c1","c2","c3","c4","c5"];
//_unitNames = [c1,c2,c3,c4,c5];

//Scriptstart:
_countUnits = count _unitNames;
for "_i" from 0 to (_countUnits - 1) do
{
	unitName = _unitNames select _i;
	hint unitName;
	sleep _pauseSmall;
	_veh = createVehicle [_typeOfVeh, (getMarkerPos _Spawn1), [], 0, "NONE"];
	createVehicleCrew _veh;
		{
			diag_log (format ["%1, %2, %3, %4", _x, faction _x, side _x, side (group _x)]);
		} forEach (crew _veh);
	hint unitName;
	publicVariable "unitName";
	sleep _pauseSmall;
};

Share this post


Link to post
Share on other sites

unitName and _veh are never connected.

I tried this, after vehicle creation:

unitName = str _veh;

But that creates the same "B Alpha 1-2-1"-names.

Tried:

unitName = _veh;

"Error hint: Type object, expected String, text" - script aborted.

Share this post


Link to post
Share on other sites
#define PAUSE_TIME 3

{
_variableName = _x select 0;
_markerName = _x select 1;

hint _variableName;

sleep PAUSE_TIME;

_veh = createVehicle ["B_Truck_01_box_F", getMarkerPos _markerName, [], 0, "NONE"];

{diag_log (format ["%1, %2, %3, %4", _x, faction _x, side _x, side (group _x)])} forEach (crew _veh);

hint _variableName;

call (compile (format ["%1 = %2", _variableName, _veh]));

publicVariable _variableName;

sleep PAUSE_TIME;
}
forEach [
["c1", "marker1"],
["c2", "marker2"],
["c3", "marker3"],
["c4", "marker4"],
["c5", "marker5"]
];

Edited by Schatten

Share this post


Link to post
Share on other sites

That didn't work either, I got ""B_Truck_01_box_F" missing ;" error somehow.

Thanks for trying Schatten, I appreciate it. But if it is this hard I'll find another way of solving it.

I'll get back to the topic if I find a solution.

Share this post


Link to post
Share on other sites

My last solution should work.

In your example (convoySpawn.sqf), variables (c1, c2, ...) are assigned to objects (c1 = _veh, ...) and its are made public. The same is done in my solution too.

_pauseSmall = 3;
_typeOfVeh = "B_Truck_01_box_F"; // https://community.bistudio.com/wiki/Arma_3_CfgVehicles_EAST#O_MBT_02_arty_F
_unitNames = ["c1","c2","c3","c4","c5"];
//_unitNames = [c1,c2,c3,c4,c5];

//Scriptstart:
_countUnits = count _unitNames;
for "_i" from 0 to (_countUnits - 1) do
{
	_unitName = _unitNames select _i;
	hint _unitName;
	sleep _pauseSmall;
	_veh = createVehicle [_typeOfVeh, (getMarkerPos _Spawn1), [], 0, "NONE"];
	createVehicleCrew _veh;
		{
			diag_log (format ["%1, %2, %3, %4", _x, faction _x, side _x, side (group _x)]);
		} forEach (crew _veh);
	hint _unitName;
	call (compile (_unitName + " = _veh"));
	publicVariable _unitName;
	sleep _pauseSmall;
};

or

#define PAUSE_TIME 3

{
_variableName = _x select 0;
_markerName = _x select 1;

hint _variableName;

sleep PAUSE_TIME;

_veh = createVehicle ["B_Truck_01_box_F", getMarkerPos _markerName, [], 0, "NONE"];

{diag_log (format ["%1, %2, %3, %4", _x, faction _x, side _x, side (group _x)])} forEach (crew _veh);

hint _variableName;

call (compile (_variableName + " = _veh"));

publicVariable _variableName;

sleep PAUSE_TIME;
}
forEach [
["c1", "marker1"],
["c2", "marker2"],
["c3", "marker3"],
["c4", "marker4"],
["c5", "marker5"]
];

Edited by Schatten
Fixed some errors

Share this post


Link to post
Share on other sites

Sir, your last edition works well.

Thank you very much for your time!

Edited by Rawhide2

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  

×