Jump to content
Sign in to follow this  
beta

Removing specific elements of an Array

Recommended Posts

So, I have an array called _array.

This array is populated with units of class SoldierWB (all BLUFOR soldier classes). The problem is, this also includes corpses and that is causing problems for my script.

The array is updated every second via the nearestObjects command. There will be no more than 7 elements in the array (unless something goes wrong heh).

What I would like to know is: what would be the easiest way to remove the corpses from this array?

My first guess would be something along the lines of:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_x = nearestObjects [player, "SoldierWB", 55];

{if (!(alive _array select _x)) then { ? ? ? };} forEach _array;

I am not sure what would go in the "? ? ?" spot. I guess I could simply dereference the element, ie:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_array select (_x - 1) = _array select _x;

But, then I would need to include logic to detect if it is the first element or not, ie:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

if (_x == 0) then {_array = _array - _array;};

Now, this isn't all that complicated, but I don't know if this would be too demanding to be run every second.

I guess I should just try it out, but I was just interested to know if anyone else has any experience with this kind of thing?

EDIT: Heh. Emoticons.

Share this post


Link to post
Share on other sites

If it's a specific element I usualy go with this method:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_Array=[1,2,[A,B,C],3,4];

_Array Set [2,"DELETE"];

_Array=_Array-["DELETE"];

For removing your corpses you could do something like this:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">for "_i" from 0 to ((Count _Array)-1) do

      {

      If !(Alive (_Array Select _i)) Then

             {

             _Array Set [_i,ObjNull];

             };

      };

_Array=_Array-[ObjNull];

Share this post


Link to post
Share on other sites
If it's a specific element I usualy go with this method:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_Array=[1,2,[A,B,C],3,4];

_Array Set [2,"DELETE"];

_Array=_Array-["DELETE"];

For removing your corpses you could do something like this:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">for "_i" from 0 to ((Count _Array)-1) do

{

If !(Alive (_Array Select _i)) Then

{

_Array Set [_i,ObjNull];

};

};

_Array=_Array-[ObjNull];

Ah, perfect.

Much better than the way I was trying (the compiler doesn't like it when you have something like: _array select 0 = _array select 1; ).

Going to give it a try, thanks!

EDIT: Looks like it worked, thanks again!

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  

×