Jump to content
snakeplissken

How to Join (lead) the AI group (script spawn) when you get close to them?

Recommended Posts

You need to call your function somewhere at start. Not only in the respawn EH.

fn_LetGoAction = {....};

call fn_letGoAction;

 

If you place some functions in a sub-folder, you need to declare the path. Usually, by description.ext (relative path is OK for mission).

Share this post


Link to post
Share on other sites

I'll abandon the sub folder for now.

 

Should I just use the init.sqf for calling all 4 at the beginning of the mission and not use the initplayerlocal.sqf

addMissionEventHandler

fn_LetGoAction

GOM_fnc_actionConditionRecruit

GOM_fnc_actionConditionLeave

Share this post


Link to post
Share on other sites

Without going the function route.

Here is a script we use on our server for the squad join and dismiss, but it has started throwing errors.

 

_allaiUnits is no declared

_x is not declared

_actioned is not declared

sa_fnc_getnearenties may be used before assigning a value

Spoiler

if(!isDedicated) then {

	[] spawn {

		while {true} do {
			_allAiUnits = [getPos player, 30, {alive _x}] call SA_fnc_getNearEntities;
			{
				if( side player == side _x && alive _x && group _x != group player && leader player == player && _x isKindOf "Man" && vehicle _x == _x && (leader _x != _x || count (units group _x) == 1) ) then {
					if(isNil {_x getVariable "sa_join_squad_action_id"}) then {
						_actionId = _x addAction ["Join My Squad", { 
							if(isNil {(_this select 0) getVariable "sa_original_group"}) then {
								(_this select 0) setVariable ["sa_original_group",group (_this select 0)];
							};
							[_this select 0] join player; 
							(_this select 0) removeAction (_this select 2);
							(_this select 0) setVariable ["sa_join_squad_action_id",nil];
						}, nil, 0, false];
						_x setVariable ["sa_join_squad_action_id",_actionId];
					};
				} else {
					if(!isNil {_x getVariable "sa_join_squad_action_id"}) then {
						_x removeAction (_x getVariable "sa_join_squad_action_id");
						_x setVariable ["sa_join_squad_action_id",nil];
					};
				};
				
				if( side player == side _x && alive _x && group _x == group player && leader player == player && _x isKindOf "Man" && vehicle _x == _x && _x != player ) then {
					if(isNil {_x getVariable "sa_leave_squad_action_id"}) then {
						_actionId = _x addAction ["Leave My Squad", { 
							if(!isNil {(_this select 0) getVariable "sa_original_group"}) then {
								[_this select 0] join ((_this select 0) getVariable "sa_original_group"); 
								(_this select 0) setVariable ["sa_original_group",nil];
							} else {
								[_this select 0] join grpNull; 
							};
							(_this select 0) removeAction (_this select 2);
							(_this select 0) setVariable ["sa_leave_squad_action_id",nil];
						}, nil, 0, false];
						_x setVariable ["sa_leave_squad_action_id",_actionId];
					};
				} else {
					if(!isNil {_x getVariable "sa_leave_squad_action_id"}) then {
						_x removeAction (_x getVariable "sa_leave_squad_action_id");
						_x setVariable ["sa_leave_squad_action_id",nil];
					};
				};
				
			} forEach _allAiUnits;
			sleep 10;
		};

	};

};
Spoiler


SA_fnc_getNearEntities = {
	private ["_position","_radius","_filteredEntities","_function","_allEntities","_allObjects"];
	_position = [_this,0] call BIS_fnc_param;
	_radius = [_this,1,50] call BIS_fnc_param;
	_function = [_this,2,{true}] call BIS_fnc_param;
	//_allEntities = _position nearEntities _radius;
	_allObjects = nearestObjects [_position, [], _radius];
	_filteredEntities = [];
	{
		if(alive _x) then {
			if([] call _function) then {
				_filteredEntities = _filteredEntities + [_x];
			};
		};
	}	
	forEach _allObjects;
	_filteredEntities;
};

 

 

 

Share this post


Link to post
Share on other sites

_allAiUnits = [getPos player, 30, {alive _x}] call SA_fnc_getNearEntities;

 

_allAIUnits = allUnits select {_x distanceSqr getpos player < 900};

if (count _allAiUnits > 0) then {... code...}

Share this post


Link to post
Share on other sites
Spoiler

if(!isDedicated) then {

	[] spawn {

		while {true} do {
			{
			_allAIUnits = allUnits select {_x distanceSqr getpos player < 900}}; 
				if (count _allAiUnits > 0) then {{
				if( side player == side _x && alive _x && group _x != group player && leader player == player && _x isKindOf "Man" && vehicle _x == _x && (leader _x != _x || count (units group _x) == 1) ) then {
					if(isNil {_x getVariable "sa_join_squad_action_id"}) then {
						_actionId = _x addAction ["Join My Squad", { 
							if(isNil {(_this select 0) getVariable "sa_original_group"}) then {
								(_this select 0) setVariable ["sa_original_group",group (_this select 0)];
							};
							[_this select 0] join player; 
							(_this select 0) removeAction (_this select 2);
							(_this select 0) setVariable ["sa_join_squad_action_id",nil];
						}, nil, 0, false];
						_x setVariable ["sa_join_squad_action_id",_actionId];
					};
				} else {
					if(!isNil {_x getVariable "sa_join_squad_action_id"}) then {
						_x removeAction (_x getVariable "sa_join_squad_action_id");
						_x setVariable ["sa_join_squad_action_id",nil];
					};
				};
				
				if( side player == side _x && alive _x && group _x == group player && leader player == player && _x isKindOf "Man" && vehicle _x == _x && _x != player ) then {
					if(isNil {_x getVariable "sa_leave_squad_action_id"}) then {
						_actionId = _x addAction ["Leave My Squad", { 
							if(!isNil {(_this select 0) getVariable "sa_original_group"}) then {
								[_this select 0] join ((_this select 0) getVariable "sa_original_group"); 
								(_this select 0) setVariable ["sa_original_group",nil];
							} else {
								[_this select 0] join grpNull; 
							};
							(_this select 0) removeAction (_this select 2);
							(_this select 0) setVariable ["sa_leave_squad_action_id",nil];
						}, nil, 0, false];
						_x setVariable ["sa_leave_squad_action_id",_actionId];
					};
				} else {
					if(!isNil {_x getVariable "sa_leave_squad_action_id"}) then {
						_x removeAction (_x getVariable "sa_leave_squad_action_id");
						_x setVariable ["sa_leave_squad_action_id",nil];
					};
				};
				
			} forEach _allAiUnits;
			sleep 10;
			};

		};

	};

};

 

This is what I got, but doesn't work. I have probably added t many brackets or something simple.

 

Thanks,

Reed

Share this post


Link to post
Share on other sites

Sure!

      {
      _allAIUnits = allUnits select {_x distanceSqr getpos player < 900}};

 


      _allAIUnits = allUnits select {_x distanceSqr getpos player < 900};

  • Like 1

Share this post


Link to post
Share on other sites

Just gonna drop this here, you know armaholic is full with scripts who does exactly this?

You can use them yourself already how they are or look into them and see how they did it?

Got a hard time recalling the name but about a year or 2 ago I've used one of them to which was even multiplayer capable.

Which allowed u to recruit any AI from the game from a nice GUI and automatically deleted them once u kicked them out of u group.

You can more compare it with a shop script but now it gives u AI and joins it up in your group while you are the leader.

Its a nice thing if u set this up beside a barracks and have the new spawned dudes running out the barracks... 

 

All roads lead to Rome.

  • Confused 1

Share this post


Link to post
Share on other sites
1 hour ago, Theo1143 said:

All roads lead to Rome.

 

Sure! My home made one just needs a right click on unit to join/dismiss friend. But, if forum can help for making scripts work.

  • Confused 1

Share this post


Link to post
Share on other sites
26 minutes ago, pierremgi said:

 

Sure! My home made one just needs a right click on unit to join/dismiss friend. But, if forum can help for making scripts work.

 

I tried your homemade one and still couldn't get it to work. Again probably I am doing something wrong.

I doubt any thing is conflicting with it.

I'd rather use your solution, but fell back to the scripted version.

 

What advantage does your version have over the script, asking so I can be educated, no offense intended?

 

Reed

 

Spoiler

//////////////Place in init.sqf

