AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How to do with one variable (https://forums.alliedmods.net/showthread.php?t=328516)

Abhinash 11-13-2020 09:32

How to do with one variable
 
Hey there.
So in my ZP there are many different classes and I don't like using different variable names like g_nem, g_assa, g_sni, g_surv, etc.
I wonder how can I achieve the same thing by using one varible and bitwise operator like --
g_iPlayerType[ iPlayer ] & P_SNIPER
g_iPlayerType[ iPlayer ] & P_NEMESIS

Like that I have seen one plugin use it but I can't understand. Can anyone tell me how to do as above ?

OciXCrom 11-13-2020 09:38

Re: How to do with one variable
 
Code:
enum _:Types {     P_SNIPER,     P_NEMESIS } new g_iPlayerType[MAX_PLAYERS + 1][Types]

Code:
if(g_iPlayerType[id] == P_SNIPER) {     // your code here }

Abhinash 11-13-2020 10:20

Re: How to do with one variable
 
Quote:

Originally Posted by OciXCrom (Post 2724771)
Code:
enum _:Types {     P_SNIPER,     P_NEMESIS } new g_iPlayerType[MAX_PLAYERS + 1][Types]

Code:
if(g_iPlayerType[id] == P_SNIPER) {     // your code here }

I don't want to use g_iPlayerType[Id] == P_SNIPER

I want to use like
Code:
if (g_iPlayerType[ id ] & P_SNIPER) { // Code }

How to do it ?

redivcram 11-13-2020 10:59

Re: How to do with one variable
 
Take a look at a programmer/scripter's very good friend.

Abhinash 11-13-2020 13:28

Re: How to do with one variable
 
Quote:

Originally Posted by redivcram (Post 2724778)
Take a look at a programmer/scripter's very good friend.

I saw that, it seems like I understood a bit.
Can you give me any other example please ?

OciXCrom 11-13-2020 13:35

Re: How to do with one variable
 
Why do you want to use bitwise operators when the variables are not mutually connected? Can a player be a nemesis and survivor at the same time? Probably not.

Abhinash 11-13-2020 14:58

Re: How to do with one variable
 
Quote:

Originally Posted by OciXCrom (Post 2724795)
Why do you want to use bitwise operators when the variables are not mutually connected? Can a player be a nemesis and survivor at the same time? Probably not.

I just want to use it because I don't like making new vars for every new class. I have over 10+ class and I don't like making 10+ vars for each class. It's a bit of personal preference.

I want when nemesis set it like g_iPlayerType[ id ] |= P_NEMESIS
When survivor g_iPlayerType[ id ] |= P_SURVIVOR

and so on.
Do you get it now ?
If yes, can you show me how to do it ?
Because I have an sma which uses same kind of var as I mentioned above but I can't understand and also Bugsy showed the same in his bit-field tutorial.
I just want someone who can explain me how to set and check back.

OciXCrom 11-13-2020 15:50

Re: How to do with one variable
 
The thing you showed isn't really a good example because you only need 1 variable and with enums you're accomplishing the thing you're asking for. Bitwise is not a good choice here, but the method is still the same - you won't have any more or less variables than with what I've shown.

redivcram 11-13-2020 16:18

Re: How to do with one variable
 
PHP Code:

#define MAX_PLAYERS    32

//Create a list of options that are supported by the plugin
enum ( <<=_Options
{
    
Speed=1// 1 << 0 or 1 
    
AutoAim,  // 1 << 1 or 1 << 1 (as value is calculated by enum)        
    
HighJump// 1 << 2 or 2 << 1
    
ExtraHP,   // 1 << 3 or 4 << 1
    
ExtraArmor // 1 << 4 or 8 << 1
}

//Define an array that will keep track of each option a player currently has
new g_OptionsMAX_PLAYERS+];

//Examples of how to apply option for player
g_Optionsid ] |= Speed;
g_Optionsid ] |= HighJump;
g_Optionsid ] |= ExtraArmor;

//Example how to check if a player has an option
if ( g_Optionsid ] & Speed )
    
//player has speed 

This was literally in Bugsy's thread. There's no way you could've missed it if you haven't read a bit yourself.

fysiks 11-13-2020 17:17

Re: How to do with one variable
 
Just because you prefer something doesn't make it good code. We try to give you the best advice to help you learn which won't always be what you "prefer". If the states are mutually exclusive (meaning that you can't be more than one thing at a time) then you should use the method provided in post #2.

The example provided in post #9 shows a case where the states are not mutually exclusive which is a valid use case of bitwise operations.


All times are GMT -4. The time now is 14:08.

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