Jump to content
Raider_18

Small problem with 'AI follow you everywhere loop'. Any clever thoughts?

Recommended Posts

Curious to what you guys think. The following loop works 99% as intended, but for one small issue. Some context, I have three AI "VIP escorts" with a hold action, you can use it to command them to stay or follow. While {TRUE} I run the following loop to get the AI to follow the player on foot or if in a vehicle. But when the AI enters a vehicle, it continually disembarks and get's back in over and over. I think it might be the orderGetIn command, but I didnt read anywhere on the wiki that its supposed to make units dismount if given the command again. I also tested it with another AI that was just given the orderGetIn to a player vehicle over and over and he doesnt exit the vehicle once loaded.

 

Function

Spoiler

Params [["_unit", objNull], ["_caller", player]];
_unit enableAI "PATH";
_unit setUnitPos "UP"; // make them stand or they will crawl all the time
_unit reveal player; // if you get far away he will know where you are
_unit addEventHandler ["GetInMan", {(_this select 0) enableSimulationGlobal false;}];

// Individual loops, single variable would make <ALL> units stay/follow when ordering <ANY> unit stay/follow
while {CB_VAR_FOLLOW1} do 
{	

	scopeName "follow"; 
  	_unit doMove position player; 
	_currentVehicle = vehicle player;

	if !(isNull objectParent player) then 
		{
			_currentVehicle = vehicle player;
			_unit assignAsCargo _currentVehicle;
			[_unit] orderGetIn true;
			[_unit] allowGetIn true;
		};

	if (isNull objectParent player) then 
		{	
			_unit enableSimulationGlobal true;
			_currentVehicle = vehicle player;
			UnassignVehicle _currentVehicle;
			[_unit] orderGetIn false;
			[_unit] allowGetIn false;
			_unit moveOut _currentVehicle; // just an extra to force exit
				
		};

	if (!alive CB_MAN_IDAP1) then {breakOut "follow";};	
	sleep 3;  

};

 

As you may notice, I have attached a GetInMan event handler to disable his simulation, and then re-enable once the player exits. It's very close, but he freezes just as he exits the vehicle and hangs out in mid-air until player exits vehicle

 

I have also tried with no luck: 

*Adding sleep commands of various lengths in between the IF statements, to try and let the simulation disable before he exits

 

*Cutting out the second IF statement, <If player exit vehicle>, part of the loop entirely, just to see if it still happens. It Does

 

*Using (player in vehicle) instead of ObjectParent, nope

https://community.bistudio.com/wiki/objectParent

 

*Removing and adding different GetIN/GetOUT commands and actions, only orderGetIn works as intended: 

https://community.bistudio.com/wiki/Category:Command_Group:_Unit_Control

 

*Waypoints, but the AI is without a group and I have x3 total guys that need to be escorted and they are ungrouped, and seperated on the map for scenario: https://community.bistudio.com/wiki/Category:Command_Group:_Waypoints

 

*JoinSilent, but commanding AI is not the intention as the player already has enough to worry about with 5 friendly AI in the team and other scripts/events. The simple hold action Stay/Follow commands attached to the 'escorts' is the preferred method

 

Any ideas guys? Thanks

 

  • Like 1

Share this post


Link to post
Share on other sites

UPDATE:

Ok so weird but, this is only happening when the player is the Driver of any vehicle, as cargo or any other seat the escort gets in any available seat and stay's put. Until the player exits. Very strange

Share this post


Link to post
Share on other sites

SOLVED - Just had to put the doMove command safely in the <player on foot> IF statment. I also added a moveInCargo command. Althought it's not ultra realistic it is somewhat necessary as the AI will sometimes wander or do weird things while trying to keep up. I will post all code below for anyone who wants to tweak it or improve it. 

 

My naming style

TAG_type_name

Variable is VAR / Person or unit is MAN / location is LOC etc.

 

Example: 

CB_VAR_BATTLE = variable battle

CB_MAN_IDAP1 = one of the IDAP escorts

 

Init.sqf

Spoiler

// Globals
missionNameSpace setVariable ["CB_VAR_FOLLOW1", FALSE];
missionNameSpace setVariable ["CB_VAR_FOLLOW2", FALSE];

/* hold actions for VIP's
[target, title, idleIcon, progressIcon, conditionShow, conditionProgress, codeStart, codeProgress, codeCompleted, codeInterrupted, arguments, duration, priority, removeCompleted, showUnconscious, showWindow]
*/

[CB_MAN_IDAP1, "Follow", 
"\a3\missions_f_oldman\data\img\holdactions\holdAction_follow_start_ca.paa", 
"\a3\missions_f_oldman\data\img\holdactions\holdAction_follow_start_ca.paa", 
"(missionNameSpace getvariable ""CB_VAR_FOLLOW1"") == FALSE && player distance _target < 4 && alive CB_MAN_IDAP1", "true", {}, {}, { 
    missionNameSpace setVariable ["CB_VAR_FOLLOW1", TRUE];
	[CB_MAN_IDAP1, player] spawn CB_fnc_actionFollow; 
}, {}, [], 0.5, 0, false, false] call BIS_fnc_holdActionAdd;

[CB_MAN_IDAP1, "Hold", 
"\a3\missions_f_oldman\data\img\holdactions\holdAction_follow_stop_ca.paa", 
"\a3\missions_f_oldman\data\img\holdactions\holdAction_follow_stop_ca.paa", 
"(missionNameSpace getvariable ""CB_VAR_FOLLOW1"") == TRUE && player distance _target < 4 && alive CB_MAN_IDAP1", "true", {}, {}, { 
    missionNameSpace setVariable ["CB_VAR_FOLLOW1", FALSE];
	[CB_MAN_IDAP1, player] spawn CB_fnc_actionStay;
}, {}, [], 0.5, 0, false, false] call BIS_fnc_holdActionAdd;

