Loop not working properly?
#1

So, I've got a loop in which every 5secs it checks wanted level, and lowers it by 1. The thing is, it will only work for the lowest ID having the wanted level, for others it won't.

So for example
ID 0: Wanted level 6 (will go -)
ID 1: Wanted level 6 (will stay 6, but will go - after ID 0 is 0)

Why does this happen?

pawn Код:
public LowerWanted()
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(GetPlayerWantedLevel(i) >= 6)
        {
            SetPlayerWantedLevel(i, 5);
            return 1;
        }
        else if(GetPlayerWantedLevel(i) == 5)
        {
            SetPlayerWantedLevel(i, 4);
            return 1;
        }
        else if(GetPlayerWantedLevel(i) == 4)
        {
            SetPlayerWantedLevel(i, 3);
            return 1;
        }
        else if(GetPlayerWantedLevel(i) == 3)
        {
            SetPlayerWantedLevel(i, 2);
            return 1;
        }
        else if(GetPlayerWantedLevel(i) == 2)
        {
            SetPlayerWantedLevel(i, 1);
            return 1;
        }
        else if(GetPlayerWantedLevel(i) == 1)
        {
            SetPlayerWantedLevel(i, 0);
        }
    }
    return 1;
}
Reply
#2

It's because your "returning 1" from inside the loop - causing it to end prematurely.

pawn Код:
for(new i = 0; i < MAX_PLAYERS; i++)
{
    if(GetPlayerWantedLevel(i) >= 6)
    {
        SetPlayerWantedLevel(i, 5);
        return 1;//remove these
    }
    else if(GetPlayerWantedLevel(i) == 5)
    {
        SetPlayerWantedLevel(i, 4);
        return 1;//all of these
    }
        //...
}
Reply
#3

Thanks iggy, I totally forgot I even had that in there.
Reply
#4

Ever heard of maths?

pawn Код:
public LowerWanted()
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(GetPlayerWantedLevel(i) > 0)
        {
            SetPlayerWantedLevel(i, GetPlayerWantedLevel(i) - 1);
        }
    }
    return 1;
}
Also it seems a bit silly reducing their level so fast - what's the point of being wanted for ~10 seconds? You might as well not make them wanted in the first place..
Reply
#5

Quote:
Originally Posted by MP2
Посмотреть сообщение
Ever heard of maths?

pawn Код:
public LowerWanted()
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(GetPlayerWantedLevel(i) > 0)
        {
            SetPlayerWantedLevel(i, GetPlayerWantedLevel(i) - 1);
        }
    }
    return 1;
}
Also it seems a bit silly reducing their level so fast - what's the point of being wanted for ~10 seconds? You might as well not make them wanted in the first place..
I did hear about math, however I did it like I did for a reason.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)