Skip to content

How to Make a Menu with 10+ Items

Would you like to put more than 10 items in a menu on one page, with the ability to pick an item without binding any extra keys? We will create a menu with 9 items, which we add with menu_additem, and the remaining items with menu_addtext. To catch a press of item 10+ we will create a delay of…

Coding·1 min read·PolarhighViews:16

Would you like to put more than 10 items in a menu on one page, with the ability to pick an item without binding any extra keys?

We will create a menu with 9 items, which we add with menu_additem, and the remaining items with menu_addtext.
To catch a press of item 10+, we will create a delay of 0.2 sec. (a cvar) during which we wait for another key press.
For example, if we first press 2, then 1, it counts as menu item 21.
And if no other key (1 in our example) is pressed within 0.2 seconds, menu item 2 is selected.

Here is an example:

Pawn
#include <amxmodx> 

new g_fButton[ 33 ] 
new g_sButton[ 33 ] 
new g_Task[ 33 ] 

new Float:g_fTime[ 33 ] 

new g_pWait, Float:WAIT 

public plugin_init()  
{ 
    register_plugin( "How to make more options in one page", "1.0", "eMinEm" ) 
     
    register_clcmd( "say /menu", "catch_buttons" ) 
     
    g_pWait = register_cvar( "menu_waittime", "0.2" )  
    WAIT = get_pcvar_float( g_pWait ) 
} 

public client_putinserver( id ) null_all( id ); 

public catch_buttons( id ) 
{ 
    if( g_Task[ id ] ) 
    { 
        if( g_fButton[ id ] != 0 ) set_buttons( id, get_gametime( ) ); 
        else null_all( id ); 
         
        show_menu( id, 0, "^n" ) 
        return PLUGIN_HANDLED 
    } 
    new menu = menu_create( "Menu", "catch_buttons_handler" ) 
     
        /*    First type of formatting the menu    */ 
    /* 
    menu_additem( menu, "Option1     \r10. \wOption 10     \r19. \wOption 19", "1", 0 ); 
    menu_additem( menu, "Option2     \r11. \wOption 11     \r20. \wOption 20", "2", 0 ); 
    menu_additem( menu, "Option3     \r12. \wOption 12     \r21. \wOption 21", "3", 0 ); 
    menu_additem( menu, "Option4     \r13. \wOption 13     \r22. \wOption 22", "4", 0 ); 
    menu_additem( menu, "Option5     \r14. \wOption 14     \r23. \wOption 23", "5", 0 ); 
    menu_additem( menu, "Option6     \r15. \wOption 15     \r24. \wOption 24", "6", 0 ); 
    menu_additem( menu, "Option7     \r16. \wOption 16", "7", 0 ); 
    menu_additem( menu, "Option8     \r17. \wOption 17", "8", 0 ); 
    menu_additem( menu, "Option9     \r18. \wOption 18", "9", 0 ); 
    */ 
     
        /*    Second type of formatting the menu    */ 
         
    menu_additem( menu, "Option1", "1" ); 
    menu_additem( menu, "Option1", "2" ); 
    menu_additem( menu, "Option1", "3" ); 
    menu_additem( menu, "Option1", "4" ); 
    menu_additem( menu, "Option1", "5" ); 
    menu_additem( menu, "Option1", "6" ); 
    menu_additem( menu, "Option1", "7" ); 
    menu_additem( menu, "Option1", "8" ); 
    menu_additem( menu, "Option1", "9" ); 
    menu_addtext( menu, "\r10. \wOption10", 0 ); 
    menu_addtext( menu, "\r11. \wOption11", 0 ); 
    menu_addtext( menu, "\r12. \wOption12", 0 ); 
    menu_addtext( menu, "\r13. \wOption13", 0 ); 
    menu_addtext( menu, "\r14. \wOption14", 0 ); 
    menu_addtext( menu, "\r15. \wOption15", 0 ); 
    menu_addtext( menu, "\r16. \wOption16", 0 ); 
    menu_addtext( menu, "\r17. \wOption17", 0 ); 
    menu_addtext( menu, "\r18. \wOption18", 0 ); 
    menu_addtext( menu, "\r19. \wOption19", 0 ); 
    menu_addtext( menu, "\r20. \wOption20", 0 ); 
    menu_addtext( menu, "\r21. \wOption21", 0 ); 
    menu_addtext( menu, "\r22. \wOption22", 0 ); 
    menu_addtext( menu, "\r23. \wOption23", 0 ); 
    menu_addtext( menu, "\r24. \wOption24", 0 ); 

    menu_addblank( menu, 0 ) 
    menu_additem( menu, "Exit", "0" );  
     
    menu_setprop( menu, MPROP_PERPAGE, 0 ); 
     
    menu_display( id, menu, 0 ); 
    return PLUGIN_HANDLED 
} 

public catch_buttons_handler( id, menu, item ) 
{ 
    new data[ 6 ], szName[ 64 ] 
    new access, callback; 
     
    menu_item_getinfo( menu, item, access, data,charsmax( data ), szName,charsmax( szName ), callback ); 
     
    new key = str_to_num( data ); 
    new Float:iTime = get_gametime( ) 
     
    if( task_exists( id ) ) remove_task( id );  
     
    g_fButton[ id ] < 0 ? ( g_fButton[ id ] = key ) : ( g_sButton[ id ] = key );  
    g_fTime[ id ] = iTime 
    set_buttons( id, iTime ) 
     
    menu_destroy( menu ) 
    return PLUGIN_HANDLED 
} 

set_buttons( id, Float:iTime ) 
{ 
    if( g_sButton[ id ] < 0 ) 
    { 
        if( iTime - g_fTime[ id ] <= WAIT ) 
        { 
            catch_buttons( id ); 
            g_Task[ id ] = 1 
            set_task( WAIT, "catch_buttons", id ) 
        } 
        else  
        { 
            exec_selected( id, g_fButton[ id ], -1 ) 
            null_all( id ) 
        } 
    } 
    else  
    { 
        exec_selected( id, g_fButton[ id ], g_sButton[ id ] ) 
        null_all( id ) 
    } 
} 

null_all( id ) 
{ 
    g_fButton[ id ] = -1 
    g_sButton[ id ] = -1 
    g_fTime[ id ] = 0.0 
    g_Task[ id ] = 0 
} 

exec_selected( id, fslot, sslot ) 
{ 
    new szChoice[ 3 ], iChoice 
     
    sslot < 0 ? formatex( szChoice, charsmax( szChoice ), "%d", fslot ) : formatex( szChoice, charsmax( szChoice ), "%d%d", fslot, sslot ); 
     
    iChoice = str_to_num( szChoice ) 
    switch( iChoice ) 
    { 
        case 1:     client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 2:     client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 3:     client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 4:     client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 5:     client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 6:     client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 7:     client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 8:     client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 9:     client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 10: client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 11: client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 12: client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 13: client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 14: client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 15: client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 16: client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 17: client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 18: client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 19: client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 20: client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 21: client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 22: client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 23: client_print( id, print_chat, "Selected item : %d", iChoice ); 
        case 24: client_print( id, print_chat, "Selected item : %d", iChoice ); 
         
        default:  client_print( id, print_center, "Item : %d does not exist", iChoice );  
    } 
}

Translator's note: I really didn't feel like translating the comments; if you really want me to, say so in the comments and I'll add the comments to the code (tautology, level 80)
Translation: Polarhigh
Source: https://forums.alliedmods.net/showthread.php?t=210686