Jump to content
Sign in to follow this  
wiggum2

Delete spawned group if distance to player is bigger then x

Recommended Posts

I use this script to spawn units and it works.

_pos = getpos a1;
_units = ["NS_zombie1", "NS_zombie2"];
_spos = [_pos,random 360,[10,20]] call SHK_pos;

_grp1 = [_spos, Resistance, _units, [], [], []] call BIS_fnc_spawnGroup;

Now i want to delete the spawned units if the distance between the player and the group (or units) is bigger then 100m.

An tried some things but could not get it to work...

I thought about something like:

While alive _grp1 do {

If player distance _grp1 > 100 then {

delete units _grp1;

};

};

I know this is totally messed up scripting but you should get a idea what i mean... ;)

Can someone help ?

Share this post


Link to post
Share on other sites

I'm 100% sure there's been a thread about this very thing recently, but as search is broken I can't find it right now, but it's there... somewhere.

Share this post


Link to post
Share on other sites

while ({alive _x} count units _grp1 != 0) do {
if ((getPos player) distance ([color="Red"]getPos leader group _grp1[/color]) > 100) then {
{deleteVehicle _x} forEach units _grp1;
deleteGroup _grp1;
};
};

Give that a whirl. If it doesn't work I'd bet its at the Red part, in which try removing the word 'group', or just follow what the error says. Hell, even try without leader. Just trial and error.

Share this post


Link to post
Share on other sites

Or

While {{alive _x} count units _grp1 > 0} do {
{
	_zomb = _x;
	if (alive _zomb) then {

		  if ( (_zomb distance player) > 100) then {	

                               deletevehicle _x;			
		};
	};

} forEach units _grp1;

  deletegroup _Grp1;

  Sleep 1;

};

Share this post


Link to post
Share on other sites

@ kylania

I know what thread you are talking about, but i think this is a different problem.

Sorry guys, it still does not work, i start to believ that there is a problem with _grp1...

Edited by Wiggum

Share this post


Link to post
Share on other sites

Was trying with foreach and gave up... was taking too much time to figure out!

This works though....

_grp1 = _this select 0;

_run = true;

while {_run} do {
for "_i" from 0 to ((count units _grp1)-1) do {
	_zomb = units _grp1 select _i;
	if ((_zomb distance player) > 100) then {
		_run = false;
	};
               sleep 0.01;
};
sleep 1;
};

{deletevehicle _x} forEach units _grp1;
deletegroup _grp1;

Edited by twirly
Added a sleep!

Share this post


Link to post
Share on other sites

another possible way:

while {(count (units _grp1)) != 0} do {
{
	_zombie = _x;
	if (({(_zombie distance _x) > 100 AND isPlayer _x} count [b]playableUnits[/b]) == 0) then {
		deleteVehicle _zombie;
	};
} foreach (units _grp1);
sleep 1;
};
deletegroup _grp1;

checking distance to allplayers in MP, or switch out playableUnits with switchableUnits for SP.

if Zombie is more than 100 away from all players, its deleted.

edit, and yeah delete the group when empty for cleanlyness like twirly did.

Share this post


Link to post
Share on other sites

Sorry guys i just cant get it work, can you please add your code to my spawn script correctly ?

I think thats what i mess up all the time...

_pos = getpos a1;
_units = ["NS_zombie1", "NS_zombie2"];
_spos = [_pos,random 360,[10,20]] call SHK_pos;

_grp1 = [_spos, Resistance, _units, [], [], []] call BIS_fnc_spawnGroup;

Share this post


Link to post
Share on other sites
_pos = getpos a1;
_units = ["NS_zombie1", "NS_zombie2"];
_spos = [_pos,random 360,[10,20]] call SHK_pos;

_grp1 = [_spos, Resistance, _units, [], [], []] call BIS_fnc_spawnGroup;

while {(count (units _grp1)) != 0} do {
{
	_zombie = _x;
	if (({(_zombie distance _x) > 100 AND isPlayer _x} count playableUnits) == 0) then {
		deleteVehicle _zombie;
	};
} foreach (units _grp1);
sleep 1;
};
deletegroup _grp1;

Share this post


Link to post
Share on other sites

