nearestObjects: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "\[\[([a-zA-Z][a-zA-Z0-9_]+)\]\]([^ ]*)<\/code>" to "$1$2</code>")
m (Fix author)
 
(15 intermediate revisions by 3 users not shown)
Line 18: Line 18:
|gr1= Object Detection
|gr1= Object Detection


|descr= Returns a list of nearest objects of the given types to the given position or object, within the specified distance. If more than one object is found they will be ordered by proximity, the closest one will be first in the array. Alternatively, you can use [[nearObjects]] command, which doesn't sort results.
|descr= Returns a list of nearest objects of the given types to the given position or object, within the specified distance. If more than one object is found they will be ordered by proximity, the closest one will be first in the array. If an object is given as the center and the filter criteria applies to it, it will be the first result in the list. Alternatively, you can use [[nearObjects]] command, which doesn't sort results.
<br><br>
<br><br>
A list of ClassName types (e.g {{hl|Tank}}) can be found in [[:Category:CfgVehicles|CfgVehicles]].
A list of ClassName types (e.g {{hl|Tank}}) can be found in [[:Category:CfgVehicles|CfgVehicles]].
{{Feature | important | This command matches objects using [[isKindOf]] comparison instead of direct class name [[==]] comparison. As a result, if a class type is a parent, the search will return all objects that inherit from the parent as well as parent itself. This command also only finds objects that are '''[[entities]]'''. If you are looking for non-entity objects, see [[nearObjects]], [[nearestObject]].}}
{{Feature | important | This command matches objects using [[isKindOf]] comparison instead of direct class name [[==]] comparison. As a result, if a class type is a parent, the search will return all objects that inherit from the parent as well as parent itself. This command also only finds objects that are '''[[entities]]'''. If you are looking for non-entity objects, see [[nearObjects]], [[nearestObject]].}}
{{Feature | warning | Because of map object streaming, using [[nearestObjects]] or similar object finding commands over long distance from player camera may not find what you are looking for, as some objects might not exist at the time of command execution. Use [[nearestTerrainObjects]] instead. }}


|s1= [[nearestObjects]] [position, types, radius, 2Dmode]
|s1= [[nearestObjects]] [position, types, radius, 2Dmode]


