how to use foreach ?
#1

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...
Reply
#2

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)
Reply
#3

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

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
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)