Jump to content
BlacKnightBK

accessing all units in an array

Recommended Posts

Hello everyone,

 

I hope none of you is annoyed by the increased number of questions i have asked lately.

So i managed to spawn the units I want nicely the way they should, so now the next step for me is their gear.

Before I give them the gear I want them to have I am pretty sure i need to remove what they already have.

 

Basically i managed to get that done for each unit separately the following way:

_unit1 = _recon0 createunit ["O_T_Recon_M_F",_leaderpos,[], 1000, "Can_COLLIDE"];
removeallweapons _unit1;
removevest _unit1;
removeuniform _unit1;
removeheadgear _unit1;
removebackpack _unit1;
_unit1 unassignItem "NVGoggles";
_unit1 removeItem "NVGoggles";
removeallitems _unit1;

That is just the code for a squad member and not the whole unit obviously.

Now when i test the code the whole squad spawns and they go over to some random location and stand there naked with their leader (since i still did not yet specify they need to patrol)

 

I have two problems however, one is the night vision goggles are not getting removed. however that is not the big issue.

 

My big issue is when i try to remove all the units gear using one loop instead of each unit separately for efficiency it is not working.

{
    removeallweapons _this;
    removeuniform _this;
    removeheadgear _this;
    removebackpack _this;
    _this unassignItem "NVGoggles";
    _this removeItem "NVGoggles";
    removeallitems _this;
    
    }foreach _reconGroups; 

"_reconGroups" is an array which contains 7 other groups. 

I defined it earlier as follows

_reconGroups = ["_recon0","_recon1","_recon2","_recon3"];

Each group is defined as follows:

_recon0 = creategroup east;
_recon1 = creategroup east;
_recon2 = creategroup east;
_recon3 = creategroup east;

I did not use "allUnits" instead of "_reconGroups" because i am afraid it would do that to players as well.

 

Also another error i get when i test the code is with the _this, where it tells me that an array is expected.

 

Thanks a lot in advance

Share this post


Link to post
Share on other sites

To remove NV and other assigned items you can use removeAllAssignedItems command, because factions can have different NVG classes. All available NVGs here Arma_3_CfgWeapons_Items#NVGoggles.

Second. As described here,  when you iterate over array items are represented by the magic variable _x

Next. Iteration over array of groups gives group for each element of array. Who you applying to group commands applicable only to units?

 

Scripting commands manual  with detailed description of input, output and effects of each command makes it possible to spend 5 minutes to save hours of your time and some forum posts.

 

Cheers

 

 

 

 

 

 

  • Like 1

Share this post


Link to post
Share on other sites
1 hour ago, serena said:

To remove NV and other assigned items you can use removeAllAssignedItems command, because factions can have different NVG classes. All available NVGs here Arma_3_CfgWeapons_Items#NVGoggles.

Second. As described here,  when you iterate over array items are represented by the magic variable _x

Next. Iteration over array of groups gives group for each element of array. Who you applying to group commands applicable only to units?

 

Scripting commands manual  with detailed description of input, output and effects of each command makes it possible to spend 5 minutes to save hours of your time and some forum posts.

 

Cheers

 

 

 

 

 

 

Thanks alot cheers. and trust me i do use the manual. and it did save me hours in other stuff but seems to me like some functions are out dated and i have to use them a different way, however, sometimes i am too much of an idiot to understand them

Share this post


Link to post
Share on other sites

Can you give specific examples of "outdated functions" for which documentation does not match the function behavior?

Share this post


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

 

Scripting commands manual  with detailed description of input, output and effects of each command makes it possible to spend 5 minutes to save hours of your time and some forum posts.

 

But in my experience, being a crap coder, until someone has learned by heart the >2000 pages referenced :ghostface:, they should pay at least as much attention to the pages:

 

Scripting Commands by Functionality - which I am finding very useful - and

Functions by Functionality - because there one can find ready-made solutions for a great variety of tasks.

Share this post


Link to post
Share on other sites
1 hour ago, serena said:

Can you give specific examples of "outdated functions" for which documentation does not match the function behavior?

I do not remember them, but a lot of functions i used them as they have been explained exactly and i still get errors of what is expected in a specific location.

 

However do not take my word for it, i am most probably wrong since I am still new to this.

Share this post


Link to post
Share on other sites

 

 

//reconGroup array
_reconGroup = [];
_reconGroup resize 4;
//create units array
_units = [];
_units resize 4;
{
  //create temporary east group for each iteration of this loop
  _tmpGrp = createGroup east;
	
//change position to wherever you need them, _x is a dude being added to the _tmpGrp (leader)
  _x = "O_T_Recon_M_F" createunit [position [0,0,0], _tmpGrp];
  removeallweapons _x;
  removevest _x;
  removeuniform _x;
  removeheadgear _x;
  removebackpack _x;
  _x unassignItem "NVGoggles";
  _x removeItem "NVGoggles";
  removeallitems _x;
  //apply the _tmpGrp into your reconGroup array at the same index position of you units loop program.
  //both arrays are equal, so the indexes line up perfect
  _reconGroup set [ _forEachIndex, _tmpGrp ];
//if that don't work try _reconGroup set [ _forEachIndex, [_tmpGrp] ];
}forEach _units;

This program is pretty straight forward.  I create two local arrays, _units and _reconGroup.  I set both size 4, as in 4 indexes.  I then loop through the _units array with the command {}forEach _units;  Since there are 4 indexes, the loop will cycle 4 times and then exit, so I can create 4 units with the magic variable _x.  Also, assigning magic variable _x to equal a unit makes that _units index equal to that dude, so the _units array could technically be called again if the namespace for the local variable doesn't disappear from your program.  I also make use of the _units forEach loop and assign a temporary east group to add the newly created unit to.  Since nobody will be in that group, those new dudes will be leader.  At the end of the script, I set those temporary groups to be indexed into the _reconGroup array.  So the leaders can be found in Leader _reconGroup select # later in your program. 

Share this post


Link to post
Share on other sites
1 hour ago, stuguy said:

 

 


//reconGroup array
_reconGroup = [];
_reconGroup resize 4;
//create units array
_units = [];
_units resize 4;
{
  //create temporary east group for each iteration of this loop
  _tmpGrp = createGroup east;
	
//change position to wherever you need them, _x is a dude being added to the _tmpGrp (leader)
  _x = "O_T_Recon_M_F" createunit [position [0,0,0], _tmpGrp];
  removeallweapons _x;
  removevest _x;
  removeuniform _x;
  removeheadgear _x;
  removebackpack _x;
  _x unassignItem "NVGoggles";
  _x removeItem "NVGoggles";
  removeallitems _x;
  //apply the _tmpGrp into your reconGroup array at the same index position of you units loop program.
  //both arrays are equal, so the indexes line up perfect
  _reconGroup set [ _forEachIndex, _tmpGrp ];
//if that don't work try _reconGroup set [ _forEachIndex, [_tmpGrp] ];
}forEach _units;

This program is pretty straight forward.  I create two local arrays, _units and _reconGroup.  I set both size 4, as in 4 indexes.  I then loop through the _units array with the command {}forEach _units;  Since there are 4 indexes, the loop will cycle 4 times and then exit, so I can create 4 units with the magic variable _x.  Also, assigning magic variable _x to equal a unit makes that _units index equal to that dude, so the _units array could technically be called again if the namespace for the local variable doesn't disappear from your program.  I also make use of the _units forEach loop and assign a temporary east group to add the newly created unit to.  Since nobody will be in that group, those new dudes will be leader.  At the end of the script, I set those temporary groups to be indexed into the _reconGroup array.  So the leaders can be found in Leader _reconGroup select # later in your program. 

Thanks a lot bro, i did fix my script and it is working, also yours is great however it wouldn't work for me since the 4 units I have in each group are of 4 different classes.


However what i would love to know is how to add all the units I created in my file to a single array so i can easily edit their gear in one loop instead of using a loop for each squad.

 

