Jump to content
Sign in to follow this  
TheGeneral2899

Repeating Code - Question on proper way to create external scripts

Recommended Posts

Hello All,

 

To keep it as brief as possible, I have a script which is being run at the mission start which is a while loop.  There are several blocks of code that are repeated on specific "rounds" of the while loop as such:

if ( _i == 7 || _i == 12 || _i == 17 || _i == 21 || _i == 23 || _i == 26 || _i == 30 || _i == 35 || _i == 39 || _i == 42 || _i == 47 || _i == 54) then 
	{
  		#BLOCK CODE #1
};
if ( _i == 9 || _i == 15 || _i == 22 || _i == 27 || _i == 34 || _i == 40 || _i == 54) then 
	{
  		BLOCK CODE #2
};

Everything works fine currently, however I am wondering if I can replace the blocks of code to some external script in order for better management and not have it be one giant script with all code in each while loop if check.  More importantly have a "BlockCode1.sqf" and "BlockCode2.sqf" scripts which are being called in the main script.

 

For reference the code basically does some selectRandom checks and then spawns some AI / vehicles and moves them to designated waypoints.

 

As I've understood it would be either via spawn, execVM or call, however I am not sure which would be best here.

 

Any experienced coders advice would be great!

Share this post


Link to post
Share on other sites

You could do:

if ((_i in [7, 12, 17, 21, 23, 26, 30, 35, 39, 42, 47, 54])) then
{
} else
{
	if ((_i in [9, 15, 22, 27, 34, 40, 54])) then
	{
	} else
	{
	};
};

 

Share this post


Link to post
Share on other sites

Hey @HazJ - The issue isnt with the while loop or the if checks.  Those are all working fine, while I'm sure yes there are "better" ways of doing it.   

My questions is how can I replace the giant blocks of scripts in the if statemetns with calls to external scripts in an efficient manner for server resources.

Share this post


Link to post
Share on other sites

Yes. I was showing you that you can use "in" command rather than lots of || checks. What giant script/code? Can you paste more please? As for execVM, call and spawn commands. It depends really... What else is in your code?

Share this post


Link to post
Share on other sites
6 minutes ago, TheGeneral2899 said:

Hey @HazJ - The issue isnt with the while loop or the if checks.  Those are all working fine, while I'm sure yes there are "better" ways of doing it.   

My questions is how can I replace the giant blocks of scripts in the if statemetns with calls to external scripts in an efficient manner for server resources.

Don't worry about performance/resources unless you're running into performance/resource problems.

Is the latter the case?

 

You could always define files as functions and use "call" or "spawn" to execute them

Have a read.

 

Cheers

Share this post


Link to post
Share on other sites

@HazJ - Ahhh cheers man.  Okay I will implement that.  I imagine that is just checking if its in the array of nmber as oppose to like 9 individual checks?

 

Here the script itself in its entirety:

 



// CREATED BY THEGENERAL - http://steamcommunity.com/profiles/76561197961489650/myworkshopfiles/?appid=107410
// Enjoy!


// CREATING GEAR UP TASK
[west,["gear"],["Gear up in the armory.  We've got limited ammo, so make due with what we have.   It looks like this is going to be a shitshow real fast.","Get Geared Up",gear],gear,1,1,true,"defend"] call BIS_fnc_taskCreate;

// START LOOP TO SPAWN WAVES
_i = 1;

