Jump to content

Recommended Posts

Hello,
I do not know how to solve this problem with array.

 

Init.sqf

POLE_UNIT = [];


CreateUnit.sqf:

_i = 0;
While { _i < 5 } Do {
_Unit = createVehicle ["B_Soldier_F", position player, [], 0, "NONE"];
ARRAY_UNIT pushBack _Unit;
};

DeleteUnit.sqf:

{
waitUntil { !(Alive _x) };
ARRAY_UNIT = ARRAY_UNIT - [_x];
} forEach ARRAY_UNIT;

I need if one the _Unit dies in the ARRAY_UNIT, so is this specific element is removed from array.

This is my Delete script is not working. I tried yet:

ARRAY_UNIT = (count ARRAY_UNIT) -1; 
ARRAY_UNIT deleteAt 1;
etc..

But it will delete all items from the field or nothing will happen.

Thank for you reply. :eh:

 

 

 

Share this post


Link to post
Share on other sites

Of course the init should be:

ARRAY_UNIT = [];

I can not edit my original post.

  • Like 1

Share this post


Link to post
Share on other sites

So when you use:

ARRAY_UNIT = (count ARRAY_UNIT) -1;

You're declaring ARRAY_UNIT as a number, which overwrites the array. 

 

You'd be better off using a new variable for the count:

ARRAY_UNIT_COUNT = (count ARRAY_UNIT) -1;

Share this post


Link to post
Share on other sites

Now I'm very new to this so this might not be the case but wouldn't your "Deleteunit.sqf" wait until the first unit in "ARRAY_UNIT" dies, then delete it from the array and then proceed to the next unit in "ARRAY_UNIT"? So I don't think i'll check all the units in the array. If this is the case it might be better using the "Killed" event handler (link).

 

so you'd get something like

{
	_x addEventHandler ["Killed", {ARRAY_UNIT deleteAt (ARRAY_UNIT find (_this select 0)}];
} forEach ARRAY_UNIT;

(Not 100% sure, haven't done much with event handlers at all)

Again I'm very new so It'd be good for someone to confirm this.

Share this post


Link to post
Share on other sites
5 hours ago, dyrmoon said:

 

DeleteUnit.sqf:


{
waitUntil { !(Alive _x) };
ARRAY_UNIT = ARRAY_UNIT - [_x];
} forEach ARRAY_UNIT;

 

This is locking the script. Its checking the first element and waiting until its dead, not looping through all the elements.

 

//Put whatever you want to stop the loop in here, eg while atleast one unit is alive
while { ({alive _x} count ARRAY_UNIT > 0) } do {

  {
		
      if (!alive _x) then {ARRAY_UNIT = ARRAY_UNIT - [_x]};

  } foreach ARRAY_UNIT;

 sleep 1;

};

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

×