Jump to content
Sign in to follow this  
jshock

Deleting Groups from Nested Arrays

Recommended Posts

Ok so, I have numerous functions in use that spawn units into a particular AO, those units and groups as they are spawned are pushBack into a global array to be referenced later in a delete function. The delete function basically takes all the cached units and then deletes them, or if the units are null then remove them from the array.

So basically the problem is that I have a lot of nested arrays within that global array and I'm not quite sure how to handle it properly, here is what I have so far (global array elements included are just for example purposes):

SHK_deleteArray = [[unit1, unit2, unit3],[unit4, unit5, unit6],[unit7]];

{

{
	if (isNull _x) then {

	SHK_deleteArray =  SHK_deleteArray - [_x];

} else {

	deleteVehicle _x;

};

} forEach (SHK_deleteArray select 0);

} forEach SHK_deleteArray;

if (count SHK_deleteArray > 0 ) exitWith {hint "Unable to delete all elements from SHK_deleteArray";};

Share this post


Link to post
Share on other sites

Use _forEachIndex

SHK_deleteArray = [[unit1, unit2, unit3],[unit4, unit5, unit6],[unit7]];

{
_index = _forEachIndex
   {
       if (isNull _x) then {

       SHK_deleteArray =  SHK_deleteArray - [_x];

   } else {

       deleteVehicle _x;

   };

   } forEach (SHK_deleteArray select _index);

} forEach SHK_deleteArray;

if (count SHK_deleteArray > 0 ) exitWith {hint "Unable to delete all elements from SHK_deleteArray";}; 

Share this post


Link to post
Share on other sites

Thanks Iceman, will give it a go.

Share this post


Link to post
Share on other sites

No worries. I was tinkering with your code. Couldn't it simply be? :

SHK_deleteArray = [[unit1, unit2, unit3],[unit4, unit5, unit6],[unit7]];

{
_index = _forEachIndex
   {
      deleteVehicle _x;
	SHK_deleteArray =  SHK_deleteArray - [_x]; // Update the array as you delete units ??
} forEach (SHK_deleteArray select _index);

} forEach SHK_deleteArray;

if (count SHK_deleteArray > 0 ) exitWith {hint "Unable to delete all elements from SHK_deleteArray";}; 

Cheers.

Edited by Iceman77

Share this post


Link to post
Share on other sites
No worries. I was tinkering with your code. Couldn't it simply be:

I thought that at first but I have a hint that displays all elements after deletion and it would pop up as:

[[NULL OBJECT, NULL OBJECT], [unit2, unit3]]

So it was more of "cleaning" the array out completely, as well as if any of the units are killed after being pushed into the array they are now nulled. I don't know if it matters but I put it there anyhow.

Share this post


Link to post
Share on other sites

Ahh forgot to mention.. _forEachIndex is a magic variable that holds the current iteration index (number) of the foreach loop.

I thought that at first but I have a hint that displays all elements after deletion and it would pop up as:

[[NULL OBJECT, NULL OBJECT], [unit2, unit3]]

So it was more of "cleaning" the array out completely, as well as if any of the units are killed after being pushed into the array they are now nulled. I don't know if it matters but I put it there anyhow.

Gotcha.

Share this post


Link to post
Share on other sites
Ahh forgot to mention.. _forEachIndex is a magic variable that holds the current iteration (number) of the foreach loop.

Yea I remember seeing that somewhere in my ventures, thanks for explanation never the less :p.

Share this post


Link to post
Share on other sites

Works perfectly btw Iceman, thanks again for your help, I knew that there was something that was needed indexing side, just didn't think index of the for loop :p.

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  

×