AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Enum bits for menu options (https://forums.alliedmods.net/showthread.php?t=318579)

skatz_ws 09-08-2019 10:07

Enum bits for menu options
 
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 
}; 


Natsheh 09-08-2019 10:22

Re: Enum bits for menu options
 
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


skatz_ws 09-08-2019 10:42

Re: Enum bits for menu options
 
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

LearninG 09-08-2019 10:46

Re: Enum bits for menu options
 
just a note :
Code:
sizeof(item) - 1
to
Code:
charsmax(item)

skatz_ws 09-08-2019 10:55

Re: Enum bits for menu options
 
Quote:

Originally Posted by LearninG (Post 2666269)
just a note :
Code:
sizeof(item) - 1
to
Code:
charsmax(item)

Can you explain me the difference? I want to learn

EFFx 09-08-2019 11:01

Re: Enum bits for menu options
 
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.

LearninG 09-08-2019 11:17

Re: Enum bits for menu options
 
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.

Bugsy 09-08-2019 12:27

Re: Enum bits for menu options
 
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.

Xalus 09-08-2019 12:47

Re: Enum bits for menu options
 
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.

Bugsy 09-08-2019 17:28

Re: Enum bits for menu options
 
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 ); 



All times are GMT -4. The time now is 17:29.

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