SA-MP Forums Archive
for loop seems to be stopping the script from progressing. - 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: for loop seems to be stopping the script from progressing. (/showthread.php?tid=512916)



for loop seems to be stopping the script from progressing. - Denying - 13.05.2014

Uh, hi.. I've the following code:
pawn Код:
CMD:admins(playerid, params[])
{
    new p;
    SendClientMessage(playerid, COLOR_MESSAGE_ONE, "[Server Staff] {ABCBF5}Administrators:");
    for(p = 0; p <= MAX_PLAYERS; p++)
    {
        printf("%d", p);
        if(PlayerInfo[p][AdminLevel] > 0 && IsPlayerConnected(p))
        {
            GetPlayerName(p, pName, sizeof(pName));
            RPName(pName, '_');
            if(PlayerInfo[p][AdminLevel] == 1) format(msg, sizeof(msg), "- Trial Admin %s (%d)", pName, p);
            if(PlayerInfo[p][AdminLevel] == 2) format(msg, sizeof(msg), "- Basic Admin %s (%d)", pName, p);
            if(PlayerInfo[p][AdminLevel] == 3) format(msg, sizeof(msg), "- Senior Admin %s (%d)", pName, p);
            if(PlayerInfo[p][AdminLevel] == 4) format(msg, sizeof(msg), "- Lead Admin %s (%d)", pName, p);
            if(PlayerInfo[p][AdminLevel] == 5) format(msg, sizeof(msg), "- Head Admin %s (%d)", pName, p);
            if(PlayerInfo[p][AdminLevel] == 6) format(msg, sizeof(msg), "- Co-owner %s (%d)", pName, p);
            if(PlayerInfo[p][AdminLevel] == 7) format(msg, sizeof(msg), "- Owner %s (%d)", pName, p);
            SendClientMessage(playerid, COLOR_USAGE, msg);
        }
    }
    printf("..");
    SendClientMessage(playerid, COLOR_MESSAGE_ONE, "[Server Staff] {ABCBF5}Helpers:");
    for(p = 0; p <= MAX_PLAYERS; p++)
    {
        if(PlayerInfo[p][Helper] == 1 && IsPlayerConnected(p))
        {
            GetPlayerName(p, pName, sizeof(pName));
            RPName(pName, '_');
            format(msg, sizeof(msg), "- Helper %s (%d)", pName, p);
            SendClientMessage(playerid, COLOR_USAGE, msg);
        }
    }
    return 1;
}
Just saying.. it doesn't reach the second printf, however, it does finish the first one (it reaches 500)
Any idea? I just can't seem to figure it out.


Re: for loop seems to be stopping the script from progressing. - Aerotactics - 13.05.2014

Correction:
pawn Код:
for(p = 0; p < MAX_PLAYERS; p++)
You're telling the loop to continue looping while p is equal to MAX_PLAYERS, the loop keeps going because you didn't tell it to stop.


Re: for loop seems to be stopping the script from progressing. - Denying - 13.05.2014

omfg, I did this mistake when I only started scripting a year ago.
Used to add the equal sign from C lessons. :/

Thank you very much, just solved me A LOT of problems. +rep


Re: for loop seems to be stopping the script from progressing. - Aerotactics - 13.05.2014

Quote:
Originally Posted by Denying
Посмотреть сообщение
omfg, I did this mistake when I only started scripting a year ago.
Used to add the equal sign from C lessons. :/

Thank you very much, just solved me A LOT of problems. +rep
Not a problem. Glad I could help.


Re: for loop seems to be stopping the script from progressing. - Denying - 13.05.2014

Quote:
Originally Posted by ******
Посмотреть сообщение
If you use "<=" in C you are most likely doing it wrong there too.
Nope. It works perfectly fine there.
We use it on arrays, so let's say an array has 10 cells, instead of writing
for( I = 0; I < 10; I++)

we'd write

for( I = 0; I <=9; I++)

It has the same effect.

It means that it would continue as long as I is smaller or equal to 9.


Re: for loop seems to be stopping the script from progressing. - Denying - 13.05.2014

Quote:
Originally Posted by ******
Посмотреть сообщение
Yes, that works, but "MAX_PLAYERS" is the size of the array, so it is more like doing this in C:

Код:
for (I = 0; I <= 10; I++)
Which is wrong in either language.
Ye, though I haven't encountered this specific example in C, we always had a specific number of cells, and that always worked, that's why I said "I am used to add the equal sign from C".


Re: for loop seems to be stopping the script from progressing. - Denying - 13.05.2014

Quote:
Originally Posted by ******
Посмотреть сообщение
It would still be exactly the same in C - "MAX_PLAYERS" is a define:

pawn Код:
#define MAX_PLAYERS 10

new arr[MAX_PLAYERS];

for (new i = 0; i <= MAX_PLAYERS; ++i)
{
}
During compilation, "MAX_PLAYERS" is replaced by "10":

pawn Код:
new arr[10];

for (new i = 0; i <= 10; ++i)
{
}
Making that code wrong. Essentially, "MAX_PLAYERS" IS a specific number, it just doesn't use the digits 0-9.
Ye, I know that, I just didn't know how to phrase myself.
Anyway, it worked in my C lessons.


Re: for loop seems to be stopping the script from progressing. - Aerotactics - 13.05.2014

Quote:
Originally Posted by ******
Посмотреть сообщение
It would still be exactly the same in C - "MAX_PLAYERS" is a define:

pawn Код:
#define MAX_PLAYERS 10

new arr[MAX_PLAYERS];

for (new i = 0; i <= MAX_PLAYERS; ++i)
{
}
During compilation, "MAX_PLAYERS" is replaced by "10":

pawn Код:
new arr[10];

for (new i = 0; i <= 10; ++i)
{
}
Making that code wrong. Essentially, "MAX_PLAYERS" IS a specific number, it just doesn't use the digits 0-9.
Even in that scenario, you'd have to #undef MAX_PLAYERS in order to redefine it. Just saying.


Re: for loop seems to be stopping the script from progressing. - Aerotactics - 13.05.2014

Quote:
Originally Posted by ******
Посмотреть сообщение
I was demonstrating the principle, not providing a solid implementation of something.
I know, but it felt good to try to correct you.