Jump to content
Learthane

Help Needed in trying to create spawning script

Recommended Posts

Hello,

 

I recently came across the idea of making a spawning script where it allows me and my friends to do a "terrorist hunt" type of situation but at any of the markers, that have been preplaced throughout the map in EDEN editor.  The idea of the script is to randomly select one of these markers and spawn a variety of enemies, whose faction would be chosen through a mission parameter before the game begins. I understand how to make a teleporter to the location and the mission parameter stuff, but I've been having trouble creating a position that the createUnit function will accept. I was hoping I could just use getMarkerPos "marker x" and plug that into the attributes but that isn't working apparently. I've also tried other approaches such as BIS_fnc_randomPos but I can't figure that one out either. I will post my code below so that everyone can view it and make suggestions to help me with my problem. A lot of things have been inline commented out so that I could test if certain portions were working but so far I've only really been confident in the first 12 lines of code. Any help is appreciated, thanks!

 

// This script allows for the random spawning of enemies in a specified area

// Mission area generation
_areas = allMapMarkers; // Areas defined in editor for mission
//deleteMarker (selectRandom _areas)

_missionArea = selectRandom _areas; // Randomly selects an area from _areas array

// Spawn enemies
_spawnCenter = getMarkerPos _missionArea; // create spawning area for enemies
//_marker1 = createMarker  ["Tester", _spawnCenter];
//_marker1 setMarkerType "b_hq";


/*_radiusX = 200;
_radiusY = 200;

_randomX = random _radiusX; // Selects a value from 0 to _radiusX
_randomY = random _radiusY; // Selects a value from 0 to _radiusY

_randomPositionSelect = random 9; // Gives the ability for units to be spread out over the area
if (_randomPositionSelect >= 5) then {_randomX  = _randomX; _randomY = _randomY}
	else {_randomX = _randomX - (2*_randomX); _randomY = _randomY- (2*_randomY);
	};
	
_spawnPos = [_randomX , _randomY];*/

// Create array of units to choose from

//_randomPos = [_spawnCenter, ["water"]] call BIS_fnc_randomPos;

_opforUnits = ["O_G_Soldier_F","O_G_Soldier_lite_F","O_G_Soldier_SL_F","O_G_Soldier_TL_F","O_G_Soldier_AR_F"];


_group1 = createGroup east;
_enemy1 = group _group1 createUnit [selectRandom _opforUnits, _randomPos, [], 0, "NONE"];
/*_enemy2 = group createUnit [selectRandom _opforUnits, _spawnPos, [], 0, "NONE"];
_enemy3 = group createUnit [selectRandom _opforUnits, _spawnPos, [], 0, "NONE"];
_enemy4 = group createUnit [selectRandom _opforUnits, _spawnPos, [], 0, "NONE"];
_enemy4 = group createUnit [selectRandom _opforUnits, _spawnPos, [], 0, "NONE"];
_enemy5 = group createUnit [selectRandom _opforUnits, _spawnPos, [], 0, "NONE"];*/

//cutText ["Units Created","PLAIN",2];

 

Share this post


Link to post
Share on other sites

Try :

_enemy1 = _group1 createUnit [selectRandom _opforUnits, _randomPos, [], 0, "NONE"];

_group1 is already a group so you don't need the group command before it.

 

Also for the random position :

_randomPos = [[_missionArea], ["water"]] call BIS_fnc_randomPos;

 

Just an example of what I will use :

_areas = allMapMarkers;
_missionArea = selectRandom _areas;
_randomPos = [[_missionArea], ["water"]] call BIS_fnc_randomPos; // random pos in the marker area but not in water


_faction = "BLU_F"; // delete this after configuring your params under, just here for test

//_faction = paramsArray select 0; // change 0 by the right number

_unitsFaction = format ["getNumber (_x >> 'scope') >= 2 AND configname _x isKindOf 'CAManBase' AND gettext (_x >> 'faction') == '%1'", _faction] configClasses (configFile >> "CfgVehicles") apply {configName _x}; // Get all the units from the choosen faction

