SA-MP Forums Archive
Infinite loop... - 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: Infinite loop... (/showthread.php?tid=653303)



Infinite loop... - insus100 - 01.05.2018

Hello, so I'm having some trouble trying to fix this, and finally I have ran out of ideas.

So I got this loop:

PHP код:
new random INVALID_PLAYER_ID;
    do{
        
random Iter_Random(Player);// I use foreach include (pick a random player).
    
}while(!IsLoggedIn[random] || IsJailed[random] == || IsAFK[random] == 1); 
I have checks, so if people disconnect and players reach 0, it won't start, the problem comes when someone disconnects while the loop is running, and Player counter reaches 0, then the loop will go forever and freeze the server. I have tried this:

PHP код:
new random INVALID_PLAYER_IDcounter 0;
    do{
        
random Iter_Random(Player);
    
counter++;
    }while(!
IsLoggedIn[random] || IsJailed[random] == || IsAFK[random] == || counter 30);//to try and stop it, but still freezes the server. 
I have tried checking player counter while in the loop, but nothing.

Any ideas? Thank you.


Re: Infinite loop... - CodeStyle175 - 01.05.2018

what are you even programming?


Re: Infinite loop... - JesterlJoker - 01.05.2018

You should really learn how to use timers though.


Re: Infinite loop... - insus100 - 01.05.2018

Quote:
Originally Posted by CodeStyle175
Посмотреть сообщение
what are you even programming?
@CodeStyle175 It is choosing a random player, that's not AFK or Jailed.
@JesterlJoker Sorry, but why would I need to use a timer here? You mean the counter right? May you teach me please? Thank you.


Re: Infinite loop... - CodeStyle175 - 01.05.2018

maybe something like this?
PHP код:
new PlayerCnt;
public 
OnPlayerConnect(playerid){
    
PlayerCnt++;
    return 
1;
}
public 
OnPlayerDisconnect(playerid,reason){
    
PlayerCnt--;
    return 
1;
}
SelectRandomPlayer(){
    if(
PlayerCnt 30)return -1;//returns -1 cause its value that player cant have...
    
new rnd,
        
bool:rightone=false;
    while(
rightone!=true){
        if(
PlayerCnt 30)return -1;
        
rnd Iter_Random(Player); 
        
rightone=(!IsLoggedIn[rnd]||IsJailed[rnd]==1||IsAFK[rnd]==1)?false:true;
    }
    return 
rnd;




Re: Infinite loop... - insus100 - 01.05.2018

Thanks, let me try it.


Re: Infinite loop... - Dayrion - 01.05.2018

You should add something like that:
PHP код:
new random Iter_Random(Player),
    
count,
    
max_count Iter_Count(Player);
while(!
IsLoggedIn[random] || IsJailed[random] == || IsAFK[random] == 1)

    
random Iter_Random(Player);// I use foreach include (pick a random player). 
    
if(++count max_count)
        break;




Re: Infinite loop... - jlalt - 01.05.2018

PHP код:
    }while(( !IsLoggedIn[random] || IsJailed[random] == || IsAFK[random] == 1) && counter 30);//to try and stop it, but still freezes the server.