SA-MP Forums Archive
Need Explanation With 'for' statement! - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Need Explanation With 'for' statement! (/showthread.php?tid=134382)



Need Explanation With 'for' statement! - Micko9 - 16.03.2010

Hello can anyone actually explain me everything in the commands like :
Код:
dcmd_SendAllTheirNames(playerid,params[])
{
    #pragma unused params
    for(i=0; i<MAX_PLAYERS; i++)
    {
       // code here...but explain me this...
    }
}
i usually get fast answers to thank you if you read this and can help me but i want a detailed explanation like alitle riping it apart...you understand...


Re: Need Explanation With 'for' statement! - Correlli - 16.03.2010

https://sampwiki.blast.hk/wiki/Keywords:Statements#for


Re: Need Explanation With 'for' statement! - ¤Adas¤ - 16.03.2010

This will loop around all players, where "i" is their ID.


Re: Need Explanation With 'for' statement! - Micko9 - 16.03.2010

Quote:
Originally Posted by ¤Adas¤
This will loop around all players, where "i" is their ID.
i got it now...its like this u can use something else ex:
Код:
#define SERVER_MAX_PLAYERS 20 // example 20 is my server's max players...
then in the cmd :
Код:
for (new i=0; i<SERVER_MAX_PLAYERS; i++)
{
   if (IsPlayerConnected(i))
   {
     SendClientMessage(playerid,0xAA000AA,"HAHA!");
   }
}
that wood work, right? ( if 20 wood be my max player setting )


Re: Need Explanation With 'for' statement! - ¤Adas¤ - 16.03.2010

SendClientMessage(i, ...);
It will send it for players from 0 to 19.


Re: Need Explanation With 'for' statement! - Micko9 - 16.03.2010

Quote:
Originally Posted by ¤Adas¤
SendClientMessage(i, ...);
It will send it for players from 0 to 19.
but if there is a player with id 20?


Re: Need Explanation With 'for' statement! - Correlli - 16.03.2010

Quote:
Originally Posted by Micko9
Quote:
Originally Posted by ¤Adas¤
SendClientMessage(i, ...);
It will send it for players from 0 to 19.
but if there is a player with id 20?
Then he won't see the message because you've limited your loop to 20 players.


Re: Need Explanation With 'for' statement! - Micko9 - 16.03.2010

Quote:
Originally Posted by Don Correlli
Quote:
Originally Posted by Micko9
Quote:
Originally Posted by ¤Adas¤
SendClientMessage(i, ...);
It will send it for players from 0 to 19.
but if there is a player with id 20?
Then he won't see the message because you've limited your loop to 20 players.
il put 21 then that wood loop 20 players!


Re: Need Explanation With 'for' statement! - Correlli - 16.03.2010

This will loop for 21 players - IDs: 0-20
pawn Код:
for(new i = 0; i < 21; i++)
{
  if(IsPlayerConnected(i)) SendClientMessage(i, 0xAA000AA, "HAHA!");
}
This will loop for 22 players - IDs: 0-21
pawn Код:
for(new i = 0; i <= 21; i++)
{
  if(IsPlayerConnected(i)) SendClientMessage(i, 0xAA000AA, "HAHA!");
}
The only difference in this code from the first one is that i've used <= so this means if i is smaller than 21 or equal to 21.

And i suggest to you to use ******'s foreach include - it's faster than normal for loop and you don't have to use IsPlayerConnected function because foreach already has this check in its code.

foreach include - http://forum.sa-mp.com/index.php?topic=117573.0

Example with foreach:
pawn Код:
foreach(Player, i) SendClientMessage(i, 0xAA000AA, "HAHA!");