nearestObject: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "\[\[Category\: ?Command[ _]Group\:.*\|\{\{uc\:\{\{PAGENAME\}\}\}\}\]\] " to "")
No edit summary
 
(88 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Command|Comments=
{{RV|type=command
____________________________________________________________________________________________


| ofp |Game name=
|game1= ofp
|version1= 1.00


|1.00|Game version=
|game2= ofpe
|version2= 1.00


|gr1= World Information |GROUP1=
|game3= arma1
____________________________________________________________________________________________
|version3= 1.00


| 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.
|game4= arma2
|version4= 1.00


{{Informative |
|game5= arma2oa
* in OFP, only objects of the exact provided class are found.
|version5= 1.50
* if an object ID is used (Alternative Syntax 3, see '''Example 2''') the search range is unlimited}} |DESCRIPTION=
____________________________________________________________________________________________


| [[nearestObject]] [position, type] |SYNTAX=
|game6= tkoh
|version6= 1.00


|p1= [position, type]: [[Array]] |Parameter1=
|game7= arma3
|p2= position:  [[Position3D]] or [[Position2D]] or [[Object]] - position to start search at |Parameter2=
|version7= 0.50


|p3= type: [[String]] - type of object (see [[typeOf]], [[CfgVehicles|class type]]) |Parameter3=
|gr1= Object Detection


| [[Object]] - Nearest object, [[objNull]] otherwise |RETURNVALUE=
|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.


|s2= [[nearestObject]] position |SYNTAX2=
{{Feature|informative|
* in {{ofp}}, only objects of the exact provided class are found.
* if an object ID is used ({{Link|#Syntax 4}}, see {{Link|#Example 2}}) the search range is unlimited
}}
 
|s1= [[nearestObject]] [position, type]
 
|p1= position: [[Object]] or [[Array]] format [[Position#Introduction|Position2D]] or [[Position#Introduction|Position3D]] - position to start search at


|p21= position: [[Position3D]] or [[Position2D]] - position to start search at |PARAMETER21=
|p2= type: [[String]] - type of object (see [[typeOf]], [[CfgVehicles|class type]])


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


|s3= position [[nearestObject]] type |SYNTAX3=
|s2= [[nearestObject]] position


|p41= position: [[Position3D]] or [[Position2D]] - position to start search at |PARAMETER41=
|p21= position: [[Array]] format [[Position#Introduction|Position2D]] or [[Position#Introduction|Position3D]] - position to start search at


|p42= type: [[String]] - type of object (see [[typeOf]], [[CfgVehicles|class type]]) |PARAMETER42=
|r2= [[Object]] - nearest object, [[objNull]] otherwise


|r3= [[Object]] - nearest object, [[objNull]] otherwise |RETURNVALUE3=
|s3= position [[nearestObject]] typeOrId
____________________________________________________________________________________________


|s4= position [[nearestObject]] id |SYNTAX4=
|p41= position: [[Position#Introduction|Position3D]] or [[Position#Introduction|Position2D]] - position to start search at


|p61= position: [[Position3D]] or [[Position2D]] - position to start search at |PARAMETER61=
|p42= typeOrId: [[String]] - type of object (see [[typeOf]], [[CfgVehicles|class type]]) or {{GVI|arma3|2.10|size= 0.75}} Visitor id (see [[getObjectID]])


|p62= id: [[Number]] - object Visitor id |PARAMETER62=
|r3= [[Object]] - nearest object, [[objNull]] otherwise


|r4= [[Object]] - nearest object, [[objNull]] otherwise |RETURNVALUE4=
|s4= position [[nearestObject]] id
____________________________________________________________________________________________


 
|p61= position: [[Array]] format [[Position#Introduction|Position2D]] or [[Position#Introduction|Position3D]] - position to start search at
|x1= <code>_nObject = [[nearestObject]] [2345, 6789];
 
_nObject = [[nearestObject]] [<nowiki/>[[player]], "StreetLamp"];</code> |EXAMPLE1=
|p62= id: [[Number]] - object Visitor id
 
|r4= [[Object]] - nearest object, [[objNull]] otherwise
 
|x1= <sqf>
_nObject = nearestObject [2345, 6789];
_nObject = nearestObject [player, "StreetLamp"];
</sqf>


|x2= Return the object with ID 123456:
|x2= Return the object with ID 123456:
<code>_nObject = [0,0,0] [[nearestObject]] 123456;</code> |EXAMPLE2=
<sqf>_nObject = [0,0,0] nearestObject 123456;</sqf>


|x3= <code>_nObject = [[getPos]] [[player]] [[nearestObject]] "StreetLamp"; </code> |EXAMPLE3=
|x3= <sqf>_nObject = getPos player nearestObject "StreetLamp";</sqf>


|x4= Return the nearest object with ([[typeOf]] _nObject == "#XXXX") -> #mark, #slop, etc. Unlimited search range:
|x4= Return the nearest object with ([[typeOf]] _nObject == "#XXXX") -> #mark, #slop, etc. Unlimited search range:
<code>_nObject = _position [[nearestObject]] -1; </code>
<sqf>_nObject = _position nearestObject -1;</sqf>


Return the nearest object with ([[typeOf]] _nObject != ""). Search range is 50m:
Return the nearest object with ([[typeOf]] _nObject != ""). Search range is 50m:
<code>_nObject = [[nearestObject]] _position;</code> |EXAMPLE4=
<sqf>_nObject = nearestObject _position;</sqf>


|x5= (See also [[allMissionObjects]]):
|x5= (See also [[allMissionObjects]]):
<code>_blood = [[nearestObject]] [<nowiki/>[[player]], "#slop"];
<sqf>
_step   = [[nearestObject]] [<nowiki/>[[player]], "#mark"];
_blood = nearestObject [player, "#slop"];
_track = [[nearestObject]] [<nowiki/>[[player]], "#track"];
_step = nearestObject [player, "#mark"];
_crater = [[nearestObject]] [<nowiki/>[[player]], "#crater"];</code> |EXAMPLE5=
_track = nearestObject [player, "#track"];
____________________________________________________________________________________________
_crater = nearestObject [player, "#crater"];
</sqf>


| [[nearestObjects]], [[nearObjectsReady]], [[nearObjects]], [[objectFromNetId]], [[nearEntities]], [[entities]], [[object]], [[nearestBuilding]], [[nearestTerrainObjects]] |SEEALSO=
|seealso= [[nearestObjects]] [[nearObjectsReady]] [[nearObjects]] [[objectFromNetId]] [[nearEntities]] [[entities]] [[object]] [[nearestBuilding]] [[nearestTerrainObjects]] [[nearestMines]]
}}
}}


<h3 style="display:none">Notes</h3>
{{Note
 
|user= Gnashes
<dl class="command_description">
|timestamp= 20190129153100
<!-- Note Section BEGIN -->
|text= The ID for Syntax 4 may be dynamically obtained via the following (as of {{arma3}} v1.88):
 
(for {{arma3}} v2.10 or newer, use [[getObjectID]] instead)
<!-- Note Section END -->
<sqf>
</dl>
// returns Object Terrain/Visitor ID, or -1 if no ID found (invalid/null object)
 
cursorObject call {
<h3 style="display:none">Bottom Section</h3>
private _obj = str (param [0, objNull]);
 
private _id = -1;
[[Category:Scripting Commands|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands OFP 1.46|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands OFP 1.96|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands OFP 1.99|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Armed Assault|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Arma 2|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Take On Helicopters|{{uc:{{PAGENAME}}}}]]


<!-- CONTINUE Notes -->
private _find = ((_obj find "#") + 2);
<dl class="command_description">
if (_find > 1) then
<dd class="notedate">Posted on January 29, 2019 - 15:31 (UTC)</dd>
{
<dt class="note">[[User:Gnashes|Gnashes]]</dt>
private _len = ((_obj find ":") - _find);
<dd class="note">
_id = _obj select [_find, _len];
The ID for Alternative Syntax 3 may be dynamically obtained via the following (as of Arma 3 v1.88):
parseNumber _id;
<code>{{cc|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
_id
};</code>
};
</dd>
</sqf>
</dl>
}}
<!-- DISCONTINUE Notes -->


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


<code>nearestObject [player, "Man"]; {{cc|could possibly return rabbits or other wildlife}}
<sqf>
 
nearestObject [player, "Man"]; // could possibly return rabbits or other wildlife
nearestObject [player, "CAManBase"]; {{cc|only returns units}}
nearestObject [player, "CAManBase"]; // only returns units
</code>
</sqf>
</dd>
}}
</dl>
<!-- DISCONTINUE Notes -->

Latest revision as of 19:21, 19 July 2024

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 typeOrId
Parameters:
position: Position3D or Position2D - position to start search at
typeOrId: String - type of object (see typeOf, class type) or Arma 3 logo black.png2.10 Visitor id (see getObjectID)
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:
nearestObjects nearObjectsReady nearObjects objectFromNetId nearEntities entities object nearestBuilding nearestTerrainObjects 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
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): (for Arma 3 v2.10 or newer, use getObjectID instead)
// 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