Jump to content
jakkob4682

Help with spawning groups from arrays inside a parent array

Recommended Posts

So I'll share what I have so far.  Be brutally honest with me if there's something I'm not doing/should(n't) be doing.  It's incomplete currently.  What I'm trying to do is create a defensive group and patrol group for each position in an array.  The groups are going to  be spawned in the same function.  The patrol group is going to be a qrf group.  So the question is how do I create unique group names inside a for-from loop.  

 

As always thanks for any advice/tips/criticism.

 

Code is in the spoiler.  It's incomplete atm.  

Spoiler

params ["_obj"];

_fnc_randomBuildingPos =
{
	params ["_pos","_scanRad"];
	
	_buildings = nearestObjects [_obj,["House","Building"],_scanRad];
	_building = selectRandom _buildings;
	_bPos = getPos _building
	
	_bPos
};
	
_fnc_randomRoadPos =
{
	params ["_pos","_scanRad"];
	
	_roads = _pos nearRoads _scanRad;
	_road = selectRandom _road;
	_rPos = getPosASL _road;
	
	_rPos
};



_bPos1 = [_obj,50] call _fnc_randomBuildingPos;
_bPos2 = [_bPos,75] call _fnc_randomBuildingPos;
_bPos3 = [_bPos1,75] call _fnc_randomBuildingPos;

_rPos1 = [_bPos1,50] call _fnc_randomRoadPos;
_rPos2 = [_bPos2,50] call _fnc_randomRoadPos;
_rPos3 = [_bPos3,50] call _fnc_randomRoadPos;

_a = [_bPos1,_rPos1];
_b = [_bPos2,_rPos2];
_c = [_bPos3,_rPos3];

_sectorArray = [_a,_b,_c];
//how do I create the groups for each index in the array and then name them uniquely

 

 

Share this post


Link to post
Share on other sites

Something like this?

{
	private _sector = _x;
	private _groupName = format["grp-sctr-%1", _forEachIndex];
	{
		private _grp = createGroup [east, true];
		//spawn units etc.
		_grp setGroupId (format ["%1-%2", _groupName, _forEachIndex];);
	} forEach _sector;
} forEach _sectorArray ;

Untested

  • Like 1

Share this post


Link to post
Share on other sites

I'm just spawning them in the BI function so,

private _grp = createGroup [east, true];
		//spawn units etc.
private _grp = _params call BIS_fnc_spawnGroup?

could I use missionNameSpace as well? I'm trying to figure how I'd reference each group separately since one is going to be defending and one is patrolling I can't call the defend/patrol functions inside the same loop can I?

Share this post


Link to post
Share on other sites

Either of these should do the trick as long as you're only using 2 groups per sector:

Spoiler

 


{
	private _sector = _x;
	private _groupName = format["grp-sctr-%1", _forEachIndex];

	{
		private _grp = [...] call BIS_fnc_spawnGroup;
      	private _pos = _x;
		
		if (_forEachIndex == 0) then {
			_grp setGroupId [format ["%1-building", _groupName]];
			//do code for building group
		};
		if (_forEachIndex == 1) then {
			_grp setGroupId [format ["%1-road", _groupName]];
			//do code for road group
		};
	} forEach _sector;
} forEach _sectorArray;


/////////// OR ////////////////////

{
	private _sector = _x;
	private _groupName = format["grp-sctr-%1", _forEachIndex];

  	private _pos = _sector select 0;
  	private _bldGrp = [...] call BIS_fnc_spawnGroup;
	_bldGrp setGroupId [format ["%1-building", _groupName]];
  	//do code for building group
  
  	private _pos = _sector select 1;
	private _roadGrp = [...] call BIS_fnc_spawnGroup;
    _roadGrp setGroupId [format ["%1-road", _groupName]];
    //do code for road group
		
} forEach _sectorArray;

Untested

Share this post


Link to post
Share on other sites

The question is: what for?

You don't have to name your groups. The engine does.

If you want to apply some codes, just use the local variable after you spawned it. (depending on how do you spawn them, but, for instance, the BIS_fnc_spawnGroup returns the group). So:

_yourTempGrp = [...] call bis_fnc_spawnGroup;

_yourTempGrp call your_defend_function;

 

 

Your problem remains unclear without any hint about how do you spawn and what do you write. You start with a supposed solution which is probably not the best way. Are you using BI modules?

 

Share this post


Link to post
Share on other sites

So this is what I'm using right now and it seems to be working fine.

@pierrmgi

Its because if I am trying to be less redundant so I don 't have to type the params for the spawnGroup function repeatedly.  

Spoiler

_sectorArray = [_a,_b,_c];
{
	private _sector = _x;
	{
		private _grp = [_x,east,(configfile >> "CfgGroups" >> "East" >> "CUP_O_RU" >> "Infantry_Ratnik_Winter" >> "InfSentry")] call BIS_fnc_spawnGroup;
		for "_i" from 1 to (count _sector - 1) do
		{
			missionNameSpace setVariable [format["grp%1-sec%2",_i,_sector],_grp];
		};
		
		defendGrp = missionNameSpace getVariable [format["grp%1",1],_grp];
		patrolGrp = missionNameSpace getVariable [format["grp%1",2],_grp];
		
		[defendGrp,defendGrp,50,3,.2,.1] call CBA_fnc_taskDefend;
		[patrolGrp,leader patrolGrp,100,3,[],true] call lambs_wp_fnc_taskPatrol;
		
	}forEach _sector;
	
}forEach _sectorArray;

 

Again, thanks for the advice/tips

  • Confused 1

Share this post


Link to post
Share on other sites

Your defendGrp and patrolGrp are useless. Totally useless. So your variables are.

More than that, your sectors are arrays, if I'm right (made of positions like [_bPos1,_rPos1]) so that can't work as positions (single [x,y,z] array)


 

_sectorArray = [_a,_b,_c];  // made of [[_bPos1,_rPos1],[_bPos2,_rPos2],[_bPos3,_rPos3]]; why not
{
  private _sectorDefend = _x #0;
  private _sectorPatrol = _x #1;
  private _grp = [ _sectorDefend ,east,(configfile >> "CfgGroups" >> "East" >> "CUP_O_RU" >> "Infantry_Ratnik_Winter" >> "InfSentry")] call BIS_fnc_spawnGroup;
  [_grp,_grp,50,3,.2,.1] call CBA_fnc_taskDefend;  // I'm not using CBA, check id you need the group param twice (as you wrote)
  private _grp = [ _sectorPatrol ,east,(configfile >> "CfgGroups" >> "East" >> "CUP_O_RU" >> "Infantry_Ratnik_Winter" >> "InfSentry")] call BIS_fnc_spawnGroup; // another identical group but not the same pos
  [_grp,leader _grp,100,3,[],true] call lambs_wp_fnc_taskPatrol; // check also the parameters
} forEach _sectorArray;

 

What else?

  • Like 1

Share this post


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

private _sectorDefend = _x #0; private _sectorPatrol = _x #1;

never seen hash marks used with the _x variable.  What does that do? Nevermind, after looking it up I see.  Just a lot of new scripting commands that I'm becoming aware of.  Thanks Pierre

 

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

×