_group1 = createGroup [east, true]; // better delete the group when empty so use this synthax
_enemy1 = _group1 createUnit [selectRandom _unitsFaction, _randomPos, [], 0, "NONE"];

Hope this help 😉

Share this post


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

 




_unitsFaction = format ["getNumber (_x >> 'scope') >= 2 AND configname _x isKindOf 'CAManBase' AND gettext (_x >> 'faction') == '%1'", _faction] configClasses (configFile >> "CfgVehicles") apply {configName _x}; // Get all the units from the choosen faction

Hope this help 😉

Where did you get 'scope' , 'CAManBase' and 'faction' from? I've been searching around and can't find anything on them. I tried your example code but it is throwing an error:

if (_this isEqualTypeArray [[], 0, 0, 0, false]) e>
 0:11:00   Error position: <isEqualTypeArray [[], 0, 0, 0, false]) e>
 0:11:00   Error isequaltypearray: Type String, expected Array
 0:11:00 File A3\functions_f\Misc\fn_getArea.sqf [BIS_fnc_getArea]..., line 30
 0:11:20 [ACE] (advanced_ballistics) INFO: Finished terrain initialization in 31.00 seconds [world: Malden]
 0:11:21 Error in expression <s, rectangular _this, -1]};


if (_this isEqualTypeArray [[], 0, 0, 0, false]) e>
 0:11:21   Error position: <isEqualTypeArray [[], 0, 0, 0, false]) e>
 0:11:21   Error isequaltypearray: Type String, expected Array
 0:11:21 File A3\functions_f\Misc\fn_getArea.sqf [BIS_fnc_getArea]..., line 30

I understand all of the other parts of your post though. Should I have just edited my _enemy1 unit and my random position function?

Share this post


Link to post
Share on other sites

The error comes from the BIS_fnc_randomPos function.

 

Your marker need to be an area and not an icon.

 

5 minutes ago, Learthane said:

Where did you get 'scope' , 'CAManBase' and 'faction' from?

It's for the search condition in the config to get all the units class names of a given faction.

Share this post


Link to post
Share on other sites

If you want to use icon marker you can try :

// _randomPos = [[[center, radius]], ["water"]] call BIS_fnc_randomPos;

_randomPos = [[[getMarkerPos _missionArea, 200]], ["water"]] call BIS_fnc_randomPos; // circle with 200 radius

// or for rectangle or ellipse

// _randomPos = [[[center, [a, b, angle, rect]]], ["water"]] call BIS_fnc_randomPos;

_randomPos = [[[getMarkerPos _missionArea, [100, 200, 0, false]]], ["water"]] call BIS_fnc_randomPos; // ellipse with 100x200 size with 0° angle

_randomPos = [[[getMarkerPos _missionArea, [100, 200, 0, true]]], ["water"]] call BIS_fnc_randomPos; // rectangle with 100x200 size with 0° angle

 

Share this post


Link to post
Share on other sites
3 hours ago, Crazy_Man said:

Oh okay I see how that worked! I got a unit to spawn which is great! Unfortunately he spawned in rock. Is there anyway to keep that from happening? Also, if I just repeated the _group*n* (where n = 1,2,etc...) and enemy*n* for let's say 30 units. Would it spawn 30 different units in 30 separate groups? I would like for them to patrol after they spawn but not all of them if that makes sense. I have a feeling I have to use the BIS_fnc_taskPatrol at some point. Would it be best to have all the created units added to an array such as 


_allUnits = [_group1,_group2,groupn,...]

and then use


for [{_i = 0}, {_i < 15}, {_i = _i + 1}] do
 {
  _aUnit = selectRandom _allUnits;
  [group _aUnit, getPos _aUnit, 100] call BIS_fnc_taskPatrol;
};
 
                         

