Jump to content
Robustcolor

Spawn ai - check limit.

Recommended Posts

Hi, I'm trying to spawn units like in the script below but i would like to use a Unitlimit and either use pushback or + to add - remove numbers from array. How should i do this? I want the While {true}do to halt when limit is reach and continue when below limit.

private _markers =  ["Z1","Z2","Z3","Z4","Z5"];

_Unitarray = [];

_Unitlimit = 12;

_allPlayers = (allPlayers - entities "HeadlessClient_F") select {alive _x};

_deviation = 180;
_distance = 400;
_markerdist = 500;

while {true} do {

_distanceMarkers = [];
{
private _marker = _x;
_distanceMarkers append (_allPlayers apply {[_x distance getmarkerpos _marker, _marker, _x]})
} forEach _markers;

_distanceMarkers sort true;

_marker = (_distanceMarkers select 0) select 1;

_markerPosition = (_distanceMarkers select 0) select 0;

if (_markerPosition < _markerdist) then {

if (count _Unitarray < _Unitlimit) then {

private _allPlayers = allPlayers - entities "HeadlessClient_F";_allPlayers = _allPlayers select {_x distance getmarkerpos _marker < _distance};

private _player = selectRandom _allPlayers

private _pos = _player getpos [_distance,(getmarkerpos _marker getDir _player) + _deviation - (random (_deviation *2))];

_grp01 = createGroup [east, true];
_unit = _grp01 createUnit [selectRandom ["uns_men_VC_mainforce_AS1","uns_men_VC_mainforce_HMG","uns_men_NVA_daccong_HMG","uns_men_NVA_daccong_AS6"], _pos, [], _distance, "FORM"];


				sleep 2;
			};
		}
		else {sleep 5;};
};

I was thinking of adding _Unitarray pushBack [_unit]; command but it gives error type group, expected number,array,string not a number.

And how would i remove when each Ai is either killed or removed so the loop can continue when it's < _Unitlimit;

Share this post


Link to post
Share on other sites
4 hours ago, Robustcolor said:

I was thinking of adding _Unitarray pushBack [_unit]; command but it gives error type group, expected number,array,string not a number.

no that command doesn't give that error. you have an error elsewhere then.

 

I  don't really understand your question.
you want to wait for units to die before continuing?
at the start of the loop just do

_Unitarray = _unitArray select {alive _x};

 

to filter out dead.

and then

_unitArray pushBack _unit;

after you've spawned a unit.

Share this post


Link to post
Share on other sites
49 minutes ago, Dedmen said:

I  don't really understand your question.
you want to wait for units to die before continuing?
at the start of the loop just do

_Unitarray = _unitArray select {alive _x};

Yes, i want to start spawning ai's when a player is near one of the markers and stop spawning them in when limit is reached. But as you kill ai's i want to keep the loop checking and then spawn in new ones till limit reached again, so on.

 

I'll try your way of select alive.

Share this post


Link to post
Share on other sites

There is something wrong with using _Unitarray + _Unitlimit.

 

It works fine with spawning and looping everything, but the script don't care about the _Unitlimit for some reason.

 

If i put the command _Unitarray = _Unitarray + [_Unit]; in first function it seems to fill the array, but my second function does not remove unit from array when killed, making the script stop spawning units because it thinks the limit  is reached.

 

If i put both _Unitarray = _Unitarray + [_Unit]; and Unitarray = _Unitarray - [_Unit]; in second function it will keep continue spawning ai's and never halt if limit reached.


What is wrong here?

private _markers =  ["1","2","3","4","5"];

_Unitarray = [];

_Unitlimit = 5;
_respawntime = 300;
_sideMB = createCenter East;
_groupMB = createGroup East;

_allPlayers = (allPlayers - entities "HeadlessClient_F") select {alive _x};

_deviation = 180;
_distance = 150;
_markerdist = 500;

while {true} do {

_distanceMarkers = [];
{
private _marker = _x;
_distanceMarkers append (_allPlayers apply {[_x distance getmarkerpos _marker, _marker, _x]})
} forEach _markers;

_distanceMarkers sort true;

_marker = (_distanceMarkers select 0) select 1;

_markerPosition = (_distanceMarkers select 0) select 0;

if ((count _Unitarray <= _Unitlimit) && (_markerPosition <= _markerdist)) then {

private _allPlayers = allPlayers - entities "HeadlessClient_F";_allPlayers = _allPlayers select {_x distance getmarkerpos _marker < _distance};

private _player = selectRandom _allPlayers;

private _pos = _player getpos [_distance,(getmarkerpos _marker getDir _player) + _deviation - (random (_deviation *2))];

_Unit = _groupMB createUnit [selectRandom ["C_man_polo_1_F_euro","C_man_polo_2_F_asia","C_Farmer_01_enoch_F","C_Man_1_enoch_F","C_Man_2_enoch_F","C_Man_3_enoch_F","C_Man_4_enoch_F","C_Man_5_enoch_F","C_Man_6_enoch_F"], _pos, [], 0, "NONE"];

[_Unit,_respawntime,_sideMB,_groupMB,_Unitarray] spawn Robust_fnc_Contact;

sleep 2;                                                                                                                                               };
sleep 5;
"Looping again" remoteExec ["hint"];
};

