Jump to content
Cockheaven

Scripts looping for 'n' number of players

Recommended Posts

Hi all, I had made a mission a while ago and it has been stable from that time up until this point. The premise is to have a few objects placed that have addactions in them allowing players to start various training scenarios. I will attach one of these objects with the addaction it contains in it's init below as well as the script that addaction executes.

Obj init
 

Spoiler

this addaction ["Spawn Assault Force", {_this select 1;hint "Assault Force Spawned";playSound "Alarm_BLUFOR"; [] spawn {  _sound = ASLToAGL [0,0,0] nearestObject "#soundonvehicle";  sleep 6.1;  deleteVehicle _sound; };execVM "AssaultAiSpawn.sqf"; ;} ];

 


AssaultAISpawn.sqf
 

Spoiler

//_markers = allMapMarkers select {toUpper _x find "CONVOY" >= 0};
if(!isServer)exitWith{
_myunitarray = ["CUP_O_BMP_HQ_CSAT","O_APC_WHEELED_02_RCWS_V2_F","O_APC_TRACKED_02_CANNON_F","O_MBT_02_CANNON_F","O_MBT_04_CANNON_F"];
_veh = selectRandom _myunitarray createVehicle getMarkerPos "ASSAULT1_1";
_veh setDir 160;
createVehicleCrew _veh;
_veh3 = selectrandom _myunitarray createVehicle getMarkerPos "ASSAULT1_3";
createVehicleCrew _veh3;
_veh3 setDir 160;
_veh4 = selectrandom _myunitarray createVehicle getMarkerPos "ASSAULT1_4";
createVehicleCrew _veh4;
_veh4 setDir 160;
_veh5 = selectrandom _myunitarray createVehicle getMarkerPos "ASSAULT1_5";
createVehicleCrew _veh5;
_veh5 setDir 160;
_veh6 = selectRandom _myunitarray createVehicle getMarkerPos "ASSAULT1_6";
createVehicleCrew _veh6;
_veh6 setDir 160;
_veh2 = selectRandom _myunitarray createVehicle getMarkerPos "ASSAULT1_2";
createVehicleCrew _veh2;
_veh2 setDir 160;
_veh7 = selectRandom _myunitarray createVehicle getMarkerPos "ASSAULT1_7";
createVehicleCrew _veh7;
_veh7 setDir 160;
_veh8 = selectRandom _myunitarray createVehicle getMarkerPos "ASSAULT1_8";
createVehicleCrew _veh8;
_veh8 setDir 160;

_veh2crew = crew _veh2; //this will give you an array which has the crew members.
_veh3crew = crew _veh3;
_veh4crew = crew _veh4;
_veh5crew = crew _veh5;
_veh6crew = crew _veh6;
_veh7crew = crew _veh7;
_veh8crew = crew _veh8;

_vehgrp = group driver _veh; //you get the convoy leader's group with this little command
_veh2crew join _vehgrp; //Array is needed for join command.
_veh3crew join _vehgrp;
_veh4crew join _vehgrp;
_veh5crew join _vehgrp;
_veh6crew join _vehgrp;
_veh7crew join _vehgrp;
_veh8crew join _vehgrp;


			[_vehgrp, getMarkerPos "SLUGFEST1"] call bis_fnc_taskAttack;
			[_vehgrp, 1] setWaypointSpeed "NORMAL";
			[_vehgrp, 1] setWaypointCombatMode "RED";
			[_vehgrp, 1] setWaypointBehaviour "SAFE";
			[_vehgrp, 1] setWaypointFormation "LINE";

			[_vehgrp, getMarkerPos "SLUGFEST2"] call bis_fnc_taskAttack;
			[_vehgrp, 1] setWaypointSpeed "NORMAL";
			[_vehgrp, 1] setWaypointCombatMode "RED";
			[_vehgrp, 1] setWaypointBehaviour "SAFE";
			[_vehgrp, 1] setWaypointFormation "LINE";

			[_vehgrp, getMarkerPos "SLUGFEST3"] call bis_fnc_taskAttack;
			[_vehgrp, 1] setWaypointSpeed "NORMAL";
			[_vehgrp, 1] setWaypointCombatMode "RED";
			[_vehgrp, 1] setWaypointBehaviour "SAFE";
			[_vehgrp, 1] setWaypointFormation "LINE";
};



if ( "ASLT1" call BIS_fnc_taskExists ) then {
		"ASLT1" call BIS_fnc_deleteTask
	}; //delete any existing ASLT1

