Arma 3 Scripting

nearestObject

  • Arma 3 v0.50
  • First in Operation Flashpoint v1.00

Command · Markdown · JSON · original wiki page

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.

Note

  • 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

Syntax

Syntax 1

nearestObject [position, type]

Parameters:

Returns: Object - nearest object, objNull otherwise

Syntax 2

nearestObject position

Parameters:

Returns: Object - nearest object, objNull otherwise

Syntax 3

position nearestObject typeOrId

Parameters:

Returns: Object - nearest object, objNull otherwise

Syntax 4

position nearestObject id

Parameters:

Returns: 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"];

See also

nearestObjects · nearObjectsReady · nearObjects · objectFromNetId · nearEntities · entities · object · nearestBuilding · nearestTerrainObjects · nearestMines

Community notes

blackfisch

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