Pool::forEachInRange: Difference between revisions

From RAGE Multiplayer Wiki
m (Replaced HTML with template)
No edit summary
 
Line 1: Line 1:
This function is used for calling a function for each element in a pool, but only if it in range of position.
{{ServersideJsFunction}}
{{JSContainer|
 
{{Pool_list}}


==Syntax==
==Syntax==
Line 7: Line 10:


===Required Arguments===
===Required Arguments===
*'''callingFunction:''' Function, what will be called.
*'''callingFunction:''' Function to be executed for each entity within range.


==Example==  
==Example==
This example will generate an array of vehicles within 100 units of the player (game distance, meters).
This example collects all vehicles within 100 units of the player and stores them in an array.


{{ServersideCode|
{{ServersideCode|
<pre>
<pre>
let getVehiclesNearbyMe = (player) => {
let getVehiclesNearby = (player) => {
const returnVehicles = [];
    const nearbyVehicles = [];
    mp.vehicles.forEachInRange(player.position, 100, (vehicle) => {
mp.vehicles.forEachInRange(player.position, 100,
        nearbyVehicles.push(vehicle);
(vehicle) => {
    });
returnVehicles.push(vehicle);
    return nearbyVehicles;
}
);
return returnVehicles;
};
};


let vehiclesNearbyMe = getVehiclesNearbyMe(mp.players.at(0));
let nearbyVehicles = getVehiclesNearby(mp.players.at(0));
console.log(`Vehicles nearby me: x${vehiclesNearbyMe.length}`);
console.log(`Vehicles nearby: ${nearbyVehicles.length}`);
</pre>
</pre>
}}
}}
Line 33: Line 32:
==See Also==
==See Also==
{{EntityPool_function}}
{{EntityPool_function}}
}}

Latest revision as of 12:12, 10 November 2024

Server-Side
Function

 JavaScript



JavaScript Syntax


Available Entity Pools

The following are the main entity pools available on the RAGEMP, used for managing various game entities:

Entity Pool Description
mp.players Manages all connected players
mp.vehicles Manages all spawned vehicles
mp.objects Manages all created objects
mp.peds Manages all non-player characters (peds)
mp.markers Manages all markers in the game
mp.labels Manages all 3D text labels
mp.checkpoints Manages checkpoints
mp.blips Manages map blips
mp.colshapes Manages collision shapes

Syntax

pool.forEachInRange(Vector3 position, int range [, int dimension], function callingFunction);

Required Arguments

  • callingFunction: Function to be executed for each entity within range.

Example

This example collects all vehicles within 100 units of the player and stores them in an array.

Server-Side
let getVehiclesNearby = (player) => {
    const nearbyVehicles = [];
    mp.vehicles.forEachInRange(player.position, 100, (vehicle) => {
        nearbyVehicles.push(vehicle);
    });
    return nearbyVehicles;
};

let nearbyVehicles = getVehiclesNearby(mp.players.at(0));
console.log(`Vehicles nearby: ${nearbyVehicles.length}`);

See Also