"Exited first loop" remoteExec ["hint"]; <-- just to check if it ever exits the loop for some reason.

Function that i call

params ["_Unit","_respawntime","_sideMB","_groupMB",","_Unitarray"];

_Unitarray = _Unitarray + [_Unit];

while {alive _Unit} do {

	_units = _Unit nearEntities ["Man", 500];
	_target = [];
	
	{
		if (group _x != _groupMB && side _x != _SideMB) then
		{
			_target = _target +[_x];
		};
		sleep 0.01;
	} foreach _units;

	_target = player;

waitUntil {sleep 0.5;simulationenabled leader _groupMB};

_Unit doMove getposATL _target;

if (_target distance position _Unit > 500) then
{
deletevehicle _Unit;
_Unitarray = _Unitarray - [_Unit];
};
sleep 0.5;
};

"Exited second loop" remoteExec ["hint"];
sleep _respawntime;
_Unitarray = _Unitarray - [_Unit];
deletevehicle _Unit;

Also, how can i make this work in a dedicated enviroment, fetching players.

	_units = _Unit nearEntities ["Man", 500];
	_target = [];
	
	{
		if (group _x != _groupMB && side _x != _SideMB) then
		{
			_target = _target +[_x];
		};
		sleep 0.01;
	} foreach _units;

	_target = player;

 

Share this post


Link to post
Share on other sites

If the units get deleted, they will be objNull in the list. Maybe just filter using select {!isNull _x}

 

15 hours ago, Robustcolor said:

deletevehicle _Unit; _Unitarray = _Unitarray - [_Unit];

 

you are deleting the unit, turning it into objNull, and then removing that from the array. Maybe swap around, or just filter out isNull.

 

 

15 hours ago, Robustcolor said:

_Unitarray = _Unitarray + [_Unit];

please just use pushBack

 

15 hours ago, Robustcolor said:

_marker = (_distanceMarkers select 0) select 1; _markerPosition = (_distanceMarkers select 0) select 0;

use params

 

15 hours ago, Robustcolor said:

if ((count _Unitarray <= _Unitlimit)

you never ever add any units to _Unitarray, its always empty.

I'd recommend you add logging to your scripts using diag_log, had you logged the count of _unitarray you would've noticed that.

 

15 hours ago, Robustcolor said:

params ["_Unit","_respawntime","_sideMB","_groupMB",","_Unitarray"];

_Unitarray = _Unitarray + [_Unit];

This doesn't add a unit to _unitarray, it creates a new _unitarray with the unit inside it, it doesn't modify the old one.

As I already told you, use pushBack, why are you asking for advice and then ignoring what I'm telling you? Its a bit annoying. Especially when you ignoring what I'm telling you, causes a problem that you then ask about, even though it could've already been solved.

Share this post


Link to post
Share on other sites

@Dedmen

Using _Unitarray pushback _Unit; works, thanks.

 

But what does not work is removing units from the array with _Unitarray = _Unitarray - [_Unit]; So the loop stops because the limit is reached.

 

I did try this

_Unitarray = _Unitarray - [_Unit];
deletevehicle _Unit;

Not working.

 

2 hours ago, Dedmen said:
17 hours ago, Robustcolor said:

_marker = (_distanceMarkers select 0) select 1; _markerPosition = (_distanceMarkers select 0) select 0;

use params 

You gave me this code a while back, How would i use params here?

Share this post


Link to post
Share on other sites
17 hours ago, Robustcolor said:

_Unitarray = _Unitarray - [_Unit];

oops didn't notice that was in the same file. It causes the same problem as the addition, you are creating a new array here, not modifying the original

use

_unitArray deleteAt (_unitArray find _unit)
there.

 

5 minutes ago, Robustcolor said:

How would i use params here?

(_distanceMarkers select 0) params ["_markerPosition", "_marker"];

Share this post


Link to post
Share on other sites
8 minutes ago, Dedmen said:

oops didn't notice that was in the same file. It causes the same problem as the addition, you are creating a new array here, not modifying the original

use

_unitArray deleteAt (_unitArray find _unit)
there.

 

(_distanceMarkers select 0) params ["_markerPosition", "_marker"];

Thanks @Dedmen 🙂👍

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

×