Nearest marker from player

Recommended Posts

Hey Everybody, i need some help to start from scratch my little script.

But, i need to find the nearest marker from 400m, from my player.  Not a radius of 400m from my player.

There is this

nearestObjects [player, ["house"], 200];

But it's just for object...

So this

// Find the nearest marker from player

_nearestMarker = [allMapMarkers, player] call BIS_fnc_nearestPosition;

But there no interval of distance...

Thank you in advance !!! 😉

Share on other sites

@Sayker, something like this:

```private ["_distance", "_distanceMin", "_marker"];

_marker = "";

_distanceMin = worldSize;

{
_distance = (getMarkerPos _x) distance2D player;

if ((_distance >= 400) and { _distance < _distanceMin }) then {
_distanceMin = _distance;
_marker = _x;
};
} forEach allMapMarkers;```

?

Share on other sites

Hiii Schatten !!

THX for your return ! 😉

Actually, i tried to test in game with my consol but doesn't work no values are find.

We need maybe at first the position of the player ?

Except that I'm looking for a marker 😋

Share on other sites

// returns all markers in an array [distance < 400 from player (to be sorted true/false), the distance itself, the marker]

private _mkrs = allMapMarkers apply {[str(player distance2D getMarkerPos _x < 400),player distance2D getMarkerPos _x,_x]};

// the array is sorted: first by string "false" before "true" (alphabetical order), then distance (closest first), then name of marker but that doesn't matter here)

_mkrs sort true;

_nearestMkrAt400Min = _mkrs #0#2;

Note: you can add a max distance in the first element of the array (to be checked true/false as well).

Share on other sites

Hi @pierremgi thank you for you answer,

I can't seem to get it to work. Sorry i'm not a expert.... 🤪

But with your script it's just for marker <400m ?

or i need  400m <" mymarker" < 5000m

THX =)))

Share on other sites
56 minutes ago, Sayker said:

Actually, i tried to test in game with my consol but doesn't work no values are find.

﻿ We need maybe at first the position of the player ?

distance2D can accept objects, so you don't need to get position in advance.

How did you test? If you just copy and paste the code into console, then it won't work. Instead you should wrap it:

```call {
private ["_distance", "_distanceMin", "_marker"];

_marker = "";

_distanceMin = worldSize;

{
_distance = (getMarkerPos _x) distance2D player;

if ((_distance >= 400) and { _distance < _distanceMin }) then {
_distanceMin = _distance;
_marker = _x;
};
} forEach allMapMarkers;

_marker
};```

Share on other sites

Ohhhh Wouaaa !!!

Schatten Thank you so much that works perfectly ! 😄

But i have a last problem that I hadn't thought of...

Another condition who is the color of the marker is  ColorOPFOR

Do you think it's possible with : getMarkerColor

Because I have to find that of the enemy and not that of my allies ahaha 😁

Share on other sites
17 hours ago, Sayker said:

But i have a last problem that I hadn't thought of...

Another condition who is the color of the marker is  ColorOPFOR

﻿﻿ Do you think it's possible with : getMarkerColor

Sure,

```call {
private ["_distance", "_distanceMin", "_marker"];

_marker = "";

_distanceMin = worldSize;

{
if (((getMarkerColor _x) == "ColorOPFOR") and {
_distance = (getMarkerPos _x) distance2D player;

(_distance >= 400) and { _distance < _distanceMin }
}) then {
_distanceMin = _distance;
_marker = _x;
};
} forEach allMapMarkers;

_marker
};```

Share on other sites
1 hour ago, Sayker said:

Hi @pierremgi thank you for you answer,

I can't seem to get it to work. Sorry i'm not a expert.... 🤪

But with your script it's just for marker <400m ?

or i need  400m <" mymarker" < 5000m

THX =)))

That works. It's tested and it's simple.

The nearest marker from player at at least 400 m and within 5000 m, and colored  Opfor, can be found like this:

```call {
private _mkrs = allMapMarkers apply {[str(player distance2D getMarkerPos _x < 400 or player distance2D getmarkerPos _x > 5000 or markerColor _x != "colorEAST"),player distance2D getMarkerPos _x,_x]};
_mkrs sort true;
private _nearestMarker = _mkrs #0#2;
player setpos getMarkerPos _nearestMarker
};```

Share on other sites

@Schatten

Mhhhhmm !!  doesn't Works while your last script works. 🤔

That return : " ";     Have You a idea ?

@pierremgi

