SA-MP Forums Archive
how to use foreach ? - 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)
+--- Thread: how to use foreach ? (/showthread.php?tid=467835)



how to use foreach ? - Ahmed10020 - 05.10.2013

hi,iwant to know how to use foreach i've read about it forum.sa-mp.com/showthread.php?t=92679
but i dont know what is the usage...


Re: how to use foreach ? - JaKe Elite - 05.10.2013

Normal Loop

pawn Код:
for(new i=0; i < MAX_PLAYERS; i++)
{
//code
}
Foreach:

pawn Код:
foreach(new i : Player)
{
//code
}
- Note: The loop (foreach code) that i posted above only loops players (not including the NPCs)


Re: how to use foreach ? - doreto - 05.10.2013

https://sampforum.blast.hk/showthread.php?tid=92679 - everything you wanna know is here.


Re: how to use foreach ? - Threshold - 05.10.2013

Yes, in correction, as shown in the thread, it replaces the following:

pawn Код:
for(new i = 0; i < MAX_PLAYERS; i++)
{
    if(IsPlayerConnected(i))
    {
        //stuff
    }
}
So basically, when you want to cycle through CONNECTED players, you use foreach.

The most commonly used form is:
pawn Код:
foreach(Player, i)
{
    //stuff
}