Raised This Month: $ Target: $400
 0% 

Enum bits for menu options


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
skatz_ws
Junior Member
Join Date: Aug 2019
Old 09-08-2019 , 10:07   Enum bits for menu options
Reply With Quote #1

Hi!
I want to create a options menu for players to turn on/off things in my server, and I want to create a enumeration with all options, to see if it's on and off.

The best way to do it is with bits? like:

PHP Code:
enum Options ( <<=)
{
    
Option1 1,
    
Option2,
    
Option3,
    
Option4,
    
Option5 
}; 
skatz_ws is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 09-08-2019 , 10:22   Re: Enum bits for menu options
Reply With Quote #2

You are going in the right direction now just create an array with 33 cells for players to store the data.

PHP Code:

enum Options 
( <<=)
{
    
Option1 1,
    
Option2,
    
Option3,
    
Option4,
    
Option5 
}; 

new 
g_menu_Options[33]

@function(
id)
{
     
g_menu_options[id] |= Option3
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 09-08-2019 at 10:27.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
skatz_ws
Junior Member
Join Date: Aug 2019
Old 09-08-2019 , 10:42   Re: Enum bits for menu options
Reply With Quote #3

Right, made it!
Now I want to create a const to give "names" to that option in the menu, is this the right way?

PHP Code:
new const MenuOptions[][Options] = 
{
    
"Option 1",
    
"Option 2",
    
"Option 3"
}; 
And in the menu, something like:

PHP Code:
static item[128];
for(new 
0sizeof Optionsi++)
{
    
formatex(itemsizeof(item) - 1"%s%s"MenuOptions[i], Options:(1<<i) ? "\yON" "\rOFF");
    
menu_additem(menuitem"");

I think that's incorrect because I dont know much about bits and how to work with that enumeration but its just to explain what I want to make

Last edited by skatz_ws; 09-08-2019 at 10:46.
skatz_ws is offline
LearninG
Senior Member
Join Date: Apr 2019
Location: Iran
Old 09-08-2019 , 10:46   Re: Enum bits for menu options
Reply With Quote #4

just a note :
Code:
sizeof(item) - 1
to
Code:
charsmax(item)
LearninG is offline
skatz_ws
Junior Member
Join Date: Aug 2019
Old 09-08-2019 , 10:55   Re: Enum bits for menu options
Reply With Quote #5

Quote:
Originally Posted by LearninG View Post
just a note :
Code:
sizeof(item) - 1
to
Code:
charsmax(item)
Can you explain me the difference? I want to learn
skatz_ws is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 09-08-2019 , 11:01   Re: Enum bits for menu options
Reply With Quote #6

The charsmax is a macro which contains the same thing you've used. Basically it's the same, but amxx already gives you a format to make it easier to write, so you can use it, its up to you.
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo
EFFx is offline
LearninG
Senior Member
Join Date: Apr 2019
Location: Iran
Old 09-08-2019 , 11:17   Re: Enum bits for menu options
Reply With Quote #7

Code:
128 - 1
Code:
127
Code:
126 + 1
Code:
sizeof(item) - 1
Code:
charsmax(item)
all of them works same ( in your case ) , but as you can see "charsmax(item)" is less hardcoded choice , and hardcoding is never welcome.

Last edited by LearninG; 09-08-2019 at 11:20.
LearninG is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-08-2019 , 12:27   Re: Enum bits for menu options
Reply With Quote #8

Associating a selection with a string really only works when using an integer-sized enum since it's an identical association. 0 in the enum corresponds to index 0 in the string array, and so on. This is not as easily done using bit-fields. Technically this will work if you allowed only 1 Option selection per player, but when allowing multiple, this will not work without checking if each individual bit exists and adding the option name to the string.

Your enum:
Code:
Option1 = 1
Option2 = 2
Option3 = 4
Option4 = 8
Option5 = 16
PHP Code:
new const MenuOptionsOptions ][] = 
This will result in an array sized at [ 32 * string size ] while you would only use [ 5 * string size ], which is a waste of memory.

You would also need to define your Options names like this:
PHP Code:
new const MenuOptionsOptions ][] = 
{
    
"",
    
"Option 1",  // 1
    
"Option 2",  // 2
    
"",
    
"Option 3",  // 4
    
"",
    
"",
    
"",
    
"Option 4",  // 8
    
"",
    
"",
    
"",
    
"",
    
"",
    
"",
    
"",
    
"Option 5",  // 16
    
"",
    
"",
    
"",
    
"",
    
"",
    
"",
    
"",
    
"",
    
"",
    
"",
    
"",
    
"",
    
"",
    
"",
    
""
}; 
IMO, at your skill level you may be better off using an array of integers to store which option(s) a player has selected. I'm not saying what you want is not doable, but I'm not sure you will be able to do this yourself.
__________________