addMissionEventHandler ["entityRespawned", { 
  _unit = _this select 0; 
  if (local _unit && isPlayer _unit) then { 
   call fn_LetGoAction 
  }; 
}];

//////////////Place in initplayerlocal.sqf

fn_LetGoAction = {

  player addAction ["Recruit",{

   [cursorobject] join group player;

  },[],0,true,true,"","[_this,_target] call GOM_fnc_actionConditionRecruit"];

  
  player addAction ["Dismiss",{

   params ["_object","_caller"];

   _newGroup = createGroup side _caller;

   [cursorobject] join _newGroup;

  },[],0,true,true,"","[_this,_target] call GOM_fnc_actionConditionLeave"];

};

///////////////////////Not sure where to place

//Recruit
GOM_fnc_actionConditionRecruit = {

params ["_this","_target"];

(_target isequalto _this AND {_target isequalto vehicle _target} and {(cursorTarget != objnull)} AND {side cursorTarget in ([side _this] call BIS_fnc_friendlySides)} AND {_target distance cursorTarget <= 15})

};

//Dismiss
GOM_fnc_actionConditionLeave = {

params ["_this","_target"];

(_target isequalto _this AND {_target isequalto vehicle _target} and {(cursorTarget != objnull)} AND {cursorTarget in units group player} AND {_target distance cursorTarget <= 15})

};

 

 

Share this post


Link to post
Share on other sites
31 minutes ago, Jnr4817 said:

 

I tried your homemade one and still couldn't get it to work. I doubt any thing is conflicting with it.

I'd rather use your solution, but fell back to the scripted version.

 

What advantage does your version have over the script, asking so I can be educated, no offense intended?

 

Reed

 

I guess you are not speaking about mine!

Anyway, if you tried MGI tactical pack, the join/dismiss group feature must be activated  or deactivated via the dashboard:  "Group SA" and "manage group" buttons.

There is a full pdf documentation inside the addon to explain all features. Some of them like "make prisoner" need a bunch of conditions to be effective.

 

Share this post


Link to post
Share on other sites

The issue, is our server is vanilla only. I am looking for a simple solution to recruit and dismiss AI in the dedicated multiplayer environment.

Duda was our scripter, but he has been real busy lately with life and other things. I am doing the best I can to improve performance of our template, keep it vanilla or no requirements, and keep the same playability we have been used to.

Thanks again for the help.

Reed

Share this post


Link to post
Share on other sites
4 minutes ago, Jnr4817 said:

The issue, is our server is vanilla only. I am looking for a simple solution to recruit and dismiss AI in the dedicated multiplayer environment.

Duda was our scripter, but he has been real busy lately with life and other things. I am doing the best I can to improve performance of our template, keep it vanilla or no requirements, and keep the same playability we have been used to.

Thanks again for the help.

Reed

 

The reason why I'm trying to help you on your script. Did you remove the { }, like i mentioned and do you get some new error? It always a step by step solution when discussing about one script among others.

Share this post


Link to post
Share on other sites

I appreciate it truly.

 

I did remove the extra {}.

 

Testing again to get the error code.

 

Share this post


Link to post
Share on other sites

The script version works now. I had placed an extra }

 

Thanks again.

 

How is your version better then the script?

Performance, speed, optimized?

Share this post


Link to post
Share on other sites

OK

Strange! The script works for me (with your conditions). I don't get any error (just running this code) on hosted server. You shouldn't have one as you wrote if (!isDedicated)... so?

Perhaps another problem somewhere else. Difficult to say without the rpt file.

 

My version?

Share this post


Link to post
Share on other sites

No, the script you helped me with without the functions.

It works now on hosted. Tested with respawn too, no issues or errors.

 

This one

Spoiler