[west,["ASLT1"],["A CSAT Assault force has landed near Lolisse. Intercept and Destroy the enemy.","Slug Fest","tank"],_veh,1,3,true] call BIS_fnc_taskCreate;

{
		_x addEventHandler [ "Killed", {[ "ASLT1", "SUCCEEDED", true ] call BIS_fnc_taskSetState;}]
}forEach units _veh;

if ( "ASLT2" call BIS_fnc_taskExists ) then {
		"ASLT2" call BIS_fnc_deleteTask
	}; //delete any existing ASLT1

[west,["ASLT2"],["A CSAT Assault force has landed near Lolisse. Intercept and Destroy the enemy.","Slug Fest","tank"],_veh2,1,3,true] call BIS_fnc_taskCreate;

{
		_x addEventHandler [ "Killed", {[ "ASLT2", "SUCCEEDED", true ] call BIS_fnc_taskSetState;}]
}forEach units _veh2;

if ( "ASLT3" call BIS_fnc_taskExists ) then {
		"ASLT3" call BIS_fnc_deleteTask
	}; //delete any existing ASLT1

[west,["ASLT3"],["A CSAT Assault force has landed near Lolisse. Intercept and Destroy the enemy.","Slug Fest","tank"],_veh3,1,3,true] call BIS_fnc_taskCreate;

{
		_x addEventHandler [ "Killed", {[ "ASLT3", "SUCCEEDED", true ] call BIS_fnc_taskSetState;}]
}forEach units _veh3;

if ( "ASLT4" call BIS_fnc_taskExists ) then {
		"ASLT4" call BIS_fnc_deleteTask
	}; //delete any existing ASLT1

[west,["ASLT4"],["A CSAT Assault force has landed near Lolisse. Intercept and Destroy the enemy.","Slug Fest","tank"],_veh4,1,3,true] call BIS_fnc_taskCreate;

{
		_x addEventHandler [ "Killed", {[ "ASLT4", "SUCCEEDED", true ] call BIS_fnc_taskSetState;}]
}forEach units _veh4;

if ( "ASLT5" call BIS_fnc_taskExists ) then {
		"ASLT5" call BIS_fnc_deleteTask
	}; //delete any existing ASLT1

[west,["ASLT5"],["A CSAT Assault force has landed near Lolisse. Intercept and Destroy the enemy.","Slug Fest","tank"],_veh5,1,3,true] call BIS_fnc_taskCreate;

{
		_x addEventHandler [ "Killed", {[ "ASLT5", "SUCCEEDED", true ] call BIS_fnc_taskSetState;}]
}forEach units _veh5;

if ( "ASLT6" call BIS_fnc_taskExists ) then {
		"ASLT6" call BIS_fnc_deleteTask
	}; //delete any existing ASLT1

[west,["ASLT6"],["A CSAT Assault force has landed near Lolisse. Intercept and Destroy the enemy.","Slug Fest","tank"],_veh6,1,3,true] call BIS_fnc_taskCreate;

{
		_x addEventHandler [ "Killed", {[ "ASLT6", "SUCCEEDED", true ] call BIS_fnc_taskSetState;}]
}forEach units _veh6;

if ( "ASLT7" call BIS_fnc_taskExists ) then {
		"ASLT7" call BIS_fnc_deleteTask
	}; //delete any existing ASLT1

[west,["ASLT7"],["A CSAT Assault force has landed near Lolisse. Intercept and Destroy the enemy.","Slug Fest","tank"],_veh7,1,3,true] call BIS_fnc_taskCreate;

{
		_x addEventHandler [ "Killed", {[ "ASLT7", "SUCCEEDED", true ] call BIS_fnc_taskSetState;}]
}forEach units _veh7;

if ( "ASLT8" call BIS_fnc_taskExists ) then {
		"ASLT1" call BIS_fnc_deleteTask
	}; //delete any existing ASLT1

[west,["ASLT8"],["A CSAT Assault force has landed near Lolisse. Intercept and Destroy the enemy.","Slug Fest","tank"],_veh8,1,3,true] call BIS_fnc_taskCreate;

{
		_x addEventHandler [ "Killed", {[ "ASLT8", "SUCCEEDED", true ] call BIS_fnc_taskSetState;}]
}forEach units _veh8;

 

And so, as the post title says, these scripts seem to be repeating depending on the number of players on the server when someone executes the addaction. i'm stumped because this did not use to happen and I cannot figure out what may have changed to cause this. If someone can see a problem in my code (I'm sure they are there) or can suggest a more secure/better way of allowing players to spawn preconfigured enemies please help me out! Thanks!

