Pool::forEach: Difference between revisions

From RAGE Multiplayer Wiki
(Undo revision 7304 by SalwadoR (talk))
m (Replaced HTML with template)
Line 2: Line 2:


==Syntax==
==Syntax==
<syntaxhighlight lang="javascript">
<pre>
pool.forEach(Function callingFunction);
pool.forEach(Function callingFunction);
</syntaxhighlight>  
</pre>  
===Required Arguments===
===Required Arguments===
*'''callingFunction:''' Function, what will be called.
*'''callingFunction:''' Function, what will be called.
Line 11: Line 11:
This example will generate text with all player nicknames.
This example will generate text with all player nicknames.


<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>
<pre>
<syntaxhighlight lang="javascript" highlight="3-7">
let getNicknamesText = () => {
let getNicknamesText = () => {
let text = ``;
let text = ``;
Line 26: Line 25:
let blahBlah = getNicknamesText();
let blahBlah = getNicknamesText();
console.log(blahBlah != `` ? blahBlah : `Server not have players.`);
console.log(blahBlah != `` ? blahBlah : `Server not have players.`);
</syntaxhighlight>
</pre>
</div>
}}


==Example #2==  
==Example #2==  
This example will add a command to remove all server vehicles by forEach.
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;">
{{ServersideCode|
<div style="margin: 10px 10px 10px 10px;"><b>Server-Side</b></div>
<pre>
<syntaxhighlight lang="javascript" highlight="3-7">
mp.events.addCommand(`removeAll`,  
mp.events.addCommand(`removeAll`,  
(player) => {
(player) => {
Line 44: Line 42:
}
}
);
);
</syntaxhighlight>
</pre>
</div>
}}


==See Also==
==See Also==
{{EntityPool_function}}
{{EntityPool_function}}

Revision as of 11:56, 26 October 2018

This function is used for calling a function for each element in a pool.

Syntax

pool.forEach(Function callingFunction);

Required Arguments

  • callingFunction: Function, what will be called.

Example #1

This example will generate text with all player nicknames.

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

let blahBlah = getNicknamesText();
console.log(blahBlah != `` ? blahBlah : `Server not have players.`);

Example #2

This example will add a command to remove all server vehicles by forEach.

Server-Side
mp.events.addCommand(`removeAll`, 
	(player) => {
		mp.vehicles.forEach(
			(vehicle) => {
				vehicle.destroy();
			}
		);
		mp.players.broadcast(`${player.name} DESTROY ALL CARS!`);
	}
);

See Also