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 )
PHP Code:
#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_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;
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
}
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 :
Spoiler
PHP Code:
#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" )
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.
I'm open to criticism and ideas, so feel free to post here.
Also if you think something can be done better or this method is not good, feel free to trash my post ;)
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
The only downside to accepting 1 and 2 character selections is that if they don't type the second one quick enough they get the wrong option. Nothing major.
May be not major but important, usualy when you know a menu you don't wait to choose option because time is strategic on cs (can be on any mod).
Anyway, menus with 2 or more pages are just ok.
This tip can be used though, if someone thinks it's cool.
fysiks - yes , in the first few uses of the menu , new users may make a wrong selection, but i'm sure that if they use the menu more then 2-3 times there will be no problem about that
ConnorMcLeod - i know that ~0.2 seconds delay in choosing something from menu can be fatal , but it still depends on how you will format you menu.
The main purpose of this method was for different format then the shown in the first post
Let's say you have more then 3 menus with les then 4 options and it will be good if you can show them at one time without messing their content.
You can still format one menu without any delay on chosing using this method
Something like this :
Code:
1. Menu 1
1.1 Item1
1.2 Item2
1.3 Item3
2. Menu 2
2.1 Item1
2.2 Item2
2.3 Item3
3. Menu 3
etc.
With this format anyone will know that they can't press 1 for using a menu option , they should pres for example 1-1 and this will have no delay exept typing the numbers