SA-MP Forums Archive
Question about for loops - 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: Question about for loops (/showthread.php?tid=412672)



Question about for loops - CoDeZ - 02.02.2013

Hello, While looking in some game modes, i saw this loop
pawn Код:
for(new i; i<MAX_PLAYERS; i++)
{
    if(IsPlayerConnected(i))
    {
        // do the stuff here.
    }
}
Question is, why should we check if the player is connected or not? even though it works without checking the player


Re: Question about for loops - MP2 - 02.02.2013

Because you only want to do stuff to players who are connected. Use foreach anyway.


Re: Question about for loops - Vince - 02.02.2013

For most things this is indeed a redundant check since other functions also have inbuilt connection checks. Though you will need to evaluate every case separately. On a side note, I use this configuration:

pawn Код:
for(new i; i < MAX_PLAYERS; i++)
{
    if(!IsPlayerConnected(i)) continue;

    // do the stuff here.
}
In before someone comes blabbing about foreach.


Re: Question about for loops - CoDeZ - 02.02.2013

Thanks both.