Last edited by Bugsy; 09-08-2019 at 12:30.
Bugsy is offline
Xalus
Veteran Member
Join Date: Dec 2009
Location: Belgium
Old 09-08-2019 , 12:47   Re: Enum bits for menu options
Reply With Quote #9

You could do it in a different way:

PHP Code:
#define bit(%1) (1<<%1)

enum _:option_enum
{
    
OPTION_1,
  
OPTION_2,
  
OPTION_3
}

new const 
option_list[option_enum][] =
{
  
"Option 1",
  
"Option 2",
  
"Option 3"
}

new 
g_client_options[33]

// Check if player has the option
for(new 0option_enumi++)
{
  if( 
g_client_optionsclient] & bit(i) )
  {
  
  }
}

// Add option to player
g_client_options[client] |= bitOption_%
It's been a long time, so not sure "Option_1" can be actually "zero".
But you get the point.
__________________
Retired.

Last edited by Xalus; 09-08-2019 at 14:59.
Xalus is offline
Old 09-08-2019, 12:59
Natsheh
This message has been deleted by Natsheh. Reason: Incorrect
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-08-2019 , 17:28   Re: Enum bits for menu options
Reply With Quote #10

I don't see a benefit in using bit-sums here, unless I am missing something. The only exception would be is if you did not need to associate an item with it's string name; in this case bitsums can save you a bit of coding. The below will be much easier to understand if you are just starting.

PHP Code:

#include <amxmodx>

enum Options
{
    
Option1,
    
Option2,
    
Option3
}

new const 
OptionNamesOptions ][] =
{
    
"Option 1",
    
"Option 2",
    
"Option 3"
};

new 
bool:g_bPlayerOptionsMAX_PLAYERS ][ Options ];

public 
plugin_init()
{
    
register_clcmd"say addopt1" "AddOpt1" );
    
register_clcmd"say addopt2" "AddOpt2" );
    
register_clcmd"say addopt3" "AddOpt3" );
    
    
register_clcmd"say delopt1" "DelOpt1" );
    
register_clcmd"say delopt2" "DelOpt2" );
    
register_clcmd"say delopt3" "DelOpt3" );
    
    
register_clcmd"say options" "ShowPlayerOptions" );
}

//The below 3 function's code can be inserted into your code directly, having functions for 1 line of code is overkill. These
//can also be accomplished using macros.
public AddOptionid Options:oOption )
{
    
g_bPlayerOptionsid ][ oOption ] = true;
}

public 
RemoveOptionid Options:oOption )
{
    
g_bPlayerOptionsid ][ oOption ] = false;
}

public 
bool:PlayerHasOptionid Options:oOption )
{
    return 
g_bPlayerOptionsid ][ oOption ];
}

public 
ShowPlayerOptionsid )
{
    new 
szOptions48 ] , iPos;
    
    for ( new 
Options:oOpt Option1 oOpt Options oOpt++ )
    {
        if ( 
PlayerHasOptionid oOpt ) == true )
        {
            
iPos += formatexszOptionsiPos ] , charsmaxszOptions ) - iPos "%s, " OptionNamesoOpt ] );
        }
    }
    
    if ( 
iPos == )
        
copyszOptions charsmaxszOptions ) , "(none)" );
    else    
        
szOptionsiPos ] = EOS;
    
    
client_printid print_chat "You have options: %s" szOptions );
}

public 
AddOpt1(idAddOptionid Option1 );
public 
AddOpt2(idAddOptionid Option2 );
public 
AddOpt3(idAddOptionid Option3 );

public 
DelOpt1(idRemoveOptionid Option1 );
public 
DelOpt2(idRemoveOptionid Option2 );
public 
DelOpt3(idRemoveOptionid Option3 ); 
__________________

Last edited by Bugsy; 09-08-2019 at 18:42.
Bugsy is offline
Reply



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 16:47.


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