Infinite loop...
#1

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.
Reply
#2

what are you even programming?
Reply
#3

You should really learn how to use timers though.
Reply
#4

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.
Reply
#5

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;

Reply
#6

Thanks, let me try it.
Reply
#7

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;

Reply
#8

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


Forum Jump:


Users browsing this thread: 1 Guest(s)