[CB_MAN_IDAP2, "Follow", 
"\a3\missions_f_oldman\data\img\holdactions\holdAction_follow_start_ca.paa", 
"\a3\missions_f_oldman\data\img\holdactions\holdAction_follow_start_ca.paa", 
"(missionNameSpace getvariable ""CB_VAR_FOLLOW2"") == FALSE && player distance _target < 4 && alive CB_MAN_IDAP2", "true", {}, {}, { 
    missionNameSpace setVariable ["CB_VAR_FOLLOW2", TRUE];
	[CB_MAN_IDAP2, player] spawn CB_fnc_actionFollow;
}, {}, [], 0.5, 0, false, false] call BIS_fnc_holdActionAdd;

[CB_MAN_IDAP2, "Hold", 
"\a3\missions_f_oldman\data\img\holdactions\holdAction_follow_stop_ca.paa", 
"\a3\missions_f_oldman\data\img\holdactions\holdAction_follow_stop_ca.paa", 
"(missionNameSpace getvariable ""CB_VAR_FOLLOW2"") == TRUE && player distance _target < 4 && alive CB_MAN_IDAP2", "true", {}, {}, { 
    missionNameSpace setVariable ["CB_VAR_FOLLOW2", FALSE];
	[CB_MAN_IDAP2, player] spawn CB_fnc_actionStay;
}, {}, [], 0.5, 0, false, false] call BIS_fnc_holdActionAdd;

 

 

My function CB_fnc_actionFollow called by hold action

Spoiler

/*
	Name: Action Follow
	
	Author: Raider
	
	Description: Handles when ordering VIP to follow you
*/

Params [["_unit", objNull], ["_caller", player]];
_unit enableAI "PATH";
_unit setUnitPos "UP"; // make them stand or they will crawl all the time
_unit reveal player; // if you get far away he will know where you are

// Individual loops, single variable would make <ALL> units stay/follow when ordering <ANY> unit stay/follow
while {CB_VAR_FOLLOW1} do 
{	

	scopeName "follow"; 
	_unit lookAt player;
	_unit setUnitPos "UP";
	_currentVehicle = vehicle player;

	if !(isNull objectParent player) then 
		{
			_currentVehicle = vehicle player;
			_unit assignAsCargo _currentVehicle;
			[_unit] orderGetIn true;
			[_unit] allowGetIn true;
			_unit MoveInCargo _currentVehicle;
		};

	if (isNull objectParent player) then 
		{	
			_unit doMove position player;
			_currentVehicle = vehicle player;
			UnassignVehicle _currentVehicle;
			[_unit] orderGetIn false;
			[_unit] allowGetIn false;
			_unit moveOut _currentVehicle; // just an extra to force exit
				
		};

	if (!alive CB_MAN_IDAP1) then {breakOut "follow";};	
	sleep 3;  

};

while {CB_VAR_FOLLOW2} do 
{	

	scopeName "follow"; 
	_unit lookAt player;
	_unit setUnitPos "UP";
	_currentVehicle = vehicle player;

	if !(isNull objectParent player) then 
		{
			_currentVehicle = vehicle player;
			_unit assignAsCargo _currentVehicle;
			[_unit] orderGetIn true;
			[_unit] allowGetIn true;
			_unit MoveInCargo _currentVehicle;
		};

	if (isNull objectParent player) then 
		{	
			_unit doMove position player;
			_currentVehicle = vehicle player;
			UnassignVehicle _currentVehicle;
			[_unit] orderGetIn false;
			[_unit] allowGetIn false;
			_unit moveOut _currentVehicle; // just an extra to force exit
				
		};

	if (!alive CB_MAN_IDAP2) then {breakOut "follow";};	
	sleep 3;  

};

 

 

My function CB_fnc_actionStay called by hold action

Spoiler

/*
	Name: Action Stay
	
	Author: Raider
	
	Description: Handles when ordering hostage to stay put
    You could add panic actions and what not
*/	

Params [["_unit",objNull], ["_caller", player]];
_unit disableAI "PATH";
_unit setUnitPos "MIDDLE";

 

 

Useage

Place 2 men in the editor and name them CB_MAN_IDAP1 & CB_MAN_IDAP2. Essentially, the hold actions dont go away but instead use the CB_VAR_FOLLOW variable to switch between which follow/stay command is visable. When ordering to follow it spawns the loop in CB_fnc_actionFollow for each individual escort. In there, you can see that if you are on foot it is simply telling them to move to your position, if in a vehicle, it uses a MoveInCargo to actually force them into the vehicle. It is important that the doMove is safely in the IF statement or they will continually disembark trying to get to your exact position.

 

Some design notes

*If there is no space in the vehicle the escorts will just stand still. You may then have to exit the vehicle and order them to stay and follow again. Ensure the scenario gives the player access to vehicles that can accomodate all escorts and potential friendly AI squad.

*When you order them to stay put, they will, regardless of combat or threats. So don't expect them to take cover if you leave them in the open. 

*I also thought about setting the doMove position a random number of meters away from the player, as they will quite literally stand right next to you. But this worked less than ideal when trying to get them to follow you in a building

 

Hope this helps someone in the future

✌️

 

  • Like 1
  • 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

×