|p1= position:  [[Object]] or [[Array]] in format [[Position#PositionAGL|PositionAGL]] or [[Position#Introduction|Position2D]] - Where to find objects, center position.
|p1= position:  [[Object]] or [[Array]] in format [[Position#PositionAGL|PositionAGL]] or [[Position#Introduction|Position2D]] - where to find objects, center position.


|p2= types: [[Array]] - List of class name of the objects to search for. Use an empty array {{ic|[]}} to search for all classes.
|p2= types: [[Array]] - list of class name of the objects to search for. Use an empty array <sqf inline>[]</sqf> to search for all classes.


|p3= radius: [[Number]] - Range from center position to search for objects.
|p3= radius: [[Number]] - range from center position to search for objects.


|p5= 2Dmode: [[Boolean]] - (Optional, default [[false]]) [[true]] for 2D distance, [[false]] for 3D distance
|p5= 2Dmode: [[Boolean]] - (Optional, default [[false]]) [[true]] for 2D distance, [[false]] for 3D distance
Line 36: Line 38:
|r1= [[Array]] - array of objects
|r1= [[Array]] - array of objects


|x1= <code>nearestObjects [<nowiki/>player, ["Car", "Tank"], 200];</code>
|x1= <sqf>nearestObjects [player, ["Car", "Tank"], 200];</sqf>


|x2= <code>nearestObjects [<nowiki/>player, ["house"], 200];</code>
|x2= <sqf>nearestObjects [player, ["house"], 200];</sqf>


|x3= <code>nearestObjects [<nowiki/>[2716,2949,0], ["Car", "Truck"], 100];</code>
|x3= <sqf>nearestObjects [[2716,2949,0], ["Car", "Truck"], 100];</sqf>


|x4= Return every object in 50 metres radius around player:
|x4= Return every object in 50 metres radius around player:
<code>nearestObjects [<nowiki/>player, [], 50];</code>
<sqf>nearestObjects [player, [], 50];</sqf>


|seealso= [[nearestTerrainObjects]] [[findNearestEnemy]] [[nearestBuilding]] [[nearestObject]] [[nearObjects]] [[nearestLocation]] [[nearEntities]] [[nearTargets]] [[nearSupplies]] [[nearestLocationWithDubbing]] [[nearObjectsReady]] [[nearRoads]] [[nearestMines]]
|seealso= [[nearestTerrainObjects]] [[findNearestEnemy]] [[nearestBuilding]] [[nearestObject]] [[nearObjects]] [[nearestLocation]] [[nearEntities]] [[nearTargets]] [[nearSupplies]] [[nearestLocationWithDubbing]] [[nearObjectsReady]] [[nearRoads]] [[nearestMines]]
Line 52: Line 54:
|timestamp= 20100403162100
|timestamp= 20100403162100
|text= To get a list with alive targets for various situations use this construct:
|text= To get a list with alive targets for various situations use this construct:
<code>_nearestTargets = [[nearestObjects]] [_submunScanPos, ["VBS2_TANK", "TANK"], _scanArea];
<sqf>
_nearestTargets = nearestObjects [_submunScanPos, ["VBS2_TANK", "TANK"], _scanArea];
_validNearestTargets = [];
_validNearestTargets = [];
{ if (alive _x ) then { _validNearestTargets set [(count _validNearestTargets), _x]; }; } [[forEach]] _nearestTargets;</code>
{ if (alive _x ) then { _validNearestTargets set [(count _validNearestTargets), _x]; }; } forEach _nearestTargets;
</sqf>
results in _validNearestTargets being filled with targets == alive .. (you could use other conditions there, of course!)
results in _validNearestTargets being filled with targets == alive .. (you could use other conditions there, of course!)
}}
}}
Line 63: Line 67:
|text= Passing an empty array to define the types will also return objects with no class at all (such as trees, bushes, stones, ...).  
|text= Passing an empty array to define the types will also return objects with no class at all (such as trees, bushes, stones, ...).  
Example:
Example:
<code>_objects = nearestObjects [_position, [], _radius];</code>
<sqf>_objects = nearestObjects [_position, [], _radius];</sqf>
Passing the array ["All"] is not the same and will only return objects that have some sort of class.
Passing the array ["All"] is not the same and will only return objects that have some sort of class.
}}
}}
Line 72: Line 76:
|text= If you want to detect nearby grenades for the position of a unit, for example, using 'nearestObjects' won't work. You have to use
|text= If you want to detect nearby grenades for the position of a unit, for example, using 'nearestObjects' won't work. You have to use
'nearObjects' instead.
'nearObjects' instead.
<code>count (nearestObjects [_unit, ["GrenadeHand"], 30]) // WON'T WORK</code>
<sqf>count (nearestObjects [_unit, ["GrenadeHand"], 30]); // WON'T WORK</sqf>
Won't ever return anything but 0.
Won't ever return anything but 0.
<code>count (_unit nearObjects ["GrenadeHand", 30]) // WORKS!</code>
<sqf>count (_unit nearObjects ["GrenadeHand", 30]); // WORKS!</sqf>
Will work.
Will work.
}}
}}


