Pool::forEachInRange: Difference between revisions

From RAGE Multiplayer Wiki
(Created page with "This function used for call function for each elements in pool, but only if it in range of position. ==Syntax== <syntaxhighlight lang="javascript"> void pool.forEachInRange(V...")
 
No edit summary
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
This function used for call function for each elements in pool, but only if it in range of position.
{{ServersideJsFunction}}
{{JSContainer|
 
{{Pool_list}}


==Syntax==
==Syntax==
<syntaxhighlight lang="javascript">
<pre>
void pool.forEachInRange(Vector3 position, int range [, int dimension], function callingFunction);
pool.forEachInRange(Vector3 position, int range [, int dimension], function callingFunction);
</syntaxhighlight>  
</pre>
 
===Required Arguments===
===Required Arguments===
*'''callingFunction:''' Function, what will be called.
*'''callingFunction:''' Function to be executed for each entity within range.


==Example #1==  
==Example==
That's example will generate array with vehicles nearby player in 100 units (game distance, like a meters).
This example collects all vehicles within 100 units of the player and stores them in an array.


<syntaxhighlight lang="javascript">
{{ServersideCode|
let getVehiclesNearbyMe = (player) => {
<pre>
const returnVehicles = [];
let getVehiclesNearby = (player) => {
    const nearbyVehicles = [];
mp.vehicles.forEachInRange(player.position, 100,
    mp.vehicles.forEachInRange(player.position, 100, (vehicle) => {
(vehicle) => {
        nearbyVehicles.push(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}`);
</syntaxhighlight>
</pre>
}}


==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