Perfect it works !!! 😍  i just change the last line in order to don't  teleport the player on the position, but just get the position of this marker ;))
----> unfortunately there is some problem .... :((   always the same markers are found

call {
private _mkrs = allMapMarkers apply {[str(player distance2D getMarkerPos _x < 1000 or player distance2D getmarkerPos _x > 5000 or markerColor _x != "colorEAST"),player distance2D getMarkerPos _x,_x]};
_mkrs sort true;
private _nearestMarker = _mkrs #0#2;
getMarkerPos _nearestMarker
};

Carreful : if these conditions of distance are not okey, it's the first coordonate of marker who is selected 😉

Little question could you explain me this private _nearestMarker = _mkrs #0﻿#2;

Thx 😃

Share on other sites

@Schatten

Maybe the first condition is the marker with distance then the color ?

Share on other sites
2 hours ago, Sayker said:

@Schatten

Mhhhhmm !!  doesn't Works while your last script works. 🤔

That return : " ";     Have You a idea ?

@pierremgi

Perfect it works !!! 😍  i just change the last line in order to don't  teleport the player on the position, but just get the position of this marker ;))
----> unfortunately there is some problem .... :((   always the same markers are found

Little question could you explain me this private _nearestMarker = _mkrs #0﻿#2;

Thx 😃

The fact you always found the same marker... is related to your way to run the code. It's normal to find the same nearest marker if you don't move the player and don't run the code again.

That's the problem with topics in which you don't have a clear view of the problem: the scenario and the context. There is no problem with codes, just problem with the wording of what is needed we can't guess. What, when, where, who (units).

_mkrs is an array made of [ [stringed boolean ("true"/"false"), distance (nbr), marker string], [...], [...]]; so, after sorting, the first element is a sub-array embedding the nearest marker from 300 to 5000 m of player and colored as OPFOR... if it exists.

For example: let's say you have ten markers: "mk1","mk2",...

in which "mk4" and "mk6" are compliant with distances (<5000 & >400 from player), and color (OPFOR).

Once sorted, the array array _mkrs looks like:

[ ["false",450.487,"mk6"], ["false",1280.562,"mk4"], ["true",38.477,"mk8"], ["true",4256.851,"mk1"], ...];

"false" are always before "true" in this case (sorting true alphabetically), but I wrote the contraposition so "false" means "OK" for your conditions. 😊

_nearestMarker = _mkrs #0#2; is same as _mkrs select 0 select 2. So:

>> the first element of the _mkrs array (first sub-array once sorted) (#0)

>>>> and the 3rd element inside this sub-array (the marker itself). (#2)

Anyway, with this method, be aware that the array can return a marker non compliant with your aim, if there is no compliant marker on map. Instead of returning nothing, if there is at least one marker on map, the selection will return an existing marker. There is no filter for rejecting undue marker, just a sorting like this:

- if there are some markers from 300 to 5000 m from player and colored OPFOR, so the first element of the appliance returns "false" (alphabetically prior to "true", these markers will be at the start of the result array (_mkrs). If not (so all markers return "true"), the markers are sorted by distance anyway but not ignored.

- the sorting by distance is fine, no comment on that.

So, to resume, if you want a position as result, and manage the case of non compliant marker at all, you need to modify the last lines like this:

...

```_mkrs sort true;
private _nearestMarkerPos = if ((_mkrs #0#0) == "false") then [{getMarkerPos (_mkrs #0#2)}, {[0,0,0]} ];
_nearestMarkerPos;```

Now, if the condition (distances,color) are met (so, _mkrs #0#0 is "false") then the result is the position of _mkrs #0#2 which is the marker (_x) in _mkrs array. Else, the result is [0,0,0]  If you are unfamiliar with this kind of scripting, have a look here (the whole page is interesting).

You just have to think about when you want to check that...

Share on other sites

Hy !

For @pierremgi

Thank you for all, your help and your explaination very clearly ! 😉

I understand more your way now !!

For @schatten

I found !! 😉  :

```call {
private ["_distance", "_distanceMin", "_marker"];

_marker = "";

_distanceMin = worldSize;

{
if
(((getMarkerColor _x) == "ColorOPFOR") and
{
_distance = (getMarkerPos _x) distance2D player;

(_distance >= 2000) and { _distance < _distanceMin }
}

) then {
_distanceMin = _distance;
_marker = _x;
};
} forEach allMapMarkers;

_marker
};```

Thank you for this awesome help !!! 💪