nearestObject: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - ";[ ]+ " to "; ")
m (Some wiki formatting)
Line 26: Line 26:
|descr= Returns the nearest object of given type (or inherited classes) to given position within a sphere. Hardcoded radius is 50 meters. Unlike with [[nearestObjects]], where distance is measured in 2D space, [[nearestObject]] will be closest object in 3D space.
|descr= Returns the nearest object of given type (or inherited classes) to given position within a sphere. Hardcoded radius is 50 meters. Unlike with [[nearestObjects]], where distance is measured in 2D space, [[nearestObject]] will be closest object in 3D space.


{{Feature | Informative |
{{Feature|informative|
* in OFP, only objects of the exact provided class are found.
* in {{ofp}}, only objects of the exact provided class are found.
* if an object ID is used (Alternative Syntax 3, see '''Example 2''') the search range is unlimited}}
* if an object ID is used ({{HashLink|#Syntax 4}}, see {{HashLink|#Example 2}}) the search range is unlimited
}}


|s1= [[nearestObject]] [position, type]
|s1= [[nearestObject]] [position, type]


|p1= [position, type]: [[Array]]
|p1= position: [[Object]] or [[Array]] format [[Position2D]] or [[Position3D]] - position to start search at
|p2= position:  [[Position3D]] or [[Position2D]] or [[Object]] - position to start search at


|p3= type: [[String]] - type of object (see [[typeOf]], [[CfgVehicles|class type]])
|p2= type: [[String]] - type of object (see [[typeOf]], [[CfgVehicles|class type]])


|r1= [[Object]] - Nearest object, [[objNull]] otherwise
|r1= [[Object]] - nearest object, [[objNull]] otherwise


|s2= [[nearestObject]] position
|s2= [[nearestObject]] position


|p21= position: [[Position3D]] or [[Position2D]] - position to start search at
|p21= position: [[Array]] format [[Position2D]] or [[Position3D]] - position to start search at


|r2= [[Object]] - Nearest object, [[objNull]] otherwise
|r2= [[Object]] - nearest object, [[objNull]] otherwise


|s3= position [[nearestObject]] type
|s3= position [[nearestObject]] type
Line 55: Line 55:
|s4= position [[nearestObject]] id
|s4= position [[nearestObject]] id


|p61= position: [[Position3D]] or [[Position2D]] - position to start search at
|p61= position: [[Array]] format [[Position2D]] or [[Position3D]] - position to start search at


|p62= id: [[Number]] - object Visitor id
|p62= id: [[Number]] - object Visitor id
Line 84: Line 84:
}}
}}


 
{{Note
<dl class="command_description">
|user= Gnashes
 
|timestamp= 20190129153100
<dt></dt>
|text= The ID for Syntax 4 may be dynamically obtained via the following (as of {{arma3}} v1.88):
<dd class="notedate">Posted on January 29, 2019 - 15:31 (UTC)</dd>
<dt class="note">[[User:Gnashes|Gnashes]]</dt>
<dd class="note">
The ID for Alternative Syntax 3 may be dynamically obtained via the following (as of Arma 3 v1.88):
<code>{{cc|returns Object Terrain/Visitor ID, or -1 if no ID found (invalid/null object)}}
<code>{{cc|returns Object Terrain/Visitor ID, or -1 if no ID found (invalid/null object)}}
[[cursorObject]] [[call]] {
[[cursorObject]] [[call]] {
Line 98: Line 94:


[[private]] _find [[=]] ((_obj [[find]] "#") + 2);
[[private]] _find [[=]] ((_obj [[find]] "#") + 2);
[[if]] (_find > 1) [[then]] {
[[if]] (_find > 1) [[then]]
{
[[private]] _len [[=]] ((_obj [[find]] ":") - _find);
[[private]] _len [[=]] ((_obj [[find]] ":") - _find);
_id [[=]] _obj [[select]] [_find, _len];
_id [[=]] _obj [[select]] [_find, _len];
Line 105: Line 102:
_id
_id
};</code>
};</code>
</dd>
}}


<dt><dt>
{{Note
<dd class="notedate">Posted on February 12, 2020 - 08:55 (UTC)</dd>
|user= blackfisch
<dt class="note">[[User:blackfisch|blackfisch]]</dt>
|timestamp= 20200212085500
<dd class="note">
|text= Note, that if you want to check for the nearest Unit you have to check for {{ic|CAManBase}} instead of {{ic|Man}}, since {{ic|Man}} will also return any wildlife population in that area:
Note, that if you want to check for the nearest Unit you have to check for {{ic|CAManBase}} instead of {{ic|Man}}, since {{ic|Man}} will also return any wildlife population in that Area:


<code>nearestObject [player, "Man"]; {{cc|could possibly return rabbits or other wildlife}}
<code>nearestObject [player, "Man"]; {{cc|could possibly return rabbits or other wildlife}}
Line 117: Line 113:
nearestObject [player, "CAManBase"]; {{cc|only returns units}}
nearestObject [player, "CAManBase"]; {{cc|only returns units}}
</code>
</code>
</dd>
}}
 
</dl>

Revision as of 18:14, 24 November 2021

Hover & click on the images for description

Description

Description:
Returns the nearest object of given type (or inherited classes) to given position within a sphere. Hardcoded radius is 50 meters. Unlike with nearestObjects, where distance is measured in 2D space, nearestObject will be closest object in 3D space.
  • in Operation Flashpoint, only objects of the exact provided class are found.
  • if an object ID is used (Syntax 4, see Example 2) the search range is unlimited
Groups:
Object Detection

Syntax 1

Syntax:
nearestObject [position, type]
Parameters:
position: Object or Array format Position2D or Position3D - position to start search at
type: String - type of object (see typeOf, class type)
Return Value:
Object - nearest object, objNull otherwise

Syntax 2

Syntax:
nearestObject position
Parameters:
position: Array format Position2D or Position3D - position to start search at
Return Value:
Object - nearest object, objNull otherwise

Syntax 3

Syntax:
position nearestObject type
Parameters:
position: Position3D or Position2D - position to start search at
type: String - type of object (see typeOf, class type)
Return Value:
Object - nearest object, objNull otherwise

Syntax 4

Syntax:
position nearestObject id
Parameters:
position: Array format Position2D or Position3D - position to start search at
id: Number - object Visitor id
Return Value:
Object - nearest object, objNull otherwise

Examples

Example 1:
_nObject = nearestObject [2345, 6789]; _nObject = nearestObject [player, "StreetLamp"];
Example 2:
Return the object with ID 123456: _nObject = [0,0,0] nearestObject 123456;
Example 3:
_nObject = getPos player nearestObject "StreetLamp";
Example 4:
Return the nearest object with (typeOf _nObject == "#XXXX") -> #mark, #slop, etc. Unlimited search range: _nObject = _position nearestObject -1; Return the nearest object with (typeOf _nObject != ""). Search range is 50m: _nObject = nearestObject _position;
Example 5:
(See also allMissionObjects): _blood = nearestObject [player, "#slop"]; _step = nearestObject [player, "#mark"]; _track = nearestObject [player, "#track"]; _crater = nearestObject [player, "#crater"];

Additional Information

See also:
nearestObjectsnearObjectsReadynearObjectsobjectFromNetIdnearEntitiesentitiesobjectnearestBuildingnearestTerrainObjects

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
Gnashes - c
Posted on Jan 29, 2019 - 15:31 (UTC)
The ID for Syntax 4 may be dynamically obtained via the following (as of Arma 3 v1.88): // returns Object Terrain/Visitor ID, or -1 if no ID found (invalid/null object) cursorObject call { private _obj = str (param [0, objNull]); private _id = -1; private _find = ((_obj find "#") + 2); if (_find > 1) then { private _len = ((_obj find ":") - _find); _id = _obj select [_find, _len]; parseNumber _id; }; _id };
blackfisch - c
Posted on Feb 12, 2020 - 08:55 (UTC)
Note, that if you want to check for the nearest Unit you have to check for CAManBase instead of Man, since Man will also return any wildlife population in that area: nearestObject [player, "Man"]; // could possibly return rabbits or other wildlife nearestObject [player, "CAManBase"]; // only returns units