to create a separate patrol route for 15 of the 30 groups? I'm trying to think of a way to have a for loop create the groups and enemies where I don't have to hardcode a set number of groups. That way based on the number of players, I could change the amount of enemies based on the players using an if statement (if I have the correct thinking going on here). I do appreciate your help though. I've never coded in this language and I'm still trying to figure out how it all works especially with ARMA.

 

Share this post


Link to post
Share on other sites
18 hours ago, Learthane said:

I got a unit to spawn which is great! Unfortunately he spawned in rock.

 

You can use the BIS_fnc_findSafePos function instead/(or with) the BIS_fnc_randomPos function.

// [center, minDist, maxDist, objDist, waterMode, maxGrad, shoreMode, blacklistPos, defaultPos] call BIS_fnc_findSafePos;

_randomPos = [getMarkerPos _missionArea, 0, 200, 5, 0, 0, 0, [], []] call BIS_fnc_findSafePos; // this will return a random position around the _missionArea marker with 200 radius and 5 meters from any objects. See the wiki page for the other arguments

Keep in mind that if this function doesn't find any safe place it will use the defaultPos (last argument wich is empty array in my example). if you encounter a problem in this case, you can set a value to the defaultPos like [0,0,0] and create a while loop who increment de maxDist until the function find a safe place.

 

An example :

_randomPos = [0,0,0];
_maxDist = 200;

while {_randomPos isEqualTo [0,0,0]} do {

	_randomPos = [getMarkerPos _missionArea, 0, _maxDist, 5, 0, 0, 0, [], [0,0,0]] call BIS_fnc_findSafePos;
	_maxDist = _maxDist + 20;
};

 

 

18 hours ago, Learthane said:

Also, if I just repeated the _group*n* (where n = 1,2,etc...) and enemy*n* for let's say 30 units. Would it spawn 30 different units in 30 separate groups?

Yes. So don't repeat the createGroup command for each unit.

 

Finally, you can make some groups patrol and other groups don't patrol.

 

 

An example :

// Marker random choice
_areas = allMapMarkers;
_missionArea = selectRandom _areas;

// Faction class names
_faction = "BLU_F"; // for test
//_faction = paramsArray select 0; // replace 0 by the right params
_unitsFaction = format ["getNumber (_x >> 'scope') >= 2 AND configname _x isKindOf 'CAManBase' AND gettext (_x >> 'faction') == '%1'", _faction] configClasses (configFile >> "CfgVehicles") apply {configName _x}; 

// Config main loop
_side = getNumber (configfile >> "CfgVehicles" >> (_unitsFaction select 0) >> "side") call BIS_fnc_sideType; // get the right side of the choosen faction
_amountOfGroups = 4;
_amountOfPatrols = 3;
_minUnitsPerGroup = 8;



// Main loop
for "_i" from 0 to _amountOfGroups-1 do {

	// Search for safe random position
	_randomPos = [0,0,0];
	_maxDist = 200;

	while {_randomPos isEqualTo [0,0,0]} do {

		_randomPos = [getMarkerPos _missionArea, 0, _maxDist, 5, 0, 0, 0, [], [0,0,0]] call BIS_fnc_findSafePos;
		_maxDist = _maxDist + 20;
	};

	// Difficulty
	_nbePlayers = count (call BIS_fnc_listPlayers - entities "HeadlessClient_F"); // count the amount of players without headLessClient
	_amountOfUnits = _minUnitsPerGroup + _nbePlayers; // It s just an idea, do as you like


	// Spawn the group
	_group = createGroup [_side, true];

	for "_j" from 0 to _amountOfUnits-1 do {
		_enemy = _group createUnit [selectRandom _unitsFaction, _randomPos, [], 0, "NONE"];
	};

	// Make it patrol or not
	if (_i < _amountOfPatrols) then {

		[_group, getPos leader _group, 100] call BIS_fnc_taskPatrol;
	};	

};

Beware, if your choosen faction isn't in the his right side (faction "BLU_F" but group created with east side), the units will kill each other.

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

×