Raised This Month: $12 Target: $400
 3% 

Unreachable code


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Mankled
Senior Member
Join Date: Oct 2019
Old 10-29-2020 , 00:54   Unreachable code
Reply With Quote #1

getting unreachable code on line 4854, how do i fix that ?

PHP Code:
public menu_mode(idmenuiditem)
{
    
// Player wants to exit the menu
    
if (item == MENU_EXIT)
    {
        
menu_destroy(menuid)
        
show_menu3_admin(id)
        return 
PLUGIN_HANDLED;
    }
    
    
// Create some necassary vars
    
static buffer[2], dummy gameid
    
    
// Retrieve the id of the game mode which was chosen
    
menu_item_getinfo(menuiditemdummybuffercharsmax(buffer), __dummy)
    
gameid buffer[0]
    
    
// Check users access level
    
if (get_user_flags(id) & ArrayGetCell(g_gamemode_flag, (gameid MAX_GAME_MODES)))
    {
        
// Only allow the game mode to proceed after some checks
        
if (allowed_custom_game())
            
command_custom_game(gameidid)
        else
            
zp_colored_print(id"^x04[ZP]^x01 %L"id"CMD_NOT")
            
        
// Show the menu again
        
show_menu_game_mode(id)
        return 
PLUGIN_HANDLED;
    }
    else
    {
        
// Player deosnt haves the required access level
        
zp_colored_print(id"^x04[ZP]^x01 %L"id"CMD_NOT_ACCESS")
        
        
// Show the menu again
        
show_menu_game_mode(id)
        return 
PLUGIN_HANDLED;
    }
    
    
menu_destroy(menuid)
    return 
PLUGIN_HANDLED;

line 4854
PHP Code:
menu_destroy(menuid
if i remove this line, the error goes to the next line "return PLUGIN_HANDLED;"


And Im having this problem in this line as well:
PHP Code:
public setview(idkeymenu)
{
    if(
key == 0) {
         
set_view(idCAMERA_3RDPERSON)
         return 
PLUGIN_HANDLED
    
}

    if(
key == 1) {
         
set_view(idCAMERA_TOPDOWN)
         return 
PLUGIN_HANDLED
    
}

    if(
key == 2) {
         
set_view(idCAMERA_NONE)
         return 
PLUGIN_HANDLED
    
}

    else {
         return 
PLUGIN_HANDLED
    
}

    return 
PLUGIN_HANDLED

line 52 ( the last PLUGIN_HANDLED)
PHP Code:
return PLUGIN_HANDLED 

Last edited by Mankled; 10-29-2020 at 02:11.
Mankled is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 10-29-2020 , 07:34   Re: Unreachable code
Reply With Quote #2

Don't return from every single branch, it's completely useless here.

if code return else code return
return

is the same thing as
if code else code
return
__________________

Last edited by HamletEagle; 10-29-2020 at 07:34.
HamletEagle is online now
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 10-29-2020 , 07:38   Re: Unreachable code
Reply With Quote #3

Delete this
Code:
else {          return PLUGIN_HANDLED     }
__________________








CrazY. is offline
Mankled
Senior Member
Join Date: Oct 2019
Old 10-29-2020 , 12:22   Re: Unreachable code
Reply With Quote #4

Quote:
Originally Posted by CrazY. View Post
Delete this
Code:
else {          return PLUGIN_HANDLED     }
it worked. and for this ?

PHP Code:
public menu_mode(idmenuiditem)
{
    
// Player wants to exit the menu
    
if (item == MENU_EXIT)
    {
        
menu_destroy(menuid)
        
show_menu3_admin(id)
        return 
PLUGIN_HANDLED;
    }
    
    
// Create some necassary vars
    
static buffer[2], dummy gameid
    
    
// Retrieve the id of the game mode which was chosen
    
menu_item_getinfo(menuiditemdummybuffercharsmax(buffer), __dummy)
    
gameid buffer[0]
    
    
// Check users access level
    
if (get_user_flags(id) & ArrayGetCell(g_gamemode_flag, (gameid MAX_GAME_MODES)))
    {
        
// Only allow the game mode to proceed after some checks
        
if (allowed_custom_game())
            
command_custom_game(gameidid)
        else
            
zp_colored_print(id"^x04[ZP]^x01 %L"id"CMD_NOT")
            
        
// Show the menu again
        
show_menu_game_mode(id)
        return 
PLUGIN_HANDLED;
    }
    else
    {
        
// Player deosnt haves the required access level
        
zp_colored_print(id"^x04[ZP]^x01 %L"id"CMD_NOT_ACCESS")
        
        
// Show the menu again
        
show_menu_game_mode(id)
        return 
PLUGIN_HANDLED;
    }
    
    
menu_destroy(menuid)
    return 
PLUGIN_HANDLED;

Mankled is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 10-29-2020 , 13:04   Re: Unreachable code
Reply With Quote #5

Code:
public menu_mode(id, menuid, item) {     // Player wants to exit the menu     if (item == MENU_EXIT)     {         menu_destroy(menuid)         show_menu3_admin(id)         return PLUGIN_HANDLED;     }         // Create some necassary vars     static buffer[2], dummy , gameid         // Retrieve the id of the game mode which was chosen     menu_item_getinfo(menuid, item, dummy, buffer, charsmax(buffer), _, _, dummy)     gameid = buffer[0]     menu_destroy(menuid)         // Check users access level     if (get_user_flags(id) & ArrayGetCell(g_gamemode_flag, (gameid - MAX_GAME_MODES)))     {         // Only allow the game mode to proceed after some checks         if (allowed_custom_game())             command_custom_game(gameid, id)         else             zp_colored_print(id, "^x04[ZP]^x01 %L", id, "CMD_NOT")                     // Show the menu again         show_menu_game_mode(id)     }     else     {         // Player deosnt haves the required access level         zp_colored_print(id, "^x04[ZP]^x01 %L", id, "CMD_NOT_ACCESS")                 // Show the menu again         show_menu_game_mode(id)     }         return PLUGIN_HANDLED; }
__________________








CrazY. is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-29-2020 , 22:37   Re: Unreachable code
Reply With Quote #6

Quote:
Originally Posted by CrazY. View Post
Delete this
Code:
else {          return PLUGIN_HANDLED     }
All returns for the second code should be removed except for the last one (as stated by Hamlet).

Also, this particular code should be implemented with a switch() instead of multiple consecutive if statements.
__________________
fysiks is offline
Mankled
Senior Member
Join Date: Oct 2019
Old 10-29-2020 , 23:01   Re: Unreachable code
Reply With Quote #7

Quote:
Originally Posted by CrazY. View Post
Code:
public menu_mode(id, menuid, item) {     // Player wants to exit the menu     if (item == MENU_EXIT)     {         menu_destroy(menuid)         show_menu3_admin(id)         return PLUGIN_HANDLED;     }         // Create some necassary vars     static buffer[2], dummy , gameid         // Retrieve the id of the game mode which was chosen     menu_item_getinfo(menuid, item, dummy, buffer, charsmax(buffer), _, _, dummy)     gameid = buffer[0]     menu_destroy(menuid)         // Check users access level     if (get_user_flags(id) & ArrayGetCell(g_gamemode_flag, (gameid - MAX_GAME_MODES)))     {         // Only allow the game mode to proceed after some checks         if (allowed_custom_game())             command_custom_game(gameid, id)         else             zp_colored_print(id, "^x04[ZP]^x01 %L", id, "CMD_NOT")                     // Show the menu again         show_menu_game_mode(id)     }     else     {         // Player deosnt haves the required access level         zp_colored_print(id, "^x04[ZP]^x01 %L", id, "CMD_NOT_ACCESS")                 // Show the menu again         show_menu_game_mode(id)     }         return PLUGIN_HANDLED; }
Thank you so much, all the codes worked.

Quote:
Originally Posted by fysiks View Post
All returns for the second code should be removed except for the last one (as stated by Hamlet).

Also, this particular code should be implemented with a switch() instead of multiple consecutive if statements.
How I do with switch()?
Mankled is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-30-2020 , 21:56   Re: Unreachable code
Reply With Quote #8

Quote:
Originally Posted by Mankled View Post
How I do with switch()?
Code:
switch( var )
{
	case 1:
	{
		// var is 1
	}
	case 2:
	{
		// var is 2
	}
	case 3, 4:
	{
		// var is 3 or 4
	}
	default:
	{
		// Runs for any value that is not part of a case above
	}
}
__________________
fysiks is offline
Mankled
Senior Member
Join Date: Oct 2019
Old 10-30-2020 , 23:37   Re: Unreachable code
Reply With Quote #9

Quote:
Originally Posted by fysiks View Post
Code:
switch( var )
{
	case 1:
	{
		// var is 1
	}
	case 2:
	{
		// var is 2
	}
	case 3, 4:
	{
		// var is 3 or 4
	}
	default:
	{
		// Runs for any value that is not part of a case above
	}
}
I did it and it compiled, could u just check if the code is fine?
PHP Code:
public setview(idkeymenu)
{
    switch( 
key )
    {
        case 
0:
        {
            
set_view(idCAMERA_3RDPERSON)
        }
        case 
1:
        {
            
set_view(idCAMERA_TOPDOWN)
        }
        case 
2:
        {
            
set_view(idCAMERA_NONE)
        }
    }
    return 
PLUGIN_HANDLED

And I just changed to start from case 0, cus I want the key 1 to work

Last edited by Mankled; 10-30-2020 at 23:41.
Mankled is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-31-2020 , 00:49   Re: Unreachable code
Reply With Quote #10

That should work.
__________________
fysiks is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:13.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode