Jump to content
dyrmoon

How I check array in array (foreach in foreach)

Recommended Posts

Hello, I can not find solution for my two question.


How can I control one item (unit) in array with different array (foreach in foreach) ?

Need example, what I mean:

ArrayUi = [Unit1,Unit2,Unit3,Unit4,Unit5];
{
if ( (_x distance ({_x} foreach AllPlayers)) < 100 ) then { _x Move GetPos ??? };
} foreach ArrayUi;

This will be check every Unit, if it closer <100m with all units in Array.
But I need every players to check with each unit in ArrayUi (not whole field).

In example, what should I put on place "???" Unit not know which players to go to (only if I use (select 0) but I do not want to).

Or I try similar thing, but I get error message (offcourse, because _x is not have parametr):

ArrayUi = [Unit1,Unit2,Unit3,Unit4,Unit5];
AllPlayers select { (_x distance ({_x} count ArrayUi > 0)) < 100 };
_x Move GetPos ???

Or use completely a different solution for this situation.

And I can't use "Player", because this command is local.

 

2.
And second, how do I find, which unit is closest with another unit?
For example, which players from Array is closest to another player (or unit).

A little similarly example as on top.

 

Thank you very much for you answer.:thud:

Share this post


Link to post
Share on other sites

Not entirely sure about your first point, could you rephrase that?

Nested forEach can be avoided most of the time, but it works like that:

_stuff = [[1,2,3],[4,5,6],[7,8,9]];

{
	_nested = _x;

	{
		systemchat str _x;
	} forEach _nested;

} forEach _stuff;

To only return units that are closer than 100m to the player do this:

ArrayUi = [Unit1,Unit2,Unit3,Unit4,Unit5];
_closestUnitsToPlayer = ArrayUi select {_x distance player < 100};

 

To sort by distance you could do something like this:

_units = [unit1,unit2,unit3];
_sorted = [_units,[],{player distance _x},"ASCEND"] call BIS_fnc_sortBy;
_sorted params ["_closest"];
systemchat format ["%1 is closest",_closest];

 

Cheers

  • Like 1

Share this post


Link to post
Share on other sites

Thank you for the quick reply.

 

Yes, it's a bit confusing.
I need that each unit in Array (ArrayUi in example) to move to the closest player.

Share this post


Link to post
Share on other sites

I'm doing this:

_NearEntite = [];
_ArrayVehicle = [Car1,Car2,Car3,Car4,Car5];
{
If ( (_x distance ({_x} foreach _ArrayVehicle)) < 1000 ) then {_NearEntite pushBack _x};
} foreach Allplayers;

{
If ( !(_NearEntite isEqualTo []) ) then {
                                        (Driver _x) move position ((_NearEntite) select 0);
                                        {_x move position ((_NearEntite) select 0)} foreach crew _x;
                                       };
} foreach _ArrayVehicle;

Players (Allplayers) is compare to vehicle (_ArrayVehicle).  But I want, that players (AllPlayers) is compare to with each Vehicle (_ArrayVehicle). And they moved to the nearest player, and not use (select 0).
Is this better example to understand what I mean (I edited this) ?

 

And I do not want to use "Player", because this command is local.

Share this post


Link to post
Share on other sites

Confusing. The nearest vehicle of any players, within 1000?

 

_nearCar = objNull;
_nearPlyr = objNull;
_dist = 1000;

{
  _plyr = _x;
  _nearCars = [car1,car2,car3,car4] apply { [_plyr distance2D _x,_x] };
  _nearCars sort True;
  if (_nearCars select 0 select 0 < _dist) then {
    _nearCar = _nearCars select 0 select 1;
    _nearPlyr = _plyr;
    _dist = _nearCars select 0 select 0
  };
} forEach allPlayers;
if (!isnull _nearCar) then {_nearCar domove getpos _nearPlyr};

 

Share this post


Link to post
Share on other sites
12 hours ago, Grumpy Old Man said:

 

To sort by distance you could do something like this:


_units = [unit1,unit2,unit3];
_sorted = [_units,[],{player distance _x},"ASCEND"] call BIS_fnc_sortBy;
_sorted params ["_closest"];
systemchat format ["%1 is closest",_closest];

 

Cheers

 

I had no idea you could use params in that capacity. I had to look it up. Do you dream of SQF at night? Now I can rewrite all of my scripts that find the closest this or that, they are ... inefficient at the moment.

Share this post


Link to post
Share on other sites
15 minutes ago, AZCoder said:

I had no idea you could use params in that capacity. I had to look it up. Do you dream of SQF at night? Now I can rewrite all of my scripts that find the closest this or that, they are ... inefficient at the moment.

Well if you do stuff for a certain amount of time it becomes second nature, goes for pretty much anything.

Just want to point out that there's a dedicated engine command to sort, no need for the BIS sortby function.

 

Cheers

 

 

Share this post


Link to post
Share on other sites

Perhaps, the nearest vehicle for the nearest player?


 

0 = [] spawn {
  _nearEntites = allPlayers;
  {
    _car = _x;
    _sortedEntites = _nearEntites apply {[_car distance2D _x,_x]};
    _sortedEntites sort True;
    if (count _sortedEntites >0) then {
      _nearEntite = _sortedEntites select 0 select 1;
      _nearEntites deleteAt 0;
      _car domove getpos _nearEntite
    };
  } forEach [car1,car2,car3,car4];
};

 

  • Like 1

Share this post


Link to post
Share on other sites

@Grumpy Old Man I've been doing this for 8 years and I can't keep up on the latest improvements. I scan the release change logs but they tend to be huge lately. You and Pierre make it easier to catch up.

 

Share this post


Link to post
Share on other sites
30 minutes ago, AZCoder said:

@Grumpy Old Man I've been doing this for 8 years and I can't keep up on the latest improvements. I scan the release change logs but they tend to be huge lately. You and Pierre make it easier to catch up.

 

Quickest way is usually checking the scripting wiki, you can click on the game version to see what commands are being added by which specific update version. A godsend!

 

Cheers

  • Like 1

Share this post


Link to post
Share on other sites
5 hours ago, pierremgi said:

Confusing. The nearest vehicle of any players, within 1000?

 

4 hours ago, pierremgi said:

Perhaps, the nearest vehicle for the nearest player?

 

Yes, you bouth example is exactly what I was looking for. Thank you very match.

This command (apply) helped me a lot,  I did not know that this exist.

_sortedEntites = _nearEntites apply {[_car distance2D _x,_x]};

And I apologize for the confusing description, but I was already tired.

  • Like 1

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

×