Pool::forEach: Difference between revisions

From RAGE Multiplayer Wiki
(Small fix)
(Pool list)
 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
This function is used for calling a function for each element in a pool.
{{ServersideJsFunction}}
{{JSContainer|
 
{{Pool_list}}
 
==Summary==
The `pool.forEach` function iterates over each element in a pool, applying a specified function to each entity. It’s useful for actions that involve every entity within a pool, such as broadcasting messages, modifying player states, or destroying all vehicles.


==Syntax==
==Syntax==
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
void pool.forEach(function callingFunction);
pool.forEach(Function callingFunction)
</syntaxhighlight>  
</syntaxhighlight>
 
===Required Arguments===
===Required Arguments===
*'''callingFunction:''' Function, what will be called.
*'''callingFunction:''' {{RageType|Function}} — A function to apply to each entity in the pool.


==Example #1==  
==Examples==
This example will generate text with all player nicknames.
===Example #1===
This example collects and returns a string of all player nicknames on the server.


<div class="header" style="background-color: #408DAE; color: #FFFFFF; border: 2px solid #408DAE;">
{{ServersideCode|
<div style="margin: 10px 10px 10px 10px;"><b>Server-Side</b></div>
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
let getNicknamesText = () => {
let getNicknamesText = () => {
let text = ``;
    let text = ``;
mp.players.forEach(
    mp.players.forEach((player) => {
(player, id) => {
        text = text === `` ? player.name : `${text}, ${player.name}`;
text = text == `` ? player.name : `${text} , ${player.name}`;
    });
}
    return text;
);
return text;
};
};


let blahBlah = getNicknamesText();
const nicknameText = getNicknamesText();
console.log(blahBlah != `` ? blahBlah : `Server not have players.`);
console.log(nicknameText !== `` ? nicknameText : `No players online.`);
</syntaxhighlight>
</syntaxhighlight>
</div>
}}
 
===Example #2===
This example adds a command to remove all vehicles on the server using `forEach`.
 
{{ServersideCode|
<syntaxhighlight lang="javascript">
mp.events.addCommand("removeAll", (player) => {
    mp.vehicles.forEach((vehicle) => {
        vehicle.destroy();
    });
    mp.players.broadcast(`${player.name} has destroyed all vehicles!`);
});
</syntaxhighlight>
}}
 
===Example #3===
This example teleports a player who types `/port <name>` to the location of the specified player.


==Example #2==
{{ServersideCode|
This example will add a command to remove all server vehicles by forEach.
<div class="header" style="background-color: #408DAE; color: #FFFFFF; border: 2px solid #408DAE;">
<div style="margin: 10px 10px 10px 10px;"><b>Server-Side</b></div>
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
mp.events.addCommand(`removeAll`,  
mp.events.addCommand("port", (player, name) => {
(player) => {
    mp.players.forEach((_player) => {
mp.vehicles.forEach(
        if (_player.name === name) player.position = _player.position;
(vehicle) => {
    });
vehicle.destroy();
});
}
);
mp.players.broadcast(`${player.name} DESTROY ALL CARS!`);
}
);
</syntaxhighlight>
</syntaxhighlight>
</div>
}}


}}
==See Also==
==See Also==
{{EntityPool_function}}
{{EntityPool_function}}
[[Category:Serverside API]]

Latest revision as of 12:10, 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

Summary

The `pool.forEach` function iterates over each element in a pool, applying a specified function to each entity. It’s useful for actions that involve every entity within a pool, such as broadcasting messages, modifying player states, or destroying all vehicles.

Syntax

pool.forEach(Function callingFunction)

Required Arguments

  • callingFunction: Function — A function to apply to each entity in the pool.

Examples

Example #1

This example collects and returns a string of all player nicknames on the server.

Server-Side
let getNicknamesText = () => {
    let text = ``;
    mp.players.forEach((player) => {
        text = text === `` ? player.name : `${text}, ${player.name}`;
    });
    return text;
};

const nicknameText = getNicknamesText();
console.log(nicknameText !== `` ? nicknameText : `No players online.`);

Example #2

This example adds a command to remove all vehicles on the server using `forEach`.

Server-Side
mp.events.addCommand("removeAll", (player) => {
    mp.vehicles.forEach((vehicle) => {
        vehicle.destroy();
    });
    mp.players.broadcast(`${player.name} has destroyed all vehicles!`);
});

Example #3

This example teleports a player who types `/port <name>` to the location of the specified player.

Server-Side
mp.events.addCommand("port", (player, name) => {
    mp.players.forEach((_player) => {
        if (_player.name === name) player.position = _player.position;
    });
});



See Also