if (!isDedicated) then {

	[] spawn {

		while {true} do {
		
		_allAIUnits = allUnits select {_x distanceSqr getpos player < 900};
		
			if (count _allAiUnits > 0) then {{
				if( side player == side _x && alive _x && group _x != group player && leader player == player && _x isKindOf "Man" && vehicle _x == _x && (leader _x != _x || count (units group _x) == 1) ) then {
					if(isNil {_x getVariable "sa_join_squad_action_id"}) then {
						_actionId = _x addAction ["Join My Squad", { 
							if(isNil {(_this select 0) getVariable "sa_original_group"}) then {
								(_this select 0) setVariable ["sa_original_group",group (_this select 0)];
							};
							[_this select 0] join player; 
							(_this select 0) removeAction (_this select 2);
							(_this select 0) setVariable ["sa_join_squad_action_id",nil];
						}, nil, 0, false];
						_x setVariable ["sa_join_squad_action_id",_actionId];
					};
				} else {
					if(!isNil {_x getVariable "sa_join_squad_action_id"}) then {
						_x removeAction (_x getVariable "sa_join_squad_action_id");
						_x setVariable ["sa_join_squad_action_id",nil];
					};
				};
				
				if( side player == side _x && alive _x && group _x == group player && leader player == player && _x isKindOf "Man" && vehicle _x == _x && _x != player ) then {
					if(isNil {_x getVariable "sa_leave_squad_action_id"}) then {
						_actionId = _x addAction ["Leave My Squad", { 
							if(!isNil {(_this select 0) getVariable "sa_original_group"}) then {
								[_this select 0] join ((_this select 0) getVariable "sa_original_group"); 
								(_this select 0) setVariable ["sa_original_group",nil];
							} else {
								[_this select 0] join grpNull; 
							};
							(_this select 0) removeAction (_this select 2);
							(_this select 0) setVariable ["sa_leave_squad_action_id",nil];
						}, nil, 0, false];
						_x setVariable ["sa_leave_squad_action_id",_actionId];
					};
				} else {
					if(!isNil {_x getVariable "sa_leave_squad_action_id"}) then {
						_x removeAction (_x getVariable "sa_leave_squad_action_id");
						_x setVariable ["sa_leave_squad_action_id",nil];
					};
				};
				
			} forEach _allAiUnits;
			sleep 10;
			};

		};

	};

};

 

 

Share this post


Link to post
Share on other sites

Ah OK. I didn't catch that because you asked for:    How is your version better then the script? Performance, speed, optimized?

 

Your function SA_fnc_getNearEntities has no chance to work:  array + array is not a working syntax.

And toooo much complicated for something easy to get with allUnits. (see BIKI)

 

You can even simplify your condition:

if( side player == side _x && alive _x && group _x != group player && leader player == player && _x isKindOf "Man" && vehicle _x == _x && (leader _x != _x || count (units group _x) == 1) ) then { if(isNil {_x getVariable "sa_join_squad_action_id"}) then { ....

 

becomes:

if( side _x == playerside  && group _x != group player && leader player == player  && (leader _x != _x || count (units group _x) == 1) && isNil {_x getVariable "sa_join_squad_action_id"}) then {...

 

Share this post


Link to post
Share on other sites
2 hours ago, pierremgi said:

 

Sure! My home made one just needs a right click on unit to join/dismiss friend. But, if forum can help for making scripts work.

 

He gives the impression he knows how to script so opening a script who exactly does that also shows him different ways of doing it.

I'm talking about scripts not mods like MCC, I never said he couldn't ask anything on here so I got no idea what u were trying to do there.

  • Confused 1

Share this post


Link to post
Share on other sites
2 hours ago, Theo1143 said:

 

He gives the impression he knows how to script so opening a script who exactly does that also shows him different ways of doing it.

I'm talking about scripts not mods like MCC, I never said he couldn't ask anything on here so I got no idea what u were trying to do there.

 

Nothing wrong about you or some script "allowing u to recruit any AI from the game from a nice GUI". Not a mod.

As you said, all roads lead to Rome. So, I imagined mine also. The reason why I spoke about my mod (my bad). Regardless of mod, it was an example for joining/dismiss unit with a simple right click (tempo zoom).

I added it's a good thing to have a forum for scripting conversation and to share some working or improving codes.  I think it's always better to try some personal code instead of copy/paste it. The scripting is a real source of pleasure... when it works.

Share this post


Link to post
Share on other sites

Yes, sorry I confused you.

 

I was asking about how your first solution the fn_letgoaction and GOM's functions script is better then the above script we were just talking about.

 

Theo I know minimal about scripting, truly enough to get myself in trouble, like above, and am trying to learn more.

 

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

×