while { _i < 70 } do {

   	// SELECTS A RANDOM LOCATION TO SPAWN AI FROM MARKERS PLACED ON MAP ON EVERY LOOP
	_spawnloc = ["spawn_1", "spawn_2", "spawn_3", "spawn_4", "spawn_5", "spawn_6", "spawn_7", "spawn_8", "spawn_9", "spawn_10", "spawn_11", "spawn_12", "spawn_13", "spawn_14", "spawn_15", "spawn_16", "spawn_17", "spawn_18", "spawn_19", "spawn_20", "spawn_21", "spawn_22"] call BIS_fnc_selectRandom; 

	// SELECTS A GROUP TO SPAWN - FOUND USING CfgGroups
	_group = ["LOP_ISTS_Support_section", "LOP_ISTS_Rifle_squad", "LOP_ISTS_InfSupTeam", "LOP_ISTS_Fireteam" ] call BIS_fnc_selectRandom;	

	// SPAWNS THE GROUP NAMED _aiGRP
	_aiGRP = [getMarkerPos _spawnloc, INDEPENDENT, (configfile >> "CfgGroups" >> "Indep" >> "LOP_ISTS" >> "Infantry" >> _group)] call BIS_fnc_spawnGroup;

	// CREATE WAYPOINT AND ATTRIBUTES
	_waypoint0 = _aiGRP addWaypoint [getMarkerPos "ai_move",0];
	_waypoint0 setWaypointType "MOVE"; 
	_waypoint0 setWaypointFormation "STAG COLUMN";
	_waypoint0 setWaypointSpeed "FULL";
	_waypoint0 setWaypointBehaviour "AWARE";

	// CREATE TASK INFORMING PLAYERS OF ONCOMING ATTACK
	if ( _i == 1) then 
	{
		// CREATING STARTING TASK
		[west,["ciabase"],["We're getting disturbing intel from local militia that all of Benghazi has turned to shit.  We don't know who's friendly, who's hostile and we've got ensure the Annex is secure until we hear otherwise.  If we lose control over the Annex, mission failed boys, so keep the area secure at all costs.","Defend the Annex",ciabase],ciabase,1,1,true,"defend"] call BIS_fnc_taskCreate;
	};


	// DICTATE WHICH LOOPS WILL SPAWN TROOP VEHICLES
	if ( _i == 7 || _i == 12 || _i == 17 || _i == 21 || _i == 23 || _i == 26 || _i == 30 || _i == 35 || _i == 39 || _i == 42 || _i == 47 || _i == 54) then 
	{

		// SAME AS ABOVE JUST USING VEHICLE SET MARKERS
		_vehiclespawnloc = ["vehspawn_1", "vehspawn_2", "vehspawn_3"] call BIS_fnc_selectRandom;

		// IF STATEMENT MAKING SURE THE DROP OFF WAYPOINT IS ON CORRECT SIDE (NORTH OR SOUTH) ACCORDING TO SPAWN LOCATION
		if (_vehiclespawnloc == "vehspawn_1") then {
			vehDrop = "vehdropoff_1";
		};
		if (_vehiclespawnloc == "vehspawn_2") then {
			vehDrop = "vehdropoff_2";
		};
		if (_vehiclespawnloc == "vehspawn_3") then {
			vehDrop = "vehdropoff_3";
		};


		// SAME AS ABOVE JUST USING VEHICLE SET MARKERS
		_vehiclespawntype = ["I_G_Offroad_01_F", "I_C_Van_02_transport_F"] call BIS_fnc_selectRandom;
		_vehspawngroup = ["LOP_ISTS_Support_section", "LOP_ISTS_Rifle_squad", "LOP_ISTS_InfSupTeam", "LOP_ISTS_Fireteam" ] call BIS_fnc_selectRandom;

		// SPAWNS VEHICLE IN AND SETS ITS DIRECTION
		_veh = _vehiclespawntype createVehicle getMarkerPos _vehiclespawnloc; 
		createVehicleCrew _veh;
		_dirVeh = markerDir _vehiclespawnloc;
		_veh setDir _dirVeh;

		// SETS GROUP OF THE DRIVER
		_vehgrp = group driver _veh;

		// SPAWNS IN GROUP TO JOIN THE VEHICLE ASSAULT
		_aiVehGroup = [getMarkerPos _vehiclespawnloc, INDEPENDENT, (configfile >> "CfgGroups" >> "Indep" >> "LOP_ISTS" >> "Infantry" >> _vehspawngroup)] call BIS_fnc_spawnGroup;

		// MOVES SPAWNED UNITS INTO VEHICLE
		{_x moveInCargo _veh} forEach units _aiVehGroup;
		_vehgrouparray = units _aiVehGroup;
		_vehgrouparray join _vehgrp;

		// CREATE WAYPOINT AND ATTRIBUTES
		_waypoint0 = _vehgrp addWaypoint [getMarkerPos vehDrop, 0];
		_waypoint0 setWaypointType "TR UNLOAD"; 
		_waypoint0 setWaypointFormation "FILE";
		_waypoint0 setWaypointSpeed "FULL";
		_waypoint0 setWaypointBehaviour "AWARE";

		_waypoint0 = _vehgrp addWaypoint [getMarkerPos vehDrop, 0];
		_waypoint0 setWaypointType "GETOUT"; 
		_waypoint0 setWaypointFormation "FILE";
		_waypoint0 setWaypointSpeed "FULL";
		_waypoint0 setWaypointBehaviour "AWARE";

		_waypoint1 = _vehgrp addWaypoint [getMarkerPos "ai_move",0];
		_waypoint1 setWaypointType "MOVE"; 
		_waypoint1 setWaypointFormation "STAG COLUMN";
		_waypoint1 setWaypointSpeed "FULL";
		_waypoint1 setWaypointBehaviour "AWARE";

	};

	// DICTATE WHICH LOOPS WILL 50 CAL TECHNICALS
	if ( _i == 9 || _i == 15 || _i == 22 || _i == 27 || _i == 34 || _i == 40 || _i == 54) then 
	{

		// SAME AS ABOVE JUST USING VEHICLE SET MARKERS
		_vehiclespawnloc = ["vehspawn_1", "vehspawn_2", "vehspawn_3"] call BIS_fnc_selectRandom;

		// IF STATEMENT MAKING SURE THE DROP OFF WAYPOINT IS ON CORRECT SIDE (NORTH OR SOUTH) ACCORDING TO SPAWN LOCATION
		if (_vehiclespawnloc == "vehspawn_1") then {
			vehDrop = "vehdropoff_1";
		};
		if (_vehiclespawnloc == "vehspawn_2") then {
			vehDrop = "vehdropoff_2";
		};
		if (_vehiclespawnloc == "vehspawn_3") then {
			vehDrop = "vehdropoff_3";
		};

		// SPAWNS VEHICLE IN AND SETS ITS DIRECTION
		_veh = "LOP_ISTS_Landrover_M2" createVehicle getMarkerPos _vehiclespawnloc; 
		createVehicleCrew _veh;
		_dirVeh = markerDir _vehiclespawnloc;
		_veh setDir _dirVeh;

		// SETS GROUP OF THE DRIVER
		_vehgrp = group driver _veh;

		// CREATE WAYPOINT AND ATTRIBUTES
		_waypoint0 = _vehgrp addWaypoint [getMarkerPos vehDrop, 0];
		_waypoint0 setWaypointType "MOVE"; 
		_waypoint0 setWaypointFormation "FILE";
		_waypoint0 setWaypointSpeed "FULL";
		_waypoint0 setWaypointBehaviour "AWARE";

	};

	// MORTAR FIRE ON BASE (2 SHOTS ON 2 RANDOM LOCATIONS IN BASE)
	if (  _i == 30 || _i == 37 || _i == 43 || _i == 53 || _i == 57 || _i == 63 || _i == 70) then 
	{

		_center = getMarkerpos "ai_move";
		_radius = 90;  
		_mortarTarget1 = [
			(_center select 0) - _radius + (2 * random _radius),
			(_center select 1) - _radius + (2 * random _radius), 0];
		_mortarTarget2 = [
			(_center select 0) - _radius + (2 * random _radius),
			(_center select 1) - _radius + (2 * random _radius), 0];
		mort1 commandArtilleryFire [_mortarTarget1, getArtilleryAmmo [mort1] select 0, 1];
		sleep 5;		
		mort2 commandArtilleryFire [_mortarTarget2, getArtilleryAmmo [mort2] select 0, 1];

	};

	// AMMO STASH SPAWN AND TASK CREATION
	if ( _i == 31) then 
	{
		// RANDOM TASK VARIABLES
		ammoTaskSpawnLoc = ["ammostash", "ammostash_1", "ammostash_2"] call BIS_fnc_selectRandom;  

		// SPAWN AMMO CRATE
		_currBox = "Box_AAF_Uniforms_F" createVehicle getMarkerPos ammoTaskSpawnLoc;
		clearMagazineCargoGlobal _currBox;
		clearWeaponCargoGlobal _currBox;
		clearItemCargoGlobal _currBox;

		// SPAWN WEAPONS
		_currBox addWeaponCargoGlobal ["rhs_weap_m4a1_carryhandle",2];
		_currBox addWeaponCargoGlobal ["rhs_weap_m4a1_carryhandle_grip2",2];
		_currBox addWeaponCargoGlobal ["rhs_weap_m249_pip_S",1];
		_currBox addWeaponCargoGlobal ["rhsusf_weap_m9",5];

		// SPAWN AMMO
		_currBox addMagazineCargoGlobal ["rhs_200rnd_556x45_M_SAW",8];
		_currBox addMagazineCargoGlobal ["30Rnd_556x45_Stanag",40];
		_currBox addMagazineCargoGlobal ["rhsusf_mag_17Rnd_9x19_FMJ",10];

		// CREATING TASK TO SHOW PLAYERS
		[west,["ammotask"],["We've got some safe houses located within the city where we have some ammo stashed.  If we start to get low on ammo, send a team out there to bring those crates back.","Ammo Stash (optional)",ammoTaskSpawnLoc],ammoTaskSpawnLoc,1,1,true,"rearm"] call BIS_fnc_taskCreate;

		_ammoGuards = [getMarkerPos ammoTaskSpawnLoc, INDEPENDENT, (configfile >> "CfgGroups" >> "Indep" >> "LOP_ISTS" >> "Infantry" >> "LOP_ISTS_Fireteam")] call BIS_fnc_spawnGroup;
		// CREATE WAYPOINT FOR AMMO GUARDS
		_waypoint5 = _ammoGuards addWaypoint [getMarkerPos "ammoTaskSpawnLoc",0];
		_waypoint5 setWaypointType "GUARD"; 
		_waypoint5 setWaypointFormation "STAG COLUMN";
		_waypoint5 setWaypointSpeed "FULL";
		_waypoint5 setWaypointBehaviour "COMBAT";

	};

	// VIP SPAWN AND TASK CREATION
	if ( _i == 44) then 
	{

		// SPAWNS THE GROUPS TO ASSAULT DIPLO COMPOUND
		_aiGRP1 = [getMarkerPos "spawn_diplo_1", INDEPENDENT, (configfile >> "CfgGroups" >> "Indep" >> "LOP_ISTS" >> "Infantry" >> "LOP_ISTS_Fireteam")] call BIS_fnc_spawnGroup;
		_aiGRP2 = [getMarkerPos "spawn_diplo_2", INDEPENDENT, (configfile >> "CfgGroups" >> "Indep" >> "LOP_ISTS" >> "Infantry" >> "LOP_ISTS_Fireteam")] call BIS_fnc_spawnGroup;
		_aiGRP3 = [getMarkerPos "spawn_diplo_3", INDEPENDENT, (configfile >> "CfgGroups" >> "Indep" >> "LOP_ISTS" >> "Infantry" >> "LOP_ISTS_Fireteam")] call BIS_fnc_spawnGroup;
		_aiGRP4 = [getMarkerPos "spawn_diplo_4", INDEPENDENT, (configfile >> "CfgGroups" >> "Indep" >> "LOP_ISTS" >> "Infantry" >> "LOP_ISTS_Fireteam")] call BIS_fnc_spawnGroup;

		// CREATE WAYPOINT AND ATTRIBUTES FOR EACH GROUP
		_waypoint0 = _aiGRP1 addWaypoint [getMarkerPos "diplocompound",0];
		_waypoint0 setWaypointType "MOVE"; 
		_waypoint0 setWaypointFormation "STAG COLUMN";
		_waypoint0 setWaypointSpeed "FULL";
		_waypoint0 setWaypointBehaviour "AWARE";

		_waypoint1 = _aiGRP2 addWaypoint [getMarkerPos "diplocompound",0];
		_waypoint1 setWaypointType "MOVE"; 
		_waypoint1 setWaypointFormation "STAG COLUMN";
		_waypoint1 setWaypointSpeed "FULL";
		_waypoint1 setWaypointBehaviour "AWARE";

		_waypoint2 = _aiGRP3 addWaypoint [getMarkerPos "diplocompound",0];
		_waypoint2 setWaypointType "MOVE"; 
		_waypoint2 setWaypointFormation "STAG COLUMN";
		_waypoint2 setWaypointSpeed "FULL";
		_waypoint3 setWaypointBehaviour "AWARE";

		_waypoint3 = _aiGRP4 addWaypoint [getMarkerPos "diplocompound",0];
		_waypoint3 setWaypointType "MOVE"; 
		_waypoint3 setWaypointFormation "STAG COLUMN";
		_waypoint3 setWaypointSpeed "FULL";
		_waypoint3 setWaypointBehaviour "AWARE";

		// RENABLING CARS IN COMPOUND TO TAKE DAMAGE
		car1 allowDamage true;
		car2 allowDamage true;
		car3 allowDamage true;
		car4 allowDamage true;

		// CREATING TASK TO SHOW PLAYERS
		[west,["redcuediplo"],["The US Diplomat to Lybia is trapped inside the US Embassy compound to the north.  They are under attack and we've been give the go ahead to rescue and extract him.  Locate the US diplomat and extract him back to the Annex.  HINT: Use Cable Ties!","Rescue US Diplomat",diplo],diplo,1,1,true,"meet"] call BIS_fnc_taskCreate;

	};

	sleep 37; //--- delay between rounds
	_i = _i + 1;

};

 

 