{{Note
{{Note
|user= AgentRev
|user= AgentRev01
|timestamp= 20140322023800
|timestamp= 20140322023800
|text= The distance from which an object is determined to be inside the radius is calculated from its model center (object modelToWorld [0,0,0]), and not the position returned by getPos/ATL/ASL. If an object is used as the origin from which to scan, distance is calculated from its model center as well.
|text= The distance from which an object is determined to be inside the radius is calculated from its model center (object modelToWorld [0,0,0]), and not the position returned by getPos/ATL/ASL. If an object is used as the origin from which to scan, distance is calculated from its model center as well.
Line 85: Line 89:


{{Note
{{Note
|user= Killzone Kid
|user= Killzone_Kid
|timestamp= 20141026151500
|timestamp= 20141026151500
|text= Return all trees in 100m radius around player:
|text= Return all trees in 100m radius around player:
<code>[[private]] _trees {{=}} [];
<sqf>
private _trees = [];
{
{
[[if]] ([[str]] _x [[find]] ": t_" > -1) [[then]] {
if (str _x find ": t_" > -1) then {
_trees [[pushBack]] _x;
_trees pushBack _x;
};
};
} forEach nearestObjects [<nowiki/>player, [], 100];</code>  
} forEach nearestObjects [player, [], 100];
</sqf>  
}}
}}


Line 103: Line 109:


{{Note
{{Note
|user= Killzone Kid
|user= Killzone_Kid
|timestamp= 20160108121400
|timestamp= 20160108121400
|text= The first call to this command can take significantly longer then consecutive calls. But even after the objects in given radius were cached, the sorting this command performs could be quite expensive. For example to sort ~7000 object it might take up to 100ms. For performance reasons you can use [[nearObjects]] instead, which is basically the same command but without added sorting.
|text= The first call to this command can take significantly longer than consecutive calls. But even after the objects in given radius were cached, the sorting this command performs could be quite expensive. For example to sort ~7000 object it might take up to 100ms. For performance reasons you can use [[nearObjects]] instead, which is basically the same command but without added sorting.
}}
}}


Line 113: Line 119:
|text= In {{arma3}}, nearestObjects is partially broken and is unable to return nearby placed explosive charges or mines when searching by classnames. Use [[nearObjects]], [[nearestObject]] or [[allMines]] instead.<br>
|text= In {{arma3}}, nearestObjects is partially broken and is unable to return nearby placed explosive charges or mines when searching by classnames. Use [[nearObjects]], [[nearestObject]] or [[allMines]] instead.<br>
Example of non-functional code:
Example of non-functional code:
<code>// always returns nothing, even if there are objects that should be returned
<sqf>
nearestObjects [<nowiki/>position player, ["APERSMine_Range_Ammo", "SatchelCharge_Remote_Ammo"], 10];</code>
// always returns nothing, even if there are objects that should be returned
nearestObjects [position player, ["APERSMine_Range_Ammo", "SatchelCharge_Remote_Ammo"], 10];
</sqf>
}}
}}


{{Note
{{Note
|user= Killzone Kid
|user= Killzone_Kid
|timestamp= 20161031003200
|timestamp= 20161031003200
|text= Since {{arma3}} v1.66 the default sorting is done according to 3D distance not [[distance2D]] as before. This is an attempt to correct some very old bug in implementation.
|text= Since {{arma3}} v1.66 the default sorting is done according to 3D distance not [[distance2D]] as before. This is an attempt to correct some very old bug in implementation.
}}
}}

Latest revision as of 23:13, 4 May 2024

Hover & click on the images for description

Description

Description:
Returns a list of nearest objects of the given types to the given position or object, within the specified distance. If more than one object is found they will be ordered by proximity, the closest one will be first in the array. If an object is given as the center and the filter criteria applies to it, it will be the first result in the list. Alternatively, you can use nearObjects command, which doesn't sort results.

A list of ClassName types (e.g Tank) can be found in CfgVehicles.
This command matches objects using isKindOf comparison instead of direct class name == comparison. As a result, if a class type is a parent, the search will return all objects that inherit from the parent as well as parent itself. This command also only finds objects that are entities. If you are looking for non-entity objects, see nearObjects, nearestObject.
Because of map object streaming, using nearestObjects or similar object finding commands over long distance from player camera may not find what you are looking for, as some objects might not exist at the time of command execution. Use nearestTerrainObjects instead.
Groups:
Object Detection

Syntax

