SA-MP Forums Archive
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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: foreach (/showthread.php?tid=198479)



foreach - armyoftwo - 12.12.2010

I have 2 players online but it only loops only for 1?
Код:
stock IsOnline(cid)
{
    foreach(Player, i) {
        printf("CharID: %i  Search ID:%i", AccountData[i][characterID], cid);
        if(AccountData[i][characterID] == cid) return i;
        else return INVALID_PLAYER_ID;
    }
}
It should print 2 times, because there are 2 players but it stops at first player so it prints only 1 time.
What's the prob?

EDIT: I just used my brain and my brain says that it will stop looping at returns, any ideas?
EDIT2: testing code


Re: foreach - MadeMan - 12.12.2010

pawn Код:
stock IsOnline(cid)
{
    foreach(Player, i) {
        printf("CharID: %i  Search ID:%i", AccountData[i][characterID], cid);
        if(AccountData[i][characterID] == cid) return i;
    }
    return INVALID_PLAYER_ID;
}



Re: foreach - Marcel - 12.12.2010

MadeMan: You only added some brackets, the working of the code hasn't been changed. I suggest you downloading the latest version of foreach. In my opinion there is no error in the code. What you could try is this code:
pawn Код:
stock IsOnline (cid)
{
    foreach (Player, i)
    {
        printf ("Player id: %d", i);
    }
}
In this way you can try what's happening, and see if it runs through all players.


Re: foreach - armyoftwo - 12.12.2010

Quote:
Originally Posted by MadeMan
Посмотреть сообщение
pawn Код:
stock IsOnline(cid)
{
    foreach(Player, i) {
        printf("CharID: %i  Search ID:%i", AccountData[i][characterID], cid);
        if(AccountData[i][characterID] == cid) return i;
    }
    return INVALID_PLAYER_ID;
}
Marcel, it's working. Thanks MadeMan
The problem was that i stopped the loop - "return INVALID_PLAYER_ID", i needed to put that outside the loop, and i didn't notice it at all!


Re: foreach - Marcel - 12.12.2010

Oh, yes. I see now! Glad to see it's fixed!