Sorry, for me it still does not work although i use exactly your script...:(

Share this post


Link to post
Share on other sites

I did a little demo here....working fine.

You need to use createCenter or have a resistance guy already existing on the map.

ZombieHQ = createcenter Resistance;

or

place a resistance guy anywhere with "deletevehicle this;" in his init.

In the demo walk away from the guys that are created right next to you.... they will disappear.

Share this post


Link to post
Share on other sites

Your demo mission works fine, now i have discovered the issue !

The "NS_zombies" (units from Sumraks Mod) have a "join grpNull" in their init.

This means that as soon as they are created through the script they join grpNull and because of that the rest of the script (the delete group part) does not work with them... :(

Is there a way to get them back into a group in my script after spawning again so the group can be deleted later ?

_pos = getpos a1;

_units = ["NS_zombie1", "NS_zombie2"];

//_spos = [_pos,random 360,[10,20]] call SHK_pos;
_spos = [(_pos select 0) + random 10,(_pos select 1) + random 10,0];

_grp1 = [_spos, Resistance, _units,[],[],[]] call BIS_fnc_spawnGroup;

_run = true;

while {_run} do {

for "_i" from 0 to ((count units _grp1)-1) do {

	_zomb = units _grp1 select _i;

	if ((_zomb distance a1) > 30) then {

		_run = false;

	};

};

sleep 1;

};

{deletevehicle _x} forEach units _grp1;
deletegroup _grp1;

Edited by Wiggum

Share this post


Link to post
Share on other sites

use createUnit array instead of spawnGroup, like this and rejoin them to the _grp1 once not in that group:

_pos = getpos a1;
_spos = [(_pos select 0) + random 10,(_pos select 1) + random 10,0];

_grp1 = createGroup resistance;
{
_unit = _grp1 createUnit [_x, _spos, [], 0, "NONE"];
waitUntil {(group _unit) != _grp1};
[_unit] joinSilent _grp1;
} foreach ["NS_zombie1", "NS_zombie2"];

while {(count (units _grp1)) != 0} do {
{
	_zombie = _x;
	if (({(_zombie distance _x) > 100 AND isPlayer _x} count playableUnits) == 0) then {
		deleteVehicle _zombie;
	};
} foreach (units _grp1);
sleep 1;
};
deletegroup _grp1;

Share this post


Link to post
Share on other sites

Thanks Demonized, but with your script only one zombie spawns and the delete still does not works...argh...

Share this post


Link to post
Share on other sites

ah yes, once a group is empty it is auto deleted.

instead of adding them to a group add them to a array (list) instead.

_pos = getpos a1;
_spos = [(_pos select 0) + random 10,(_pos select 1) + random 10,0];

_list1 = [];
{
_grp1 createGroup resistance;
_unit = _grp1 createUnit [_x, _spos, [], 0, "NONE"];
_list1 = _list1 + [_unit];
} foreach ["NS_zombie1", "NS_zombie2"];

while {(count _list1) != 0} do {
{
	_zombie = _x;
	if (({(_zombie distance _x) > 100 AND isPlayer _x} count playableUnits) == 0) then {
		deleteVehicle _zombie;
		_list1 = _list1 - [_zombie];
	};
} foreach _list1;
sleep 1;
};

---------- Post added at 03:46 PM ---------- Previous post was at 03:40 PM ----------

you can also just spawn your zombies however you want, like spawngroup and then just run a clean script like this for deleteing all zombies more than 100 meter away from any player, remember to use switchableunits instead of playableunits if single player.

this will work as long as the mission is running and will delete any zombie no matter group if range is more than 100 to all players.

while {true} do {
{
	if (_x isKindOf "NS_zombie1" OR _x isKindOf "NS_zombie2") then {
		_zombie = _x;
		if (({(_zombie distance _x) > 100 AND isPlayer _x} count playableUnits) == 0) then {
			deleteVehicle _zombie;
			_list1 = _list1 - [_zombie];
		};
	};
} foreach allUnits;
sleep 1;
};

Share this post


Link to post
Share on other sites

I think we are close to the solution Demonized ! :)

But both scripts dont work.

For the first one i get a error (missing ; in line 6).

And for the second one i also get a error.

I myselfe cant see the problem, maybe someone else knows what to do...

Share this post


Link to post
Share on other sites

this works, tested with regular us troops, it seems my > should be < for unknown reasons..

there was a not deleted list line in previous post, wich probably caused an error.

sleep 5;
while {true} do {
{
	if (_x isKindOf "NS_zombie1" OR _x isKindOf "NS_zombie2") then {
		_zombie = _x;
		if (({(_zombie distance _x) < 100 AND isPlayer _x} count switchableUnits) == 0) then {
			deleteVehicle _zombie;
		};
	};
} foreach allUnits;
sleep 1;
};

Share this post


Link to post
Share on other sites

Yeah, it works, thanks ! :)

Edited by Wiggum

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  

×