I am grouping all the groups I am creating this way:

private _groups = ["_reconGroups", "_sniperTeams"];

And the squads this way:

private _reconGroups = [];
_reconGroups = ["_recon0","_recon1","_recon2","_recon3"];
private _sniperTeams = [];
_sniperTeams = ["_sniper0","_sniper1","_sniper2"];

I do not want to be using a allUnits command since i am pretty sure it will add all units in the server into the array and not just the once created for this mission. Other than that, the only way i see i can do it is add the units myself into an array but would be very tiring I am sure and redundant.

 

Thanks
 

 

Share this post


Link to post
Share on other sites
{ 
	//code _x is the unit 
}forEach units groupNameVariable;

edit units that you already defined in a group ^

 

The code I already gave you can assign new units to new groups or be tweaked to add troops to existing groups.

Share this post


Link to post
Share on other sites
Just now, stuguy said:

{ //code _x is the unit }forEach units groupNameVariable;

 

I totally do not get it

Sorry

Share this post


Link to post
Share on other sites

forEach is a type of for loop that iterates, or counts, through an array.  units is an arma command that returns a list of units in a group that you specify.  groupNameVariable will need to be changed to be a group variable name that you made, like _recon0 or _sniper1.  So the forEach statement that I supplied you will count through a list of units belonging to a group you give it.  The group you give it is just a list of men in it, like an excel spread sheet with a roster of people in it going down a column or row.  Each iteration of the loop goes through one space of that list at a time, or "index" as we call it.  It goes in order, or in an order you specify with more advanced commands.  That index's contents is represented inside the braces by the magic variable _x.  The index number is represented by magic variable _forEachIndex.  The forEach loop begins by default at index 0, then goes to 1, 2 ,3 etc.  Whatever code you put in the braces gets applied to each unit in that group.  So if you put inventory commands from the virtual arsenal, that loadout gets applied to every unit in the group you write in for the groupNameVariable.

Share this post


Link to post
Share on other sites
4 minutes ago, stuguy said:

forEach is a type of for loop that iterates, or counts, through an array.  units is an arma command that returns a list of units in a group that you specify.  groupNameVariable will need to be changed to be a group variable name that you made, like _recon0 or _sniper1.  So the forEach statement that I supplied you will count through a list of units belonging to a group you give it.  The group you give it is just a list of men in it, like an excel spread sheet with a roster of people in it going down a column or row.  Each iteration of the loop goes through one space of that list at a time, or "index" as we call it.  It goes in order, or in an order you specify with more advanced commands.  That index is represented inside the braces by the magic variable _x.  The forEach loop begins by default at index 0, then goes to 1, 2 ,3 etc.  Whatever code you put in the braces gets applied to each unit in that group.  So if you put inventory commands from the virtual arsenal, that loadout gets applied to every unit in the group you write in for the groupNameVariable.

 

okay  i get what you mean now, however this is the way I am doing and I don't like it.

Since I have 4 recon squads means i have to make 4 loops which is not efficient and very redundant.

Is there a way to check the units in _reconGroups instead of _recon 0??

 

And thank you for your time stuguy, really appreciate it bro :)

Share this post


Link to post
Share on other sites

So you are serving multidimensional arrays.  You have to iterate the column A, B, C, and D on your excel sheet.  Each column has a list of names going down it.  You are thinking, "I need 4 loops to handle this".  Wrong.  Two at most.   One forEach loop to cycle through your 4 groups, and one more forEach loop inside of the first loop to cycle through the list of men. 

 

{
	{

		//do stuff
		//_x is once again the unit from the group supplied by the parent forEach.
	
	}forEach _x;
	//this _x is the magic variable for the group from forEach _groupArray
}forEach _groupArray;

This loop is like cycling through A-1,2,3,4etc, then B-1,2,3,4etc, and so on

Share this post


Link to post
Share on other sites
3 minutes ago, stuguy said:

