Jump to content
Sign in to follow this  
m1ndgames

Spawn Ai in a Loop

Recommended Posts

Hi,

the script should spawn Ai and fly them to a waypoint, then the heli moves away and despawns when its far enough.

The number of Ai Forces that will be inserted should be double the size of the currently connected players. It should send another chopper when Ai count is less then Player count.

The loop:

// WarZones_Initialize_Server_Loop_AiSpawn.sqf
/////////////////////////////////////////////////////////////////////////////////////
// Create Independent Side Centers and Groups
INDEPENDENT_HQ = createCenter resistance;
INDEPENDENT_Group = createGroup INDEPENDENT_HQ;
["Created Independent Side Centers and Groups"] call WarZones_fnc_Debug;

[] spawn {
while {true} do {
	[] call WarZones_fnc_NPCspawn;
};
sleep 300;
};

The Spawn function:

// Fill array with all living Players
_allPlayers = [];
{
if (isPlayer _x) then
{
	_allPlayers pushBack _x;
};
} forEach playableUnits;

// Count the Players
_playerscount = count _allPlayers;

if (count (units INDEPENDENT_Group) > _playerscount) exitWith {};

// Create Transport Heli
heli = createVehicle ["I_Heli_Transport_02_F", getpos base_independent_flagpole, [], 3000, "FLY"];
["Spawned AAF Heli"] call WarZones_fnc_Debug;

// Create Heli Group
HELI_Group = createGroup INDEPENDENT_HQ;

// Assign heli group
[heli] join HELI_Group;

// Create Crew
createVehicleCrew heli;
["Created AAF Heli Crew"] call WarZones_fnc_Debug;

// Count empty seats
_helicargo = heli emptyPositions "cargo";

// Group cant be greater then cargo space
_groupsize = _playerscount *2;
if (_groupsize > _helicargo) then {
_groupsize = _helicargo;
};

// Create random AAF group
TROOPER_Group = [getPos heli, resistance, _groupsize] call BIS_fnc_spawnGroup;
["Created AAF Trooper Group"] call WarZones_fnc_Debug;

// Move into Cargo
{
_x moveInCargo heli;
[_x] join INDEPENDENT_Group;
} forEach units TROOPER_Group;

deleteGroup TROOPER_Group;
["Deleted AAF Trooper Group"] call WarZones_fnc_Debug;

// Set waypoint (AAF Base)
_waypoint_aaf = HELI_Group addWaypoint [getpos base_independent_flagpole, 0];

_waypoint_aaf setWaypointCompletionRadius 50;

_unload = createTrigger ["EmptyDetector", getpos base_independent_flagpole];
_unload setTriggerArea [100, 100, 0, false];
_unload setTriggerActivation ["GUER", "PRESENT", true];
_unload setTriggerStatements ["this", "hint 'Heli in Zone'", "hint 'Heli left Zone'"]; // Todo: Insert/Halo
_waypoint2 = HELI_Group addWaypoint [[0,150,0], 0];

// Move Heli away and de-spawn it
[] spawn {
while {true} do {
	_distance = [heli, getpos base_independent_flagpole] call BIS_fnc_distance2D;
	if (_distance > 3000) then {
		deleteVehicle heli;
		deleteGroup HELI_Group;
		["AAF Chopper: Despawned"] call WarZones_fnc_Debug;
	};
};
sleep 5;
};

Output:

13:56:48 ----> WarZones Debug:  Spawned AAF Heli
13:56:48 ----> WarZones Debug:  Created AAF Heli Crew
13:56:48 ----> WarZones Debug:  Created AAF Trooper Group
13:56:48 ----> WarZones Debug:  Deleted AAF Trooper Group
13:56:48 ----> WarZones Debug:  Spawned AAF Heli
13:56:48 ----> WarZones Debug:  Created AAF Heli Crew
13:56:48 ----> WarZones Debug:  Created AAF Trooper Group
13:56:48 ----> WarZones Debug:  Deleted AAF Trooper Group
13:56:48 ----> WarZones Debug:  Spawned AAF Heli
13:56:48 ----> WarZones Debug:  Created AAF Heli Crew
13:56:48 ----> WarZones Debug:  Created AAF Trooper Group
13:56:48 ----> WarZones Debug:  Deleted AAF Trooper Group
... (Loop)

