How to: Menu with 10+ Options in one page

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1367
Gender:
United States of America

How to: Menu with 10+ Options in one page

Post by Archive-Poster »

Did you ever wonder how to make a menu with more then 10 options in one page , and you can select any of them without force players to bind keys?

We will create menu with 9 options and add more with addtext, then we will catch which slot the player has pressed and wait 0.2( Cvar ) seconds to press one more .
If he press second slot in this time it will execute (if first choose was 2 and second 1 ) - 21
Else the first chosen option will be executed in 0.2 seconds

Here is the code with comments ( hope i did well with them )

Code: Select all

#include <amxmodx>

new g_fButton[ 33 ] // Here we will store the first slot pressed by the player
new g_sButton[ 33 ] // Here we will store the second slot pressed by the player
new g_Task[ 33 ] // Here we will store if there was a task when opening the menu

new Float:g_fTime[ 33 ] // Here we will store the time when player pressed slot1

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" ) // register command for opening the menu
    
    g_pWait = register_cvar( "menu_waittime", "0.2" ) 
    // How much to wait for chosing options from the menu
    // Note : If u make it below 0.2 a lot of mistakes can happen when players use the menu
    //       If u make it above 0.4 there will be too much delay when using slot 1-9
    
    WAIT = get_pcvar_float( g_pWait ) // store the time to wait in global variable so we can use it for our checks
}

public client_putinserver( id ) null_all( id ); // Make sure the player has the right numbers stored

public catch_buttons( id )
{
    if( g_Task[ id ] ) // if slot is pressed but there is no second in the given time execute the first chosen slot ( from 0 to 9 )
    {
        if( g_fButton[ id ] != 0 ) set_buttons( id, get_gametime( ) );
        else null_all( id ); // make sure to null all checks so players can open the menu again
        
        show_menu( id, 0, "^n" ) // Hide the menu after the item is executed or if item is exit
        return PLUGIN_HANDLED
    }
    new menu = menu_create( "Menu", "catch_buttons_handler" ) // Create the menu
    
            /*    Maximum 383 symbols including menu title    */
    
        /*    First type of formatting the menu    */
// Doesnt really matter what you will put in the options , you just want to show players what will happen if they press "XX"

    /*
    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 );
    */
    // If you want more options use addtext as in the example below
    
        /*    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 ) // Make space between the menu options and Exit
    menu_additem( menu, "Exit", "0" ); // Add exit button to the menu
// If we don't add Exit on 0 slot and we don't add 0 slot in our menu, players whould not be able to select items with 0 in it ( like 10, 20.. )
    
    menu_setprop( menu, MPROP_PERPAGE, 0 ); // No EXIT option in the menu
    
    menu_display( id, menu, 0 ); // Show the menu to the player
    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 ); 
    // If first slot is already chosen a task is waiting for the second , we want to remove it when we press the second slot
    
    g_fButton[ id ] < 0 ? ( g_fButton[ id ] = key ) : ( g_sButton[ id ] = key ); // Check which button we want to store , first or second one
    g_fTime[ id ] = iTime // Set the time when the slot is pressed
    set_buttons( id, iTime )
    
    menu_destroy( menu )
    return PLUGIN_HANDLED
}

set_buttons( id, Float:iTime )
{
    if( g_sButton[ id ] < 0 ) // If no second slot pressed yet
    {
        if( iTime - g_fTime[ id ] <= WAIT ) // Check if there is time to press second one
        {
            catch_buttons( id ); // Show him the menu to press the second one
            g_Task[ id ] = 1 // We now know that there is a task set
            set_task( WAIT, "catch_buttons", id ) // Show the menu again
        }
        else 
        {
            exec_selected( id, g_fButton[ id ], -1 ) // If no second button pressed execute the first one
            null_all( id )
        }
    }
    else 
    {
        exec_selected( id, g_fButton[ id ], g_sButton[ id ] ) // If there is a second button pressed execute selection
        null_all( id )
    }
}

null_all( id )
{
    g_fButton[ id ] = -1 // We want to use slot 0 too
    g_sButton[ id ] = -1 // Same as above
    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 );
    // Check if we have second slot chosen , if yes format them both , if not format only the first one
    
    iChoice = str_to_num( szChoice )
    
    // Here is what will actually happen when player choose something from the menu
    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 ); 
        // If player choose slot above 24( or how much we add ), tell him that this item does not exist
    }
} 
And here it is without comments :

Code: Select all

#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 ); 
    }
} 
I don't know if this can be useful to someone , but i like it and decided to share it with you.

Who is online

Users browsing this forum: No registered users and 1 guest