So you are serving multidimensional arrays.  You have to iterate the column A, B, C, and D on your excel sheet.  Each column has a list of names going down it.  You are thinking, "I need 4 loops to handle this".  Wrong.  Two at most.   One forEach loop to cycle through your 4 groups, and one more forEach loop inside of the first loop to cycle through the list of men. 

 


{
	{

		//do stuff
		//_x is once again the unit from the group supplied by the parent forEach.
	
	}forEach _x;
	//this _x is the magic variable for the group from forEach _groupArray
}forEach _groupArray;

 

Hmm, I will try that again then with a little twist you just gave me right now. Thanks

Share this post


Link to post
Share on other sites

just be sure to change "_groupArray" to be the list of groups you made.  Just one more query, are your groups empty or do they have a unit or units in them already?

Share this post


Link to post
Share on other sites
Just now, stuguy said:

just be sure to change "_groupArray" to be the list of groups you made.  Just one more query, are your groups empty or do they have a unit or units in them already?

Every group I make i fill it with the units i need immediately in the lines after

Share this post


Link to post
Share on other sites

I can teach you to do it with loops...  Especially if all of your groups have the same loadouts.  Hell, I can even show you how to make different load outs and still apply them to squad leader, medic, marksman, at soldier, mg, etc.

Share this post


Link to post
Share on other sites
Just now, stuguy said:

I can teach you to do it with loops...  Especially if all of your groups have the same loadouts.

Cool :)

Sorry all my previous programming exeprience was just the classes i took in introduction to c++. So pretty much a noob here.

 

and yes, the way i am doing it is every Recon group will have 4 members in it (leader, scout, marksman and medic).

Every recon leader will have the same gear as every other recon Leader and so on with the others

Share this post


Link to post
Share on other sites
{
	_grp = _x
	_grp = createGroup east;
	{
		switch(_forEachIndex) do {
			case 0 : {};
			case 1 : {};
			case 2 : {};
			case 3 : {};
			case 4 : {};
			case 5 : {};
			default : {};
		};
	}forEach _x;
	_x set [_forEachIndex, _grp];
}forEach _groupArray;

 

Share this post


Link to post
Share on other sites

switch do statement will allow you to set the type of unit class or even loadouts for that index of unit.

Share this post


Link to post
Share on other sites
7 minutes ago, stuguy said:

{
	_grp = _x
	_grp = createGroup east;
	{
		switch(_forEachIndex) do {
			case 0 : {};
			case 1 : {};
			case 2 : {};
			case 3 : {};
			case 4 : {};
			case 5 : {};
			default : {};
		};
	}forEach _x;
	_x set [_forEachIndex, _grp];
}forEach _groupArray;

 

Thanks, so all i need to do is fill in the cases??

Also how does it know there are 4 units it needs to create, i dont see anything in the code that tells it needs to alternate 4 times

Share this post


Link to post
Share on other sites
{
	_grp = _x
	_grp = createGroup east;
	{
		switch(_forEachIndex) do {
			case 0 : {_x = "O_T_Recon_M_F" createunit [position [0,0,0], _grp];
					removeallweapons _x;
					removevest _x;
					removeuniform _x;
					removeheadgear _x;
					removebackpack _x;
					_x unassignItem "NVGoggles";
					_x removeItem "NVGoggles";
					removeallitems _x;
					//add gear commands
					};
			case 1 : {
						//repeat above case for next type of unit
					};
			case 2 : {
						//rinse repeat but different
					};
			case 3 : {
						//again here
					};
		};
	}forEach _x;
	_x set [_forEachIndex, _grp];
}forEach _reconGroups;

 

2 minutes ago, BlacKnightBK said:

Thanks, so all i need to do is fill in the cases??

yes

 

2 minutes ago, BlacKnightBK said:

Also how does it know there are 4 units it needs to create, i dont see anything in the code that tells it needs to alternate 4 times

the switch statement's condition is the numerical index of the current iterated index for the forEachLoop it is executed in.  The case # fires off if it is equal to the current index.

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

×