Pool::forEach: Difference between revisions

From RAGE Multiplayer Wiki
m (Adding a third and easier to understand example)
No edit summary
Line 1: Line 1:
This function is used for calling a function for each element in a pool.
{{ServersideJsFunction}}
{{JSContainer|
 
==Available Entity Pools==
 
* '''mp.players''' - Pool of all connected players
* '''mp.vehicles''' - Pool of all active vehicles
* '''mp.objects''' - Pool of all world objects
* '''mp.peds''' - Pool of all non-player characters (peds)
* '''mp.markers''' - Pool of all markers in the game
* '''mp.blips''' - Pool of all blips on the map
* '''mp.checkpoints''' - Pool of all checkpoints
* '''mp.colshapes''' - Pool of all collision shapes
* '''mp.labels''' - Pool of all text labels in the world
 
==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==
<pre>
<syntaxhighlight lang="javascript">
pool.forEach(Function callingFunction);
pool.forEach(Function callingFunction)
</pre>  
</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.


{{ServersideCode|
{{ServersideCode|
<pre>
<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.`);
</pre>
</syntaxhighlight>
}}
}}


==Example #2==  
===Example #2===
This example will add a command to remove all server vehicles by forEach.
This example adds a command to remove all vehicles on the server using `forEach`.
 
{{ServersideCode|
{{ServersideCode|
<pre>
<syntaxhighlight lang="javascript">
mp.events.addCommand(`removeAll`,  
mp.events.addCommand("removeAll", (player) => {
(player) => {
    mp.vehicles.forEach((vehicle) => {
mp.vehicles.forEach(
        vehicle.destroy();
(vehicle) => {
    });
vehicle.destroy();
    mp.players.broadcast(`${player.name} has destroyed all vehicles!`);
}
});
);
</syntaxhighlight>
mp.players.broadcast(`${player.name} DESTROY ALL CARS!`);
}
);
</pre>
}}
}}
==Example #3==
 
This example will teleport a player that types the command /port to the player whose name he writes as 1st argument.
===Example #3===
This example teleports a player who types `/port <name>` to the location of the specified player.
 
{{ServersideCode|
{{ServersideCode|
<pre>
<syntaxhighlight lang="javascript">
mp.events.addCommand('port', (player, name) => {
mp.events.addCommand("port", (player, name) => {
mp.players.forEach(_player => {
    mp.players.forEach((_player) => {
if(_player.name === name)
        if (_player.name === name) player.position = _player.position;
player.position = _player.position;
    });
});
});
});
</pre>
</syntaxhighlight>
}}
 
}}
}}
==See Also==
==See Also==
{{EntityPool_function}}
{{EntityPool_function}}
[[Category:Serverside API]]

Revision as of 11:58, 10 November 2024

Server-Side
Function

 JavaScript



JavaScript Syntax


Available Entity Pools

  • mp.players - Pool of all connected players
  • mp.vehicles - Pool of all active vehicles
  • mp.objects - Pool of all world objects
  • mp.peds - Pool of all non-player characters (peds)
  • mp.markers - Pool of all markers in the game
  • mp.blips - Pool of all blips on the map
  • mp.checkpoints - Pool of all checkpoints
  • mp.colshapes - Pool of all collision shapes
  • mp.labels - Pool of all text labels in the world

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