I'm very CONFUSED right now.. about else
#1

Well, I've been scripting my GM with YSI without any problems. But now I started to think about how 'else' must be scripted.

pawn Код:
Command_(kick)
{
if (help)
{
Text_Send(playerid, "KICK_HELP");
Text_SendFormat(playerid, "KICK_HELP1", "kick");
}
else
{
if (sscanf(params, "u", player))
{
Text_Send(playerid, "KICK_HELP");
Text_SendFormat(playerid, "KICK_HELP1", "kick");
}
else
{
if (IsPlayerConnected(player))
{
Kick(player);
Text_SendFormat(player, "YOURE_KICKED", ReturnPlayerName(playerid), playerid);
Text_SendToAllFormat("THEYRE_KICKED", ReturnPlayerName(player), player, ReturnPlayerName(playerid), playerid);
}
else
{
Text_SendFormat(playerid, "NOT_CONNECTED", player);
}
}
}
return 1;
}
Can someone explain me the WHY I have to put else instead of else if
Reply
#2

Wiki explains it.
Reply
#3

I've been searching for it, no results.
Reply
#4

https://sampwiki.blast.hk/wiki/Keywords:Statements#else
Reply
#5

That means that this code:
pawn Код:
Command_(kick)
{
if (help)
{
Text_Send(playerid, "KICK_HELP");
Text_SendFormat(playerid, "KICK_HELP1", "kick");
}
else
{
if (sscanf(params, "u", player))
{
Text_Send(playerid, "KICK_HELP");
Text_SendFormat(playerid, "KICK_HELP1", "kick");
}
else
{
if (IsPlayerConnected(player))
{
Kick(player);
Text_SendFormat(player, "YOURE_KICKED", ReturnPlayerName(playerid), playerid);
Text_SendToAllFormat("THEYRE_KICKED", ReturnPlayerName(player), player, ReturnPlayerName(playerid), playerid);
}
else
{
Text_SendFormat(playerid, "NOT_CONNECTED", player);
}
}
}
return 1;
}
Must be

pawn Код:
Command_(kick)
{
if (help)
{
Text_Send(playerid, "KICK_HELP");
Text_SendFormat(playerid, "KICK_HELP1", "kick");
}
else
{
}
if (sscanf(params, "u", player))
{
Text_Send(playerid, "KICK_HELP");
Text_SendFormat(playerid, "KICK_HELP1", "kick");
}
else if (IsPlayerConnected(player))
{
Kick(player);
Text_SendFormat(player, "YOURE_KICKED", ReturnPlayerName(playerid), playerid);
Text_SendToAllFormat("THEYRE_KICKED", ReturnPlayerName(player), player, ReturnPlayerName(playerid), playerid);
}
else
{
Text_SendFormat(playerid, "NOT_CONNECTED", player);
}
}
}
return 1;
}
( just look at the elses )
Reply
#6

else works like this:

pawn Код:
if(blabla == 1)
{
  // it's 1
}
else
{
  // it's not 1
}
else if works like this:

pawn Код:
if(blabla == 1)
{
  // it's 1
}
else if(blabla == 3)
{
  // it's 3
}
else if(blabla == 5)
{
  // it's 5
}
else if is similar to switch().

if 'blabla' was 5 and you did this:

if(blabla == 2)
{
// it's 2
}
else
{
if(blabla == 1)
{
// it will still get here.
}
else if(bblabla == 3)
{
// and here
}
}
Reply
#7

pawn Код:
Command_(kick)
{
if (help)
{
Text_Send(playerid, "KICK_HELP");
Text_SendFormat(playerid, "KICK_HELP1", "kick");
}
else
{
if (sscanf(params, "u", player))
{
Text_Send(playerid, "KICK_HELP");
Text_SendFormat(playerid, "KICK_HELP1", "kick");
}
else
{
if (IsPlayerConnected(player))
{
Kick(player);
Text_SendFormat(player, "YOURE_KICKED", ReturnPlayerName(playerid), playerid);
Text_SendToAllFormat("THEYRE_KICKED", ReturnPlayerName(player), player, ReturnPlayerName(playerid), playerid);
}
else
{
Text_SendFormat(playerid, "NOT_CONNECTED", player);
}
}
}
return 1;
}
When I add this:
pawn Код:
else
{
if (sscanf(params, "u", player))
{
Text_Send(playerid, "KICK_HELP");
Text_SendFormat(playerid, "KICK_HELP1", "kick");
}
else
{
if (IsPlayerConnected(player))
{
Means that it also checks if the player is connected? And also adds/checks for sscanf?

I also saw this:
ex.

if(iamadmin)
{
Text_Send(playerid, iamadmin);
{
if(playerisconnected)
Kick(playerid);
}
}
return 1;
}

So you have open brackets. What does that mean?
Reply
#8

Код:
}
else <== the else
{
if (sscanf(params, "u", player)) <=== if at start
{
If im right thats a "class" of else if with the else and the if,

Im not shure but what i know its just the same
else + else if i think realy are the same
Reply
#9

pawn Код:
Command_(kick)
{
    if (help)
    {
        Text_Send(playerid, "KICK_HELP");
        Text_SendFormat(playerid, "KICK_HELP1", "kick");
    }
    else
    {
    }
    if (sscanf(params, "u", player))
    {
        Text_Send(playerid, "KICK_HELP");
        Text_SendFormat(playerid, "KICK_HELP1", "kick");
        {
        if (IsPlayerConnected(player))
        {
            Kick(player);
            Text_SendFormat(player, "YOURE_KICKED", ReturnPlayerName(playerid), playerid);
            Text_SendToAllFormat("THEYRE_KICKED", ReturnPlayerName(player), player, ReturnPlayerName(playerid), playerid);
        }
    }
    }
    else
    {
    Text_SendFormat(playerid, "NOT_CONNECTED", player);
    }
    return 1;
}
Would this be good?
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)