Syntax:
nearestObjects [position, types, radius, 2Dmode]
Parameters:
position: Object or Array in format PositionAGL or Position2D - where to find objects, center position.
types: Array - list of class name of the objects to search for. Use an empty array [] to search for all classes.
radius: Number - range from center position to search for objects.
since Arma 3 logo black.png1.66
2Dmode: Boolean - (Optional, default false) true for 2D distance, false for 3D distance
Return Value:
Array - array of objects

Examples

Example 1:
nearestObjects [player, ["Car", "Tank"], 200];
Example 2:
nearestObjects [player, ["house"], 200];
Example 3:
nearestObjects [[2716,2949,0], ["Car", "Truck"], 100];
Example 4:
Return every object in 50 metres radius around player:
nearestObjects [player, [], 50];

Additional Information

See also:
nearestTerrainObjects findNearestEnemy nearestBuilding nearestObject nearObjects nearestLocation nearEntities nearTargets nearSupplies nearestLocationWithDubbing nearObjectsReady nearRoads nearestMines

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note
PhilippRauch - c
Posted on Apr 03, 2010 - 16:21 (UTC)
To get a list with alive targets for various situations use this construct:
_nearestTargets = nearestObjects [_submunScanPos, ["VBS2_TANK", "TANK"], _scanArea]; _validNearestTargets = []; { if (alive _x ) then { _validNearestTargets set [(count _validNearestTargets), _x]; }; } forEach _nearestTargets;
results in _validNearestTargets being filled with targets == alive .. (you could use other conditions there, of course!)
Rübe - c
Posted on Aug 10, 2010 - 02:28 (UTC)
Passing an empty array to define the types will also return objects with no class at all (such as trees, bushes, stones, ...). Example:
_objects = nearestObjects [_position, [], _radius];
Passing the array ["All"] is not the same and will only return objects that have some sort of class.
Katulobotomia - c
Posted on Sep 11, 2013 - 14:54 (UTC)
If you want to detect nearby grenades for the position of a unit, for example, using 'nearestObjects' won't work. You have to use 'nearObjects' instead.
count (nearestObjects [_unit, ["GrenadeHand"], 30]); // WON'T WORK
Won't ever return anything but 0.
count (_unit nearObjects ["GrenadeHand", 30]); // WORKS!
Will work.
AgentRev01 - c
Posted on Mar 22, 2014 - 02:38 (UTC)
The distance from which an object is determined to be inside the radius is calculated from its model center (object modelToWorld [0,0,0]), and not the position returned by getPos/ATL/ASL. If an object is used as the origin from which to scan, distance is calculated from its model center as well.
Killzone_Kid - c
Posted on Oct 26, 2014 - 15:15 (UTC)
Return all trees in 100m radius around player:
private _trees = []; { if (str _x find ": t_" > -1) then { _trees pushBack _x; }; } forEach nearestObjects [player, [], 100];
Tankbuster - c
Posted on Aug 27, 2015 - 16:45 (UTC)
If you use "Man" as the class to look for, it will only find dismounted men. IE, men in vehicles will NOT be found.
Killzone_Kid - c
Posted on Jan 08, 2016 - 12:14 (UTC)
The first call to this command can take significantly longer than consecutive calls. But even after the objects in given radius were cached, the sorting this command performs could be quite expensive. For example to sort ~7000 object it might take up to 100ms. For performance reasons you can use nearObjects instead, which is basically the same command but without added sorting.
Ceeeb - c
Posted on Mar 02, 2016 - 23:09 (UTC)
In Arma 3, nearestObjects is partially broken and is unable to return nearby placed explosive charges or mines when searching by classnames. Use nearObjects, nearestObject or allMines instead.
Example of non-functional code:
// always returns nothing, even if there are objects that should be returned nearestObjects [position player, ["APERSMine_Range_Ammo", "SatchelCharge_Remote_Ammo"], 10];
Killzone_Kid - c
Posted on Oct 31, 2016 - 00:32 (UTC)
Since Arma 3 v1.66 the default sorting is done according to 3D distance not distance2D as before. This is an attempt to correct some very old bug in implementation.