Pool::forEach: Difference between revisions
Almeidowski (talk | contribs) m (Adding a third and easier to understand example) |
No edit summary |
||
| Line 1: | Line 1: | ||
{{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== | ||
< | <syntaxhighlight lang="javascript"> | ||
pool.forEach(Function callingFunction) | pool.forEach(Function callingFunction) | ||
</ | </syntaxhighlight> | ||
===Required Arguments=== | ===Required Arguments=== | ||
*'''callingFunction:''' Function | *'''callingFunction:''' {{RageType|Function}} — A function to apply to each entity in the pool. | ||
==Example #1== | ==Examples== | ||
This example | ===Example #1=== | ||
This example collects and returns a string of all player nicknames on the server. | |||
{{ServersideCode| | {{ServersideCode| | ||
< | <syntaxhighlight lang="javascript"> | ||
let getNicknamesText = () => { | let getNicknamesText = () => { | ||
let text = ``; | |||
mp.players.forEach((player) => { | |||
text = text === `` ? player.name : `${text}, ${player.name}`; | |||
}); | |||
return text; | |||
}; | }; | ||
const nicknameText = getNicknamesText(); | |||
console.log( | console.log(nicknameText !== `` ? nicknameText : `No players online.`); | ||
</ | </syntaxhighlight> | ||
}} | }} | ||
==Example #2== | ===Example #2=== | ||
This example | This example adds a command to remove all vehicles on the server using `forEach`. | ||
{{ServersideCode| | {{ServersideCode| | ||
< | <syntaxhighlight lang="javascript"> | ||
mp.events.addCommand( | 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 | ===Example #3=== | ||
This example teleports a player who types `/port <name>` to the location of the specified player. | |||
{{ServersideCode| | {{ServersideCode| | ||
< | <syntaxhighlight lang="javascript"> | ||
mp.events.addCommand( | mp.events.addCommand("port", (player, name) => { | ||
mp.players.forEach((_player) => { | |||
if (_player.name === name) player.position = _player.position; | |||
}); | |||
}); | }); | ||
</ | </syntaxhighlight> | ||
}} | |||
}} | }} | ||
==See Also== | ==See Also== | ||
{{EntityPool_function}} | {{EntityPool_function}} | ||
[[Category:Serverside API]] | |||
Revision as of 11:58, 10 November 2024
Server-Side Function
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
- Functions
- Properties
- Arrays