Unreachable code fix? +REP
#1

Why is this an unreachable code?
Код:
if (strcmp("/closetank47", cmdtext, false, 10) == 0)
	{
		if(IsPlayerInRangeOfPoint(playerid, 2, 652.20001220703, -1506.3000488281, 23.5))
		{
		    SendClientMessage(playerid, 0x10F441AA, "Tank Closed!");
		    Create3DTextLabel("Tank Closed!", 0x10F441AA, 658.70001220703, -1507.5999755859, 22, 40.0, 0, 1);
		    return 1;
		}
		else
		{
		    SendClientMessage(playerid, 0xAFAFAFAA, "Get Closer to the Tank!");
		    return 1;
		}
		return 1;
	}
Im not asking anyone to fix it for me im asking whats the problem with it. (but fixing it for me is fine also.)
Reply
#2

pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if (strcmp("/closetank47", cmdtext, true, 10) == 0)
    {
        if(IsPlayerInRangeOfPoint(playerid, 2, 652.20001220703, -1506.3000488281, 23.5))
        {
            SendClientMessage(playerid, 0x10F441AA, "Tank Closed!");
            Create3DTextLabel("Tank Closed!", 0x10F441AA, 658.70001220703, -1507.5999755859, 22, 40.0, 0, 1);
        }
        else
        {
            SendClientMessage(playerid, 0xAFAFAFAA, "Get Closer to the Tank!");
        }
        return 1;
    }
    return 1;
}
try this
Reply
#3

Remove "return 1;" under Create3DText....


Edit: too late
Reply
#4

pawn Код:
if (strcmp("/closetank47", cmdtext, true, 10) == 0)
    {
        if(IsPlayerInRangeOfPoint(playerid, 2, 652.20001220703, -1506.3000488281, 23.5))
        {
            SendClientMessage(playerid, 0x10F441AA, "Tank Closed!");
            Create3DTextLabel("Tank Closed!", 0x10F441AA, 658.70001220703, -1507.5999755859, 22, 40.0, 0, 1);
            return 1;
        }
        else
        {
            SendClientMessage(playerid, 0xAFAFAFAA, "Get Closer to the Tank!");
            return 1;
        }
        return 1;
    }
Try it now...
Reply
#5

What you did wrong.
pawn Код:
if(strcmp("/closetank47", cmdtext, false, 10) == 0)
{
    // if the player is in range of the point
    if(IsPlayerInRangeOfPoint(playerid, 2, 652.20001220703, -1506.3000488281, 23.5))
    {
        // this code will be ran
        SendClientMessage(playerid, 0x10F441AA, "Tank Closed!");
        Create3DTextLabel("Tank Closed!", 0x10F441AA, 658.70001220703, -1507.5999755859, 22, 40.0, 0, 1);
       
        // and returning 1 here stops the rest of the command from processing
        return 1;
    }
    // if the player is not in range of the point
    else
    {
        // this code is ran
        SendClientMessage(playerid, 0xAFAFAFAA, "Get Closer to the Tank!");
        // and returning 1 here stops the code
        return 1;
    }
    // since both statements stop the rest of the command
    // the code below will never be reachable, that explains the "unreachable code" error.
    return 1;
}
So the correct way would be.
pawn Код:
if(strcmp("/closetank47", cmdtext, false, 10) == 0)
{
    if(IsPlayerInRangeOfPoint(playerid, 2, 652.20001220703, -1506.3000488281, 23.5))
    {
        SendClientMessage(playerid, 0x10F441AA, "Tank Closed!");
        Create3DTextLabel("Tank Closed!", 0x10F441AA, 658.70001220703, -1507.5999755859, 22, 40.0, 0, 1);
        return 1;
    }
    else
    {
        SendClientMessage(playerid, 0xAFAFAFAA, "Get Closer to the Tank!");
    }
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)