Simple Question.
#1

I have be thinking, i use

pawn Код:
if
and

pawn Код:
else and else if
When i use them they work normal but i need to know the correct way to use it.
Reply
#2

I cant explain it but I can show you so you can have an idea. Post a code that you made that you think that uses else or else if or if that is wrong.
Reply
#3

ok here:

pawn Код:
COMMAND:cuff(playerid, params[]) // or CMD:mycommand(playerid, params[])
{
    new id,pskin = GetPlayerSkin(playerid),Float:x, Float:y, Float:z;
    if(pskin == 265 || pskin == 266 || pskin == 267 || pskin == 280 || pskin == 281 || pskin == 284 || pskin == 285 || pskin == 286 || pskin == 287 || pskin == 288)
    if(sscanf(params, "u",id)) SendClientMessage(playerid, 0xFF0000AA, "Usage: /cuff [id]");
    if (id == INVALID_PLAYER_ID) SendClientMessage(playerid, 0xFF0000AA, "Player not found");
    else if(GetPlayerSkin(playerid))return SendClientMessage(playerid,0xFF0000FF,"ERROR: You dont have permisson to access this command.");
    GetPlayerPos(id,x,y,z);
    if(!IsPlayerInRangeOfPoint(id, 3, x, y, z))return SendClientMessage(playerid, 0xFF0000FF, "**You are not close enough to that player to cuff him.");
    else if (IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: You cannot ues this while in a vehicle.");
    else if(TogglePlayerControllable(id,0))return SendClientMessage(id,0x8080FFFF,"**You have been restrained in handcuffs.");
    Cuff[id] = 1;
    {
    GetPlayerPos(id,x,y,z);
    }
    return 1;
}
Reply
#4

I show in a few, damn forums fucked up my post...
Reply
#5

pawn Код:
COMMAND:cuff(playerid, params[]) // or CMD:mycommand(playerid, params[])
{
    new id,pskin = GetPlayerSkin(playerid),Float:x, Float:y, Float:z;
    if(pskin == 265 || pskin == 266 || pskin == 267 || pskin == 280 || pskin == 281 || pskin == 284 || pskin == 285 || pskin == 286 || pskin == 287 || pskin == 288) { //changed - added a brace '{'
    /* needs indention */ if(sscanf(params, "u",id)) SendClientMessage(playerid, 0xFF0000AA, "Usage: /cuff [id]");
    if (id == INVALID_PLAYER_ID) SendClientMessage(playerid, 0xFF0000AA, "Player not found");
    else if(GetPlayerSkin(playerid))return SendClientMessage(playerid,0xFF0000FF,"ERROR: You don;t have permisson to access this command."); //Delete this whole line its not needed since you have getplayerskin up there.
    GetPlayerPos(id,x,y,z); //This is ok.
    if(!IsPlayerInRangeOfPoint(id, 3, x, y, z))return SendClientMessage(playerid, 0xFF0000FF, "**You are not close enough to that player to cuff him."); //This is ok too.
    if (IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: You cannot use this while in a vehicle."); //Changed - else if (isplayerinanyvehicle(playerid) wont work. Use as it is posted.
    TogglePlayerControllable(id,0); SendClientMessage(id,0x8080FFFF,"**You have been restrained in handcuffs."); //This is epic fail line (no offense) else if(toggleplayercontrollable - fails terrible, use as i post.
    Cuff[id] = 1;
    } else return SendClientMessage(playerid,YOURCOLOR,"Sorry, you can not used this command."); //This is for the GetPlayerSkin at top, if they don't have them skins, it will send this message.
    return 1;
}
Try to learn from that, I realize that it looks a mess and looks like wont help at all, it should help a little, would have better post but forums fucked my first post so i made it shorter and lazier.
Reply
#6

If you tested it you would see that it is messed up, but i will try it.
Reply
#7

Use 'if' when you do checks.
Use 'else if' if you want to check something else, if the previous 'else if' or 'if' doesn't meet the conditions
Use 'else' if you want to end the series of checks. If you add a new 'if' after 'else', it will be executed only IF the 'else' statement is called.
Reply
#8

And why do i need the indentation, because when i use it like that i have to indent the rest of lines below it.
Reply
#9

Indentation is mostly for the overview. If you got large scripts with lots of functions and ifs, and you do not indent them, you will experience a great fail when searching for something

But if you indent correctly, you can easily differ what part of the code belongs to a specific 'if' or 'else'

a small Example, Im sure you will see the differecne
no indentation:
pawn Код:
for(new i = 0; i < INV_slots_allowed[playerid]; i ++) {
if(pInventory[playerid][i][ItemID] == INVALID_ITEM_ID) {
format(text, sizeof(text), "%s - Empty space -\r\n", text);
count += 2;
} else {
format(text, sizeof(text), "%s%s - Amount: %d\r\n", text, name, amount);
count += 3;
}
}
normal indentation:
pawn Код:
for(new i = 0; i < INV_slots_allowed[playerid]; i ++)
{
    if(pInventory[playerid][i][ItemID] == INVALID_ITEM_ID)
    {
        format(text, sizeof(text), "%s - Empty space -\r\n", text);
        count += 2;
    } else
    {
        format(text, sizeof(text), "%s%s - Amount: %d\r\n", text, name, amount);
        count += 3;
    }
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)