I dont know why its looped.. It should detect the units in the group, right?

Share this post


Link to post
Share on other sites

//replace:
if (count (units INDEPENDENT_Group) > _playerscount) exitWith {};
//with
hintSilent format ["Units in IND_Group: %1", count units INDEPENDENT_Group];
if (count (units INDEPENDENT_Group) > _playerscount) exitWith {};

Writing tests is a good way to find and solve logical errors.

Also, you have way too many useless comments. Example:

_counter = _counter + 1 //increment "_counter" by 1

Share this post


Link to post
Share on other sites

I don't see what good the sleeps are "in" both of your while loops, seeing how they are outside the scope of the loop. Also neither loop has an exit condition, I recommend having one.

Share this post


Link to post
Share on other sites

Changed it a bit..

// WarZones_Initialize_Server_Loop_AiSpawn.sqf
/////////////////////////////////////////////////////////////////////////////////////
// Create Independent Side Centers and Groups
[] spawn {
scopeName "aispawn";
while {true} do {
	[] call WarZones_fnc_NPCspawn;
	sleep 300;
};
};

// Fill array with all connected Players
_allPlayers = [];
{
if (isPlayer _x) then
{
	_allPlayers pushBack _x;
};
} forEach playableUnits;

// Count the current Players
_playerscount = count _allPlayers;

// When Groupsize bigger then connected players: breakout to loop
if (count (units INDEPENDENT_Group) > _playerscount) then {
	breakOut "aispawn";
};

// Create Heli Group
HELI_Group = createGroup INDEPENDENT_HQ;

// Create Transport Heli
heli = createVehicle ["I_Heli_Transport_02_F", getpos base_independent_flagpole, [], 3000, "FLY"];
heli flyInHeight 300; ["Spawned AAF Heli"] call WarZones_fnc_Debug;

// Assign heli to group
[heli] join HELI_Group;

// Create Heli Crew
createVehicleCrew heli; ["Created AAF Heli Crew"] call WarZones_fnc_Debug;

// Count empty seats
_helicargo = heli emptyPositions "cargo";

// Group cant be greater then cargo space
_groupsize = _playerscount *2;
if (_groupsize > _helicargo) then {
_groupsize = _helicargo;
};

// Create random AAF group with defined groupsize
TROOPER_Group = [getPos heli, resistance, _groupsize] call BIS_fnc_spawnGroup; ["Created AAF Trooper Group"] call WarZones_fnc_Debug;

// Move Troopers into Heli Cargo
{
_x moveInCargo heli;
[_x] join INDEPENDENT_Group;
} forEach units TROOPER_Group;

// Delete the empty Trooper Group
deleteGroup TROOPER_Group; ["Deleted AAF Trooper Group"] call WarZones_fnc_Debug;

// Set waypoint (AAF Base)
_waypoint_aaf = HELI_Group addWaypoint [getpos base_independent_flagpole, 0];
_waypoint_aaf setWaypointCompletionRadius 50;

// Create Trigger on AAF Base
_unload = createTrigger ["EmptyDetector", getpos base_independent_flagpole];
_unload setTriggerArea [100, 100, 0, false];
_unload setTriggerActivation ["GUER", "PRESENT", true];
_unload setTriggerStatements ["this", "hint 'Heli in Zone'", "hint 'Heli left Zone'"]; // Todo: Insert/Halo

// Move Heli away
_despawn = HELI_Group addWaypoint [[0,150,0], 0]; heli flyInHeight 500;

// Check heli distance to AAF base and de-spawn
scopeName "checkdistance";
while {true} do {
_distance = [heli, getpos base_independent_flagpole] call BIS_fnc_distance2D;
if (_distance > 3000) then {
	deleteVehicle heli;
	["AAF Chopper: Despawned"] call WarZones_fnc_Debug;
	breakOut "checkdistance";
};
sleep 5;
};

0:23:40 Error Undefined variable in expression: independent_group

0:23:40 Error Undefined variable in expression: heli_group

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  

×