Question
#1

I help a friend with a prison rp gamemode and i found this:
PHP код:
COMMAND:pm(playeridparams[])
{
    new 
targetidmessage[128], str[128], i;
    
0;
    if(
sscanf(params,"us[128]"targetidmessage))return SendClientMessage(playeridGREY"USAGE: /pm [playerid] [message]");
    if(!
IsPlayerConnected(targetid)) return SendClientMessage(playeridGREY"ID-ul tinta nu exista.");
    if(
targetid == playerid) return SendClientMessage(playeridGREY"Nu iti poti trimite PM singur.");
    if(
PlayerStat[playerid][TogPM] == 1) return SendClientMessage(playeridGREY"You have disabled PMs (/togpm to enable them).");
    if(
PlayerStat[playerid][AdminLevel] < && PlayerStat[targetid][TogPM] == 1) return SendClientMessage(playeridGREY"The player has disabled PMs.");
    if(
PlayerStat[playerid][AdminLevel] < && AutoPM[targetid][0] != 0)
    {
        
format(strsizeof(str), "(([%d]AutoPM from %s: %s))"targetidGetOOCName(targetid), AutoPM[targetid]);
        
SendClientMessage(playeridYELLOWstr);
        return 
1;
    }
    if(
Server[PMsStatus] == 0) return SendClientMessage(playeridGREY"PMs are disabled by an admin.");
    if(
strlen(message) < 1) return SendClientMessage(playeridGREY"Mesajul este prea scurt.");
    if(
strlen(message) > 100) return SendClientMessage(playeridGREY"Mesajul este prea lung.");
    
format(strsizeof(str), "(([%d]PM from %s: %s))"playeridGetOOCName(playerid), message);
    
SendClientMessage(targetidYELLOWstr);
    if(
PMsTracked[targetid] == 1)
    {
        
loop_start1:
        if(
IsPlayerConnected(i) == 1)
        {
            if(
MAX_PLAYERS)
            {
                goto 
script_continue;
            }
            if(
TrackPMs[i] == targetid)
            {
                
format(strsizeof(str), "((PM from %s to %s: %s))"GetOOCName(playerid), GetOOCName(targetid), message);
                
SendClientMessage(iYELLOWstr);
                
PlayerPlaySound(i10850,0);
                
i++;
                goto 
loop_start1;
            }
            else
            {
                
i++;
                goto 
loop_start1;
            }
        }
        else
        {
            
i++;
            goto 
loop_start1;
        }
    }
    if(
PMsTracked[playerid] == 1)
    {
        
loop_start2:
        if(
IsPlayerConnected(i) == 1)
        {
            if(
MAX_PLAYERS)
            {
                goto 
script_continue;
            }
            if(
TrackPMs[i] == playerid)
            {
                
format(strsizeof(str), "((PM from %s to %s: %s))"GetOOCName(playerid), GetOOCName(targetid), message);
                
SendClientMessage(iYELLOWstr);
                
PlayerPlaySound(i10850,0);
                
i++;
                goto 
loop_start2;
            }
            else
            {
                
i++;
                goto 
loop_start2;
            }
        }
        else
        {
            
i++;
            goto 
loop_start2;
        }
    }
    
script_continue:
    
PlayerPlaySound(targetid10850,0);
    
format(strsizeof(str), "(([%d]PM to %s: %s))"targetidGetOOCName(targetid), message);
    
PlayerPlaySound(playerid11370,0);
    
SendClientMessage(playeridYELLOWstr);
    
format(strsizeof(str), "%s PM'd %s: %s"GetOOCName(playerid), GetOOCName(targetid), message);
    
PMLog(str);
    return 
1;

Anyone here that can explain me this:
PHP код:
loop_start1:
        if(
IsPlayerConnected(i) == 1)
        {
            if(
MAX_PLAYERS)
            {
                goto 
script_continue;
            }
            if(
TrackPMs[i] == targetid)
            {
                
format(strsizeof(str), "((PM from %s to %s: %s))"GetOOCName(playerid), GetOOCName(targetid), message);
                
SendClientMessage(iYELLOWstr);
                
PlayerPlaySound(i10850,0);
                
i++;
                goto 
loop_start1;
            }
            else
            {
                
i++;
                goto 
loop_start1;
            }
        }
        else
        {
            
i++;
            goto 
loop_start1;
        } 
I know its a loop type of deal but i wonder how its work more precise. And if there is a alternative to this. For me look strange....
Reply
#2

Ew. Goto should never be used. It's bad practice, it leads to spaghetti code and it's a pain in the ass to debug. There's always a way to solve a problem without the need for goto. In this case it should just be a plain old for-loop.
Reply
#3

Is a goto loop. Is normally not recommended.
Just change it to a foreach loop like:
PHP код:
foreach(new iPlayers)
{
     if(
TrackPMs[i] == targetid)
    {
                
format(strsizeof(str), "((PM from %s to %s: %s))"GetOOCName(playerid), GetOOCName(targetid), message);
                
SendClientMessage(iYELLOWstr);
                
PlayerPlaySound(i10850,0); 
    }

The code looks more simple and cleaner.
Hope I helped you
Reply
#4

Quote:
Originally Posted by Vince
Посмотреть сообщение
Ew. Goto should never be used. It's bad practice, it leads to spaghetti code and it's a pain in the ass to debug. There's always a way to solve a problem without the need for goto. In this case it should just be a plain old for-loop.
Well, its hard to understand this loop to be honest. And im sure its hard to debug....So a for loop

Quote:
Originally Posted by 10MIN
Посмотреть сообщение
Is a goto loop. Is normally not recommended.
Just change it to a foreach loop like:
PHP код:
foreach(new iPlayers)
{
     if(
TrackPMs[i] == targetid)
    {
                
format(strsizeof(str), "((PM from %s to %s: %s))"GetOOCName(playerid), GetOOCName(targetid), message);
                
SendClientMessage(iYELLOWstr);
                
PlayerPlaySound(i10850,0); 
    }

The code looks more simple and cleaner.
Hope I helped you
Yeah, thanks, make me understand this weird loop. Never use/heard about goto loop to be honest
Reply
#5

Test
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)