Jump to content
Sign in to follow this  
zuff

forEach on multiple units of multiple groups?

Recommended Posts

I've searched for this but have found nothing.

I want to give 4 different groups' units loadouts, here's how i expected it to work:

{
           removeAllAssignedItems _x;
           removeAllWeapons _x;
           removeAllContainers _x;
           _x addVest "V_BandollierB_khk"; 
           _x addMagazines ["30Rnd_65x39_caseless_green", 3];
           _x addWeapon "arifle_Khaybar_F";
           _s = ((round (random 1)) + 1);
           if (_s == 2) then {
               _x addWeapon "ItemMap";
               hint "Map Given";
           };
       } forEach units [pGroup1,pGroup2,pGroup3,pGroup4]; 

But of course it returns the array of units of the group, not each individual.

so I tried this:

{
           removeAllAssignedItems _x;
           removeAllWeapons _x;
           removeAllContainers _x;
           _x addVest "V_BandollierB_khk"; 
           _x addMagazines ["30Rnd_65x39_caseless_green", 3];
           _x addWeapon "arifle_Khaybar_F";
           _s = ((round (random 1)) + 1);
           if (_s == 2) then {
               _x addWeapon "ItemMap";
               hint "Map Given";
           };
       } forEach [units pGroup1,units pGroup2,units pGroup3,units pGroup4]; 

But that gave me the same result. What am I doing wrong?

Share this post


Link to post
Share on other sites

{

{

...your code

} forEach units _x;

} forEach [group1, group2....];

or you can just

_myallunits = units group1 + units group2 + ....

{

...your code

} forEach _myallunits;

Share this post


Link to post
Share on other sites
{

{

...your code

} forEach units _x;

} forEach [group1, group2....];

or you can just

_myallunits = units group1 + units group2 + ....

{

...your code

} forEach _myallunits;

Brilliant! Can't believe I didn't consider a forEach inside a forEach! you rock.

EDIT:

Say I wanted to give UPSMON to the leader of each group, like this:

        {
           {
               [_x, "choseMark3", "showmarker", "track"] execVM "scripts\upsmon.sqf"   ;        
           } forEach leader _x;
       } forEach [pGroup1, pGroup2, pGroup3, pGroup4];

But I'm getting this error:

Error in expression <ecVM "scripts\upsmon.sqf"   ;        
} forEach leader _x;
} forEach [pGroup1, p>
 Error position: <forEach leader _x;
} forEach [pGroup1, p>
 Error foreach: Type Object, expected Array

What am I doing wrong?

EDIT: Fixed it with this:

        {
           [leader _x, "choseMark3", "showmarker", "track"] execVM "scripts\upsmon.sqf"   ;        
       } forEach [pGroup1, pGroup2, pGroup3, pGroup4];

Edited by zuff

Share this post


Link to post
Share on other sites

Sorry to double post but I have another quick question:

Say I want to create multiple groups using BIS_fnc_spawnGroup using forEach, like so:

{
   _x = [patrolPosition, EAST, (configfile >> "CfgGroups" >> "East" >> "OPF_F" >> "infantry" >> "OIA_InfTeam"),[],[],[0.25,0.4], [0.1,0.1]] call BIS_fnc_spawnGroup;
} forEach [pGroup1, pGroup2, pGroup3, pGroup4];

Why doesn't this work? It's giving me "undefined variable" error for pGroup1.

Share this post


Link to post
Share on other sites
Sorry to double post but I have another quick question:

Say I want to create multiple groups using BIS_fnc_spawnGroup using forEach, like so:

{
   _x = [patrolPosition, EAST, (configfile >> "CfgGroups" >> "East" >> "OPF_F" >> "infantry" >> "OIA_InfTeam"),[],[],[0.25,0.4], [0.1,0.1]] call BIS_fnc_spawnGroup;
} forEach [pGroup1, pGroup2, pGroup3, pGroup4];

Why doesn't this work? It's giving me "undefined variable" error for pGroup1.

As you are passing in nothing, there is no such thing as pGroup1 etc.

Placing pGroup1 = grpNull; for each variable before the forEach loop will make it work but (guessing here) that you then want to be able to reference each group by using the variable pGroup#. This will not work either e.g

pGroup1 = grpNull;
pGroup2 = grpNull;
pGroup3 = grpNull;
pGroup4 = grpNull;

{
   _x = [patrolPosition, EAST, (configfile >> "CfgGroups" >> "East" >> "OPF_F" >> "infantry" >> "OIA_InfTeam"),[],[],[0.25,0.4], [0.1,0.1]] call BIS_fnc_spawnGroup;
} forEach [pGroup1, pGroup2, pGroup3, pGroup4];
hint str units pGroup1

Will result in an empty array in the hint.

Its basically saying grpNull = [patrolPosition blah blah

You will need to compile your statement passing in a string as the variable name e.g

{
call compile format ["%1 = [patrolPosition, EAST, (configfile >> ""CfgGroups"" >> ""East"" >> ""OPF_F"" >> ""infantry"" >> ""OIA_InfTeam""),[],[],[0.25,0.4], [0.1,0.1]] call BIS_fnc_spawnGroup;",_x]
} forEach ["pGroup1","pGroup2", "pGroup3", "pGroup4"];

hint str units pGroup1 will now give you as you expect, a list of units.

Take notice of the above double quotes everywhere due to a string in a string.

From here on you can use your variables as per the rest of the examples in this thread.

You could even include everything youve discussed in this thread into one compile. e.g

{
call compile
format ["
%1 = [patrolPosition, EAST, (configfile >> ""CfgGroups"" >> ""East"" >> ""OPF_F"" >> ""infantry"" >> ""OIA_InfTeam""),[],[],[0.25,0.4], [0.1,0.1]] call BIS_fnc_spawnGroup;
{
	removeAllAssignedItems _x;
	removeAllWeapons _x;
	removeAllContainers _x;
	_x addVest ""V_BandollierB_khk"";
	_x addMagazines [""30Rnd_65x39_caseless_green"", 3];
	_x addWeapon ""arifle_Khaybar_F"";
	_s = ((round (random 1)) + 1);
	if (_s == 2) then {
		_x addWeapon ""ItemMap"";
	};
} forEach units %1;
[leader %1, ""choseMark3"", ""showmarker"", ""track""] execVM ""scripts\upsmon.sqf""",_x];
} forEach ["pGroup1","pGroup2","pGroup3","pGroup4"];

Although that maybe a bit of an overkill and not very nice to edit if you want to change something down the road.

EDIT: Maybe like this instead to make it cleaner. Returning the currentGroup out of the call compile

{
_currentGroup = [] call compile format
["%1 = [patrolPosition, EAST, (configfile >> ""CfgGroups"" >> ""East"" >> ""OPF_F"" >> ""infantry"" >> ""OIA_InfTeam""),[],[],[0.25,0.4], [0.1,0.1]] call BIS_fnc_spawnGroup;
		%1",_x];
{
	removeAllAssignedItems _x;
	removeAllWeapons _x;
	removeAllContainers _x;
	_x addVest "V_BandollierB_khk";
	_x addMagazines ["30Rnd_65x39_caseless_green", 3];
	_x addWeapon "arifle_Khaybar_F";
	_s = ((round (random 1)) + 1);
	if (_s == 2) then {
		_x addWeapon "ItemMap";
	};
} forEach units _currentGroup;
[leader _currentGroup, "choseMark3", "showmarker", "track"] execVM "scripts\upsmon.sqf";
} forEach ["pGroup1","pGroup2","pGroup3","pGroup4"];

Anyway i digress and i am now just thinking out aloud to myself :D.

Edited by Larrow

Share this post


Link to post
Share on other sites

Nice Larrow! It works great!

Could you explain your last example? I don't understand how that return works but It works flawlessly in my mission! Thanks so much.

Share this post


Link to post
Share on other sites

Example showing compile line broken down using first passed string of "pGroup1". (Ive replaced the arguments for BIS_fnc_spawnGroup just to make it easier to read)

Line as it is in the script just shortened arguments to make it readable

_currentGroup = [] call compile format ["%1 = [arguments] call BIS_fnc_spawnGroup; %1",_x];

_x becomes "pGroup1" our passed in forEach value

_currentGroup = [] call compile format ["%1 = [arguments] call BIS_fnc_spawnGroup; %1", [color="#FF0000"]"pGroup1"[/color]];

All the %1's get formatted to pGroup1

_currentGroup = [] call compile "[color="#FF0000"]pGroup1[/color] = [arguments] call BIS_fnc_spawnGroup; [color="#FF0000"]pGroup1[/color]"

String gets compiled. Looking more like an inline function now

_currentGroup = [] call {pGroup1 = [arguments] call BIS_fnc_spawnGroup; pGroup1}

As per any function the last variable reference without closing the line with a ; is the value returned.

So our inline function is basically looking like this

pGroup1 = [arguments] call BIS_fnc_spawnGroup;
pGroup1

pGroup1 gets returned and assigned to _currentGroup

Edited by Larrow

Share this post


Link to post
Share on other sites

Oh ok! Makes perfect sense now, thanks for breaking it down for me.

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  

×