P.S. There are a lot of people that have helped me this far and I am blatantly using their code, if you are one of them and you recognize your code, thank you!

Share this post


Link to post
Share on other sites

this addaction ["Spawn Assault Force", // will run on server + clients (so everybody will see the addAction)

  {

     _this select 1;  // no idea what you do with that

     hint "Assault Force Spawned";  // should trigger locally (on player who triggered the action)

     playSound "Alarm_BLUFOR";    // same

     [] spawn { _sound = ASLToAGL [0,0,0] nearestObject "#soundonvehicle"; sleep 6.1; deleteVehicle _sound; };  // same

      execVM "AssaultAiSpawn.sqf"; // run locally but the effects will depend on the code, so on each command used

  }];


Suggested init field (if you want to hear the alarm for all players, and spawn tanks on server):

Spoiler

 


alarmStart = {
  hint "Assault Force Spawned";
  playSound "Alarm_BLUFOR";
 [] spawn { _sound = ASLToAGL [0,0,0] nearestObject "#soundonvehicle"; sleep 6.1; deleteVehicle _sound; };
};
this addAction ["Spawn Assault Force",{alarmStart remoteExec ["call"];assaultSpawn remoteExec ["call",2]; },[], 1,true,true,"","",6];

 

 

 

Note: See below for assaultSpawn

 

AssaultAISpawn.sqf

if(!isServer)exitWith{...  } // why? usually the AI units are spawned on server (or headless client)... where they will be finally managed. They will not stay in local client PC except those who belong to the client group/vehicle (client leader/driver).

On the contrary, you should create your units on server, the remote exec your code on server.

 remove this condition.

 

Suggested code:

Spoiler

 


_myunitarray = ["O_APC_WHEELED_02_RCWS_V2_F","O_APC_TRACKED_02_CANNON_F","O_MBT_02_CANNON_F","O_MBT_04_CANNON_F"];
{
  _mk = _x;
  _idx = _x select [9];
  _grp = [ getMarkerPos _mk, EAST,[selectrandom _myunitarray],[],[],[],[],[],160] call BIS_fnc_spawnGroup;
  _veh = vehicle leader _grp;
  _curr = "ASLT"+_idx;

  [_grp, getMarkerPos "SLUGFEST1"] call bis_fnc_taskAttack;
  [_grp, 1] setWaypointCombatMode "RED";
  [_grp, 1] setWaypointBehaviour "SAFE";
  [_grp, 1] setWaypointFormation "LINE";

  [_grp, getMarkerPos "SLUGFEST2"] call bis_fnc_taskAttack;
  [_grp, 1] setWaypointCombatMode "RED";
  [_grp, 1] setWaypointBehaviour "SAFE";
  [_grp, 1] setWaypointFormation "LINE";

  [_grp, getMarkerPos "SLUGFEST3"] call bis_fnc_taskAttack;
  [_grp, 1] setWaypointCombatMode "RED";
  [_grp, 1] setWaypointBehaviour "SAFE";
  [_grp, 1] setWaypointFormation "LINE";

  _veh setVariable ["taskOnVeh",_curr];
  if ( _curr call BIS_fnc_taskExists) then {_curr call BIS_fnc_deleteTask};
  [west,[_curr],["A CSAT Assault force has landed near Lolisse. Intercept and Destroy the enemy.","Slug Fest","tank"],_veh,1,3,true] call BIS_fnc_taskCreate;
  _veh addEventHandler [ "Killed", { params ["_veh"];
    [_veh getVariable ["taskOnVeh","fake"],"SUCCEEDED"] call BIS_fnc_taskSetState
  }];

} forEach ["ASSAULT1_1","ASSAULT1_2","ASSAULT1_3","ASSAULT1_4","ASSAULT1_5","ASSAULT1_6","ASSAULT1_7","ASSAULT1_8"];

 

 

 

Then, modify your addAction (as above):

>   first:  don't execVM "AssaultAiSpawn.sqf", spawn it after compiling the processed file. That means, add this line in init.sqf:

AssaultSpawn = compile preprocessFileLineNumbers  "AssaultAiSpawn.sqf"; // assaultSpawn is now the code you can spawn/call and remote exec

>  second: replace in addAction:

  execVM "AssaultAiSpawn.sqf";   by:    assaultSpawn remoteExec ["call",2];  // you can call it instead of spawn it

 

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

×