Jump to content
Sign in to follow this  
Koni

Delete certain number of units from a group

Recommended Posts

I have a group with a AI civilian unit as group leader that keeps getting units joining it, well "chickens" actually, and when the group gets to a certain number of units in the group I need to delete a set amount of the chickens from the group.

So say the group totals 40 units, how do I delete say 25 of them from the group ?

Thanks

Share this post


Link to post
Share on other sites

Untested but here is the concept. Use count units myGroup (use your group name) to check the number of units in the group. How is up to you: trigger, waitUntil, etc. When the count is > 39 then run a for-from-to-Loop (0-24) and inside the brackets use delete vehicle to delete a unit using select _i. It will repeat 25 times.

Example code:

Trigger Condition:

((count units myGroup) > 39)

Trigger On Act:

nul = [] execVm "deleteHens.sqf";

deleteHens.sqf:

// a loop repeating 25 times
for "_i" from 0 to 24 do
{
   deleteVehicle (units myGroup select _i);
};

Edited by Loyalguard
Added example

Share this post


Link to post
Share on other sites

That looks very good Loyalguard, thanks, though not tested it yet, but if deleting 0 to 24, would that not delete the civilian group leader, and leave a chicken in command.

If that is the case would delete 1 to 25 just delete the chickens and leave the civilian group leader present ?

Share this post


Link to post
Share on other sites

Maybe try this:

// a loop repeating 25 times
for "_i" from 0 to 24 do
{
   if (_i > 0)  then {
   deleteVehicle (units myGroup select _i);
};
}; 

If the civilian group leader is the first unit of the group, he should be "select 0" i think. So just check if _i > 0 may help.

Share this post


Link to post
Share on other sites
Maybe try this:

// a loop repeating 25 times
for "_i" from 0 to 24 do
{
   if (_i > 0)  then {
   deleteVehicle (units myGroup select _i);
};
}; 

If the civilian group leader is the first unit of the group, he should be "select 0" i think. So just check if _i > 0 may help.

The script works in doing what it should do but it's throwing up a error zero divisor message ?

Share this post


Link to post
Share on other sites

Yes, that's the first thing I changed.

Share this post


Link to post
Share on other sites

Ok, Are you making sure you have at least 26 units in (leader + 25 hens) before trying to run the loop? You will get zero divisor errors if you try to access an element of an array that does not exist. So, if your group is too small when you run the code then you will get the error after it deletes the last unit in the group and as the loop continues until it reaches the full count.

If your group is big enough then we need to dig deeper.

Share this post


Link to post
Share on other sites

yeah I thought that may be an issue so I waited till I had a group of over 40, still the same.

Share this post


Link to post
Share on other sites

I think you need to delete the first unit in the array and keep deleting it until the group reaches the size you require.

for "_i" from 0 to 24 do  { deleteVehicle (units this select 1);  };   

If you wanted to leave a group of four it would be

for "_i" from 0 to 24 do  { deleteVehicle (units this select 4);  };   

Share this post


Link to post
Share on other sites

1 to 22 seems to be the magic number where it stops erroring, even though the group is larger than 30 units, kind of strange

My mistake... the first lot of hens the player steals are named, thus they don't seem to get counted in the script by the prefix of _i, so technically the script is trying to access an element of an array that dosen't exist :eek: well that's all I can put it down to anyway, as now it's working propperly making sure there is more than 25 hens added to the group that are not named.

Edited by Koni

Share this post


Link to post
Share on other sites

Hmm, perhaps the index of array elements is updating faster than the loop runs and that cause the error. Let's try this. We will capture all of the current units of myGroup in a seperate array and then delete from the seperate array, not the units myGroup array so we know the index should stay the same throughout.

// Capture the current units in the group.
_currentUnits = units myGroup;

// Delete units 1-25 from the array _currentUnits (and by default from myGroup).
for "_i" from 1 to 25 do
{
   deleteVehicle ( _currentUnits select _i);
};  

If not that, try F2K's solution.

Share this post


Link to post
Share on other sites

Sorry Loyalguard for my mistake and thanks for your persistant patience :)

Updated see my post above :)

Edited by Koni

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  

×