foreach
#1

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

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;
}
Reply
#3

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

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!
Reply
#5

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


Forum Jump:


Users browsing this thread: 1 Guest(s)