Question about if else statements
#1

I was recently thinking about this, as it always confused me a bit when it came to coding (Usually as a whole, but more specifically sa-mp in this case).

If I had code like this:
PHP код:
if(IsPlayerConnected(playerid))
{
code blah blah blah
}
if(
IsPlayerAdmin(playerid))
{
Code blah blah blah

Would the sequence of else statements correspond to their position?

So the first else statement would be for is the player connected, and the second one for the isplayeradmin? Or how exactly does that work?

And then another question I've always wondered, coming from making edits on scripts that are very advanced for my coding level, is this:

When I have code like this:

PHP код:
if(IsPlayerConnected(playerid))
{
if(
IsACop(playerid)
{
if(
PlayerInfo[playerid] [pRank] < 5)
{
code blah blah blah

How does the else statement work in this situation, where does it go? Is it sequential like I think it is, or am I wrong in thinking that?

My thinking right now would be that the else for IsPlayerConnected comes first, then is a cop, then playerinfo. I'm just confused on how exactly the sequence goes because I want to restrict a privatemessage command to only admins and I don't want to mess it up in anyway.
Reply
#2

if(IsPlayerConnected(playerid))
{
if(PlayerInfo[playerid] [AdminLevel] >= 1)
{
code blah blah blah
}
}
Reply
#3

if(1happen this)
{
1happen this
if(2happen this)
{
2happen this
if(3happen this
{
3happen this
}
}
}

if not happen 1, happen 2 and 3 not happen
if happen 1 but not happen 2, happen 3 not happen
if happen 1 and happen 2, happen 3
Reply
#4

In your code, there is no "else" statement.
pawn Код:
// Check if the player is connected
if (IsPlayerConnected(playerid))
{
    // The player is connected, now check if he is a cop
    if (IsACop(playerid))
    {
        // Here the player is connected AND he's a cop, now we need to check if he's also an admin
        if (PlayerInfo[playerid][pRank] < 5)
        {
            // Code here to execute: the player is connected, he's a cop AND also is an admin
        }
        else
        {
            // The player is connected, AND he's a cop, but he's not an admin
        }
    }
    else
    {
        // The player is connected, but he's not a cop
    }
}
else
{
    // The player is not connected
}
I hope this will give some insights.

You could also do this:
pawn Код:
// If the player is NOT connected, exit the function
if (IsPlayerConnected(playerid) == 0) return 1;
// Here the player is connected, check if he's a cop now and exit the function if he's NOT a cop
if (IsACop(playerid) == 0) return 1;
// Here the player is connected AND a cop, now check if he's an admin (if not, exit the function)
if (PlayerInfo[playerid][pRank] < 5) return 1;

// Here the player is connected, he's a cop AND he's an admin, so execute the code for your pm system
Reply
#5

Something power did not mention is that you will always get confused if you don't properly ident your code. Seriously, it's not hard to press tab and makes it incredibly way easier to read. Compare power's code to the other posted codes including yours, and tell me which one is more understanble.

Ident your code and you will notice how it is trivial to make an else's sequences - which is after every closing curly bracket (also known as the end of an statement).
Reply
#6

Quote:
Originally Posted by PowerPC603
Посмотреть сообщение
In your code, there is no "else" statement.
pawn Код:
// Check if the player is connected
if (IsPlayerConnected(playerid))
{
    // The player is connected, now check if he is a cop
    if (IsACop(playerid))
    {
        // Here the player is connected AND he's a cop, now we need to check if he's also an admin
        if (PlayerInfo[playerid][pRank] < 5)
        {
            // Code here to execute: the player is connected, he's a cop AND also is an admin
        }
        else
        {
            // The player is connected, AND he's a cop, but he's not an admin
        }
    }
    else
    {
        // The player is connected, but he's not a cop
    }
}
else
{
    // The player is not connected
}
Okay, because what I was trying to do was make this command:
PHP код:
        if(strcmp(cmd"/pm"true) == || strcmp(cmd"/privatemessage"true) == 0//db
    
{
        if(
IsPlayerConnected(playerid))
        {
            if(
gPlayerLogged[playerid] == 0)
            {
                
SendClientMessage(playeridCOLOR_GREY"   You havent logged in yet !");
                return 
1;
            }
            
tmp strtok(cmdtextidx);
            if(!
strlen(tmp))
            {
                
SendClientMessage(playeridCOLOR_GRAD2"USAGE: (/p)rivate(m)essage [playerid/PartOfName] [message text]");
                return 
1;
            }
            
giveplayerid ReturnUser(tmp);
            if (
IsPlayerConnected(giveplayerid))
            {
                if(
giveplayerid != INVALID_PLAYER_ID)
                {
                    if(
HidePM[giveplayerid] > 0)
                    {
                        
SendClientMessage(playeridCOLOR_GREY"   That player is blocking PMs !");
                        return 
1;
                    }
                    
GetPlayerName(playeridsendernamesizeof(sendername));
                    
GetPlayerName(giveplayeridgiveplayersizeof(giveplayer));
                    if(
giveplayerid == playerid)
                    {
                        
format(stringsizeof(string), "* %s mutters somthing."sendername);
                        
ProxDetector(5.0playeridstringCOLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE);
                    }
                    new 
length strlen(cmdtext);
                    while ((
idx length) && (cmdtext[idx] <= ' '))
                    {
                        
idx++;
                    }
                    new 
offset idx;
                    new 
result[64];
                    while ((
idx length) && ((idx offset) < (sizeof(result) - 1)))
                    {
                        
result[idx offset] = cmdtext[idx];
                        
idx++;
                    }
                    
result[idx offset] = EOS;
                    if(!
strlen(result))
                    {
                        
SendClientMessage(playeridCOLOR_GRAD2"USAGE: (/p)rivate(m)essage [playerid/PartOfName] [message text]");
                        return 
1;
                    }
                    
format(stringsizeof(string), "%s(ID: %d) PMs:(( %s ))"sendernameplayerid, (result));
                    
SendClientMessage(giveplayeridCOLOR_ORANGEstring);
                    
format(stringsizeof(string), "PM sent to %s(ID: %d):(( %s ))"giveplayergiveplayerid, (result));
                    
SendClientMessage(playerid,  COLOR_ORANGEstring);
//                    PMLog(string[]);
                    
for(new i=0i<MAX_PLAYERSi++)
                    {
                        if(
BigEar[i])
                        {
                            
format(stringsizeof(string), "%s (%d) PMs to %s (%d): %s"sendernameplayeridgiveplayergiveplayerid, (result));
                            
SendClientMessage(iCOLOR_GREENstring);
                        }
                    }
                    return 
1;
                }
            }
            else
            {
                    
format(stringsizeof(string), "   %d is not an active player."giveplayerid);
                    
SendClientMessage(playeridCOLOR_GRAD1string);
            }
        }
        return 
1;
    } 
For use with admins only. So if I wanted to do this, could I just add in the line right after the IsPlayerConnected saying:

if(PlayerInfo[playerid] [pAdmin] >= 1)

And then continue on, or would I have to do more?
Reply
#7

If you use it like this:
pawn Код:
if(IsPlayerConnected(playerid))
{
    code blah blah blah
}
if(IsPlayerAdmin(playerid))
{
    Code blah blah blah
}
When the code is executed it will load from the top to the bottom, if the first statement is true the code thats inside the symbols "{" "}" will be loaded, if the statement is false it will skip it and continue with the rest of the code, if you add the function "return 1;" and if the code is true it will skip the rest of the code.

If you have it like this:
pawn Код:
if(IsPlayerConnected(playerid))
{
    if(IsACop(playerid)
    {
        if(PlayerInfo[playerid] [pRank] < 5)
        {
            code blah blah blah
        }
    }
}
Well obviously every statement has to be true to execute the code.
Reply
#8

Following the same structure:

Код:
if(gPlayerLogged[playerid] == 0) 
{ 
      SendClientMessage(playerid, COLOR_GREY, "   You havent logged in yet !"); 
      return 1; 
}
You can do

Код:
if(PlayerInfo[playerid] [pAdmin] == 0) 
{ 
      SendClientMessage(playerid, COLOR_GREY, "   You're not allowed to use this command!"); 
      return 1; 
}
Or even...

Код:
if(PlayerInfo[playerid] [pAdmin] == 0) 
     return SendClientMessage(playerid, COLOR_GREY, "   You're not allowed to use this command!");
Or different representations

Код:
if(!PlayerInfo[playerid] [pAdmin]) 
     return SendClientMessage(playerid, COLOR_GREY, "   You're not allowed to use this command!");
Код:
if(PlayerInfo[playerid] [pAdmin] < 1) 
     return SendClientMessage(playerid, COLOR_GREY, "   You're not allowed to use this command!");
You just need to read the code to figure it out; Also, you're checking if the player is not connected then if the player is not an INVALID_PLAYER_ID, which is redundant because... well, a connected player can't be an invalid player id.
Reply
#9

if(IsPlayerConnected(playerid)) don't need it obviously they're connected if they typed the command.
Reply
#10

if(PlayerInfo[playerid] [pAdmin] < 1) return SendClientMessage(playerid, COLOR_GREY, "You Need Admin Level 1 To use this command.");
is more easy
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)