Jump to content
Sign in to follow this  
BEAKSBY

Arrays and for do help

Recommended Posts

How would I create something like the following using for do and arrays?

I'm not sure how to create a series of variables with _i in them?

veh0 = _veh0;
veh1 = _veh1;
veh2 = _veh2;
//... n times

and how would I do the following:

_posDrop0 = getPosATL veh0;
_posDrop1 = getPosATL veh1;
_posDrop2 = getPosATL veh2;
//... n times

and

_veh0 = createMine ["APERSMine",_posDrop0,  [], 0];
_veh1 = createMine ["APERSMine",_posDrop1,  [], 0];
_veh2 = createMine ["APERSMine",_posDrop2,  [], 0];
//... n times

Share this post


Link to post
Share on other sites

missionNamespace setVariable and forEach

//put all _posDrop's in an array
_outputArray = [];
{
missionNamespace setVariable[format["_veh%1",_forEachIndex], createMine ["APERSMine", _x,  [], 0]];
_outputArray pushBack [format["_veh%1", _forEachIndex];
}forEach _posDropArray;

EDIT: I didn't see there was more to your post. So basically, any time you write a spawning script, it should return an array with the objects spawned for easy script-transferring. That way you eliminate weird stuff like this:

veh0 = _veh0; 
veh1 = _veh1; 
veh2 = _veh2;

and you can get the positions of all the vehicles with another simple loop that's essentially the same as the first code, using the array of variable strings that we output

{
missionNamespace setVariable[format["_posDrop%1",_forEachIndex], call compile format["getPosATL %1", _x]];
}forEach _outputArray;

Yeah...that might just be crazy enough to work.

---------- Post added at 23:04 ---------- Previous post was at 22:28 ----------

Let's try another approach, because I really wasn't paying enough attention while I wrote my first post. Things were done in a weird way. Let's start with the way you're spawning units. You should always return an array with those units inside, unless you don't plan on ever needing to run code on them that can't be run during spawning. Something like this:

//fictional function
//let's say the first parameter is unit to spawn and second parameter is how many.
_vehArray = ["B_SOLDIER_F", 6] call DREAD_spawnUnits;

Now that you have an array with all of the units, you can easily get the positions of each with:

_posArray = [];
{
_posArray pushBack [getPosATL _x];
}forEach _vehArray;
//returns an array with all the positions

You can even write a function for that, feed the first array in, and get the position array from the output.

Now for the fun part, where we create the mines and give them variables. For ease of understanding, let's say I turned the above code into a function, got the array of positions and named it _posArray. Now we would be able to use something like in my first post to create dynamic variables.

_outputArray = [];
{
   missionNamespace setVariable[format["_veh%1",_forEachIndex], createMine ["APERSMine", _x,  [], 0]];
   _outputArray pushBack [format["_veh%1", _forEachIndex];
}forEach _posArray;

So now what you've got an array of strings that each have the same name as variables, which at any time you can use something like:

_obj = missionNamespace getVariable (_outputArray select 3);

And now you have an actual variable, which you can use to do code with.

(All of this should be considered "At least in theory", I have never gotten a setup this crazy to work. And I've been trying for 185 posts worth of time)

Edited by DreadedEntity

Share this post


Link to post
Share on other sites

OK thanks Entity...

I managed to get by with the second part of your post, using a combination of for dos, forEachs and remebering that I can use the vehicle created as an element for creating an array of vehicles with pushBacks.

Share this post


Link to post
Share on other sites
How would I create something like the following using for do and arrays?

I'm not sure how to create a series of variables with _i in them?

Spawn 3 mines at 3 vehicle's positions:

_vehArray = [veh0, veh1, veh2];

for "_i" from 0 to (count _vehArray) - 1 do {
call compile format ["_veh%1 = createMine ['APERSMine', getPosATL veh%1, [], 0]", _i];
};

/* ---- OR . . .

for "_i" from 0 to (count _vehArray) - 1 do {
call compile format ["_veh%1 = createMine ['APERSMine', getPosATL (_vehArray select _i), [], 0]", _i];
};

*/

/* ---- OR . . . 

for "_i" from 0 to 3 - 1 do { //Use a number without array
   call compile format ["_veh%1 = createMine ['APERSMine', getPosATL veh%1, [], 0]", _i];
};

*/

Or if you want to define the _posDrop's positions too:

_vehArray = [veh0, veh1, veh2];

for "_i" from 0 to (count _vehArray) - 1 do {
call compile format ["
	_posDrop%1 = getPosATL (_vehArray select _i);
	_veh%1 = createMine ['APERSMine', _posDrop%1, [], 0];
	", 
_i];
};

And if you need to store the positions and mines into an array:

_posArray = [];
_mineArray = [];
_vehArray = [veh0, veh1, veh2];

for "_i" from 0 to (count _vehArray) - 1 do {
call compile format ["
	_posDrop%1 = getPosATL (_vehArray select _i); 
	_veh%1 = createMine ['APERSMine', _posDrop%1, [], 0];
	_posArray set [count _posArray, _posDrop%1];
	_mineArray set [count _mineArray, _veh%1];
   ", 
_i];
};

And... if you want to create a "dynamic" vehicle array, then fetch nearby land vehicles with nearestObjects instead of manually defining the array >> _vehArray = [veh0, veh1, veh2]:

// Create a mine at every vehicle position within 200m of myPos

_posArray = [];
_mineArray = [];
_vehArray = nearestObjects [myPos, ["landVehicle"], 200];

for "_i" from 0 to (count _vehArray) - 1 do {
call compile format ["
	_posDrop%1 = getPosATL (_vehArray select _i); 
	_veh%1 = createMine ['APERSMine', _posDrop%1, [], 0];
	_posArray set [count _posArray, _posDrop%1];
	_mineArray set [count _mineArray, _veh%1];
   ", 
_i];
};

Hope that helps. Cheers.

Edited by Iceman77

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  

×