Jump to content
He'sCalledTheStig

Init code/script for AI to hunt the nearest player

Recommended Posts

So I found this line somewhere that goes in the init box of the AI vehicle, or the group leader. I want to change it to target the nearest player/playable character instead of "the player".

 

_null = this spawn {
Hunt_players_fnc = {
    _player = "";
    {
        if (isPlayer _x AND alive _x) then {_player = _x};
    } foreach (playableUnits + switchAbleUnits);
    _wp = _this addWaypoint [getPos _player, (50 + (floor(random 70)))];
    _wp setWaypointStatements ["true", "_null = (group this) spawn Hunt_players_fnc;"];
    _wp setWaypointType "SAD";
    _wp setWaypointCombatMode "RED";
    _wp setWaypointSpeed "FULL";
};

_null = (group _this) spawn Hunt_players_fnc;
};

 

 

 

Keep in mind I'm kind of rarted when it comes to this sort of thing. Thanks.

Share this post


Link to post
Share on other sites
12 hours ago, He'sCalledTheStig said:

I want to change it to target the nearest player/playable character instead of "the player".

 

Try this:

Spoiler

if (isServer) then {
	_null = this spawn {
		Hunt_players_fnc = {
			_player = objNull;
			_distance = 100000;
			{
				if (_x In allPlayers && alive _x && _x distance (Leader _this) < _distance) then {
					_distance = _x distance (Leader _this); 
					_player = _x;
				};
			} foreach (playableUnits + switchAbleUnits);

			if !(isNull _player) then {
				_wp = _this addWaypoint [getPos _player, (50 + (floor(random 70)))];
				_wp setWaypointStatements ["true","_null = (group this) spawn Hunt_players_fnc;"];
				_wp setWaypointType "SAD";
				_wp setWaypointCombatMode "RED";
				_wp setWaypointSpeed "FULL";
			};
		};

		_null = (group _this) spawn Hunt_players_fnc;
	}; 
};

 

 

Share this post


Link to post
Share on other sites
On 5/15/2020 at 2:31 AM, opusfmspol said:

 

Try this:

  Hide contents


if (isServer) then {
	_null = this spawn {
		Hunt_players_fnc = {
			_player = objNull;
			_distance = 100000;
			{
				if (_x In allPlayers && alive _x && _x distance (Leader _this) < _distance) then {
					_distance = _x distance (Leader _this); 
					_player = _x;
				};
			} foreach (playableUnits + switchAbleUnits);

			if !(isNull _player) then {
				_wp = _this addWaypoint [getPos _player, (50 + (floor(random 70)))];
				_wp setWaypointStatements ["true","_null = (group this) spawn Hunt_players_fnc;"];
				_wp setWaypointType "SAD";
				_wp setWaypointCombatMode "RED";
				_wp setWaypointSpeed "FULL";
			};
		};

		_null = (group _this) spawn Hunt_players_fnc;
	}; 
};

 

 

Works fine in singleplayer, but doesn't function on dedicated server.

Share this post


Link to post
Share on other sites

See if this works for dedicated:

if (isServer) then {
	_null = this spawn {
		Hunt_players_fnc = {
			_player = objNull;
			_players = +(allPlayers - (entities "HeadlessClient_F"));
			_distance = 100000;
			{
				if (alive _x && _x distance (Leader _this) < _distance) then {
					_distance = _x distance (Leader _this); 
					_player = _x;
				};
			} foreach _players;

			if !(isNull _player) then {
				_wp = (Group _this) addWaypoint [getPos _player, (50 + (floor(random 70)))];
				_wp setWaypointStatements ["true","_null = this spawn Hunt_players_fnc;"];
				_wp setWaypointType "SAD";
				_wp setWaypointCombatMode "RED";
				_wp setWaypointSpeed "FULL";
			};
		};

		_null = _this spawn Hunt_players_fnc;
	}; 
};

 

  • Thanks 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

×