EntityInRange
The (nearest) entity-in-range interface helps you to easily create event based listeners to check what is the nearest entity to the player.
Simple example
Public interface methods
create(texture_names, on_reach_callback, on_leave_callback):EntityInRangeListener
create(texture_names, on_reach_callback, on_leave_callback):EntityInRangeListener
Creates a listener that calls the on_reach_callback
if the player reaches the nearest entity of given texture_names
and vice versa calls the on_leave_callback
if the player lefts it.
Note: The created listener is initially active (on).
Returns an instance of EntityInRangeListener
.
texture_names
table Array of texture names.on_reach_callback
function Callback for "on-reach".Arguments:
nearest_entity
EntityInRangeData Object with entity details.listener_id
number ID of listener object.
on_leave_callback
function, optional Callback for "on-left".Same arguments as
on_reach_callback
.
custom_range
number, optional Custom range for the listener. Default seeConfig.EntityInRange.DEFAULT_SEARCH_RADIUS
in config.lua. Be careful with this value, because it can have a big impact on performance.
getListenerById(listener_id):EntityInRangeListener
getListenerById(listener_id):EntityInRangeListener
Returns the listener object (EntityInRangeListener
) matching the given listener ID otherwise nil
.
listener_id
number ID of a listener.
getEntitiesNearby(x, y, z, radius, model_name_or_hash, entity_type, filter_func):EntityInRangeData[]|nil
getEntitiesNearby(x, y, z, radius, model_name_or_hash, entity_type, filter_func):EntityInRangeData[]|nil
Returns the nearest entity (data object EntityInRangeData
) of given texture_name
or nil
if no entity was found.
x
number X position.y
number Y position.z
number Z position.radius
number, Range in meters to search for.texture_name
string|number|table<string|number>, optional Texture name(s) or hash(es)entity_type
number, optional Entity type. DefaultENTITY_TYPE_OBJECT
filter_func
function, optional Optional filter function to filter the found entities.Arguments:
entity
numbermodel_hash
number
Returns:
boolean:
true
if the entity should be included in the result otherwisefalse
.
Example:
getNearestObjectEntity(x, y, z, texture_name, range, entity_type):EntityInRangeData|nil
getNearestObjectEntity(x, y, z, texture_name, range, entity_type):EntityInRangeData|nil
Returns the nearest entity (data object EntityInRangeData
) of given texture_name
or nil
if no entity was found.
x
number X position.y
number Y position.z
number Z position.texture_name
string|number Texture name or hashrange
number, optional Range to search for. Default seeConfig.EntityInRange.DEFAULT_SEARCH_RADIUS
in config.lua. Be careful with this value, because it can have a big impact on performance.entity_type
number, optional Entity type. DefaultENTITY_TYPE_OBJECT
Example:
ENTITY_TYPE_OBJECT
number, static
ENTITY_TYPE_OBJECT
number, staticEntity type for objects.
ENTITY_TYPE_PLAYER
number, static
ENTITY_TYPE_PLAYER
number, staticEntity type for players.
ENTITY_TYPE_VEHICLE
number, static
ENTITY_TYPE_VEHICLE
number, staticEntity type for vehicles.
Public methods of EntityInRangeListener
EntityInRangeListener
remove():EntityInRangeListener
remove():EntityInRangeListener
Removes the listener from the main entity-in-range checking loop and returns itself.
off():EntityInRangeListener
off():EntityInRangeListener
Alias for remove()
.
on():EntityInRangeListener
on():EntityInRangeListener
Insert the listener back to the main entity-in_range checking loop and returns itself.
Development notes
Open todos
Known issues
If a related resource is restarted and the player stand inside a range the Entity In Range trys to call the on-leave callback, but the resource is not available anymore. This results in an error.
Item sets have a maximum of entities they can handle. All entities above this maximum can not be found. This is a REDM/RDR2 issue.
Performance issues with classes/functions as callback arguments
Due to REDM restrictions (due to behavior currently unknown to us) on passing values between exported and non-exported script modules no classes or functions (callbacks) are passed as arguments into events. In this interface only the ID of the listener object is passed instead of the listener object itself. You can then get the listener object via the listener ID if required. This saves a lot of performance.
Last updated