SA-MP Forums Archive
Loop not working properly? - 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: Loop not working properly? (/showthread.php?tid=451172)



Loop not working properly? - 101 - 16.07.2013

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;
}



Re: Loop not working properly? - iggy1 - 16.07.2013

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
    }
        //...
}



Re: Loop not working properly? - 101 - 16.07.2013

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


Re: Loop not working properly? - MP2 - 16.07.2013

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..


Re: Loop not working properly? - 101 - 16.07.2013

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.