Raised This Month: $12 Target: $400
 3% 

How to do with one variable


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Abhinash
Senior Member
Join Date: Jan 2015
Location: India,kolkata
Old 11-13-2020 , 09:32   How to do with one variable
Reply With Quote #1

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 ?
Abhinash is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 11-13-2020 , 09:38   Re: How to do with one variable
Reply With Quote #2

Code:
enum _:Types {     P_SNIPER,     P_NEMESIS } new g_iPlayerType[MAX_PLAYERS + 1][Types]

Code:
if(g_iPlayerType[id] == P_SNIPER) {     // your code here }
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Abhinash
Senior Member
Join Date: Jan 2015
Location: India,kolkata
Old 11-13-2020 , 10:20   Re: How to do with one variable
Reply With Quote #3

Quote:
Originally Posted by OciXCrom View Post
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 ?

Last edited by Abhinash; 11-13-2020 at 10:20.
Abhinash is offline
redivcram
Veteran Member
Join Date: Jul 2014
Location: Serbia
Old 11-13-2020 , 10:59   Re: How to do with one variable
Reply With Quote #4

Take a look at a programmer/scripter's very good friend.
redivcram is offline
Abhinash
Senior Member
Join Date: Jan 2015
Location: India,kolkata
Old 11-13-2020 , 13:28   Re: How to do with one variable
Reply With Quote #5

Quote:
Originally Posted by redivcram View Post
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 ?
Abhinash is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 11-13-2020 , 13:35   Re: How to do with one variable
Reply With Quote #6

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.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Abhinash
Senior Member
Join Date: Jan 2015
Location: India,kolkata
Old 11-13-2020 , 14:58   Re: How to do with one variable
Reply With Quote #7

Quote:
Originally Posted by OciXCrom View Post
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.
Abhinash is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 11-13-2020 , 15:50   Re: How to do with one variable
Reply With Quote #8

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.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
redivcram
Veteran Member
Join Date: Jul 2014
Location: Serbia
Old 11-13-2020 , 16:18   Re: How to do with one variable
Reply With Quote #9

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.

Last edited by redivcram; 11-13-2020 at 16:18.
redivcram is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-13-2020 , 17:17   Re: How to do with one variable
Reply With Quote #10

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.
__________________
fysiks is offline
Reply


Thread Tools
Display Modes

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


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