Share this post


Link to post
Share on other sites
8 minutes ago, Grumpy Old Man said:

Don't worry about performance/resources unless you're running into performance/resource problems.

Is the latter the case?

 

You could always define files as functions and use "call" or "spawn" to execute them

Have a read.

 

Cheers

 

Awesome!  That is exactly what I needed and will have a read.  Cant believe I didnt find this myself... :down:

 

As for performance issues, I image it was more because of the mass of AI that is being spawned in.

 

Share this post


Link to post
Share on other sites

Make a new function. Pass important arguments to it. For example, don't repeat createVehicle and other stuff unless really needed. Hopefully you get the idea:

// ["C_SUV_01_F", ["spawn10", "spawn20", "spawn30"]] call TAG_fnc_someFunction;
// [(selectRandom ["C_SUV_01_F", "C_Hatchback_01_sport_F", "C_Truck_02_covered_F"]), ["spawn10", "spawn20", "spawn30"]] call TAG_fnc_someFunction;

params
[
	["_vehicleType", "C_Offroad_01_F"],
	["_spawnLocations", ["spawn1", "spawn2", "spawn3"]]
];

_vehicle = _vehicleType createVehicle (selectRandom (getMarkerPos _spawnLocations));

Not tested. I recommend you use selectRandom command instead of BIS function. Also, you can use a nested array for stuff like this:

_spawn = selectRandom [["vehspawn_1", "vehdropoff_1"], ["vehspawn_2", "vehdropoff_2"], ["vehspawn_3", "vehdropoff_3"]];

hintSilent format ["%1 %2", (_spawn select 0), (_spawn select 1)];

This will cut out that if statement for checking which spawn location belongs to what dropoff point.

Share this post


Link to post
Share on other sites

@HazJ - Awesome that is also what I am looking for.   I know there are more efficient ways of doing things, in terms of code amount / functions and that perfectly illustrates what I am after.

 

Much appreciated to both of you @HazJ and @Grumpy Old Man

 

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
Sign in to follow this  

×