foreach loop preferencing id0 over anyone else?
#1

so I've got a loop in a function on a 300ms timer, the timer gets called, it checks if the player has a Boolean set to true, if it does, it checks some math with their vehicle, now if i'm player 1 and player 0 has the Boolean to false, it works fine. but the second ID0 gets into the loop, it iterates id0 about 15 times before i'm lucky to see myself checked through.

before I post all my lengthy code for all prying eyes to steal my work, does anyone have any idea what could be causing this? Here's the start where it prints off if the player got an iteration done

pawn Код:
public FuncName()
{
    foreach(new i : Player)
    {
        if(!IsPlayerConnected(i)) continue;
        if(!PlayerStats[i][Boolean])
        {
            new str[80];
            format(str,sizeof(str),"THIS GUY WAS FALSE:%i",i);
            SendClientMessageToAll(ORANGE,str);
            continue;
        }
        else
        {
            new str[80];
            format(str,sizeof(str),"DRIFT TICK FOR PLAYERID %i",i);
            SendClientMessageToAll(ORANGE,str);
Код:
[15:15:28] DRIFT TICK FOR PLAYERID 0

[15:15:28] DRIFT TICK FOR PLAYERID 0

[15:15:28] DRIFT TICK FOR PLAYERID 0

[15:15:29] DRIFT TICK FOR PLAYERID 0

[15:15:29] DRIFT TICK FOR PLAYERID 0

[15:15:29] DRIFT TICK FOR PLAYERID 0

[15:15:30] DRIFT TICK FOR PLAYERID 0

[15:15:30] DRIFT TICK FOR PLAYERID 1

[15:15:30] DRIFT TICK FOR PLAYERID 0

[15:15:30] DRIFT TICK FOR PLAYERID 1

[15:15:30] DRIFT TICK FOR PLAYERID 0

[15:15:30] DRIFT TICK FOR PLAYERID 1

[15:15:31] DRIFT TICK FOR PLAYERID 0

[15:15:31] DRIFT TICK FOR PLAYERID 1

[15:15:31] DRIFT TICK FOR PLAYERID 0

[15:15:31] DRIFT TICK FOR PLAYERID 0

[15:15:31] DRIFT TICK FOR PLAYERID 0

[15:15:32] DRIFT TICK FOR PLAYERID 0

[15:15:32] DRIFT TICK FOR PLAYERID 0

[15:15:32] DRIFT TICK FOR PLAYERID 0

[15:15:33] DRIFT TICK FOR PLAYERID 0

[15:15:33] DRIFT TICK FOR PLAYERID 0

[15:15:33] DRIFT TICK FOR PLAYERID 0

[15:15:34] DRIFT TICK FOR PLAYERID 0

[15:15:34] DRIFT TICK FOR PLAYERID 0

[15:15:34] DRIFT TICK FOR PLAYERID 0

[15:15:35] DRIFT TICK FOR PLAYERID 0

[15:15:35] DRIFT TICK FOR PLAYERID 0

[15:15:35] DRIFT TICK FOR PLAYERID 0

[15:15:35] DRIFT TICK FOR PLAYERID 0

[15:15:36] DRIFT TICK FOR PLAYERID 0

[15:15:36] DRIFT TICK FOR PLAYERID 0

[15:15:36] DRIFT TICK FOR PLAYERID 0

[15:15:37] DRIFT TICK FOR PLAYERID 0

[15:15:37] DRIFT TICK FOR PLAYERID 0

[15:15:37] DRIFT TICK FOR PLAYERID 0

[15:15:38] DRIFT TICK FOR PLAYERID 0

[15:15:38] DRIFT TICK FOR PLAYERID 0

[15:15:38] DRIFT TICK FOR PLAYERID 1
I also have Slices timerfix warning me that the function took between 13ms and 60ms on occasion, but it doesn't seem to warn me that it's going higher so I assumed 300ms was a fine limit.
Reply
#2

4char
Reply
#3

Else is supposed to be called if boolean is false, that's what the exclamation operator is for, it is supposed to be going into else, but it's supposed to be doing it evenly, the code should look like

Код:
[15:15:31] DRIFT TICK FOR PLAYERID 0

[15:15:31] DRIFT TICK FOR PLAYERID 1

[15:15:31] DRIFT TICK FOR PLAYERID 0

[15:15:31] DRIFT TICK FOR PLAYERID 1

[15:15:31] DRIFT TICK FOR PLAYERID 0

[15:15:31] DRIFT TICK FOR PLAYERID 1

[15:15:31] DRIFT TICK FOR PLAYERID 0

[15:15:31] DRIFT TICK FOR PLAYERID 1
but instead it's doing a large amount of id:0 then one id:1
Reply
#4

Quote:
Originally Posted by pds2k12
Посмотреть сообщение
Why do you use continue below PlayerStats[i][Boolean], try removing that because that continues that loop to else, that is why else is getting called most of the time.
LOL. Some people.. xD

pawn Код:
public FuncName()
{
    foreach(Player, i)
    {
        new str[30];
        if(PlayerStats[i][Boolean]) format(str, sizeof(str), "DRIFT TICK FOR PLAYERID %d", i);
        else format(str, sizeof(str), "THIS GUY WAS FALSE: %d", i);
        SendClientMessageToAll(ORANGE, str);
    }
    //Rest of code...
}
Worth a shot, this honestly should work.
Reply
#5

Turns out I had bad code terminating the loop early due to the function never finishing, because I have a dedicated server I test this on and something outside of sa-mp decided not to send a value back and just infinitely looped, I didn't notice the asynchronous loop not returning to the sa-mp server, so it basically timed out the for loop and started again from player 0, under certain conditions it would return a value and the loop continued, which is why the erratic number pattern was printed into chat instead of a smooth 0/1/0/1/0/1/0/1

tl;dr, plugin code gone bad, didn't talk back to for loop, loop would fail and return itself ( safety or else it hangs the whole server ) and now it's fixed.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)