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

[INC] Customflags 0.6 Beta


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 05-15-2012 , 19:03   [INC] Customflags 0.6 Beta
Reply With Quote #1

This is nothing special but i find it incredibly useful and wonder why nobody uses this technique (at least i didn't saw something like that in another plugin).
If you write big plugins, things can get complicated and you might end up with resetting 20 variables on ClientDisconnect.
I still don't know exactly how to use bitwise operators, which brought me to the idea to use them in my scripts.
This was intended as a "library" for my own includes, but i think this will be useful to someone else too.

Well, here is an simple example that should explain what the purpose of this includefile is.
Example #1



Notes
From what i've found out you can store ~30 flags until they start again from zero.
You can use this for example to limit native calls to things like: IsFakeClient, IsPlayerAlive, etc
Of course you can group your flags, for example:
Example


Changelog



Tell me what you think

Yours sincerely
Impact
Attached Files
File Type: inc customflags.inc (3.8 KB, 157 views)
__________________

Last edited by Impact123; 08-15-2012 at 21:24.
Impact123 is offline
ReFlexPoison
☠☠☠
Join Date: Jul 2011
Location: ☠☠☠
Old 05-15-2012 , 20:20   Re: [INC] Customflags 0.5 Beta
Reply With Quote #2

Doesn't SMLIB have these functions? Or somewhat similar ones? Correct me if I'm wrong (which I probably am)
ReFlexPoison is offline
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 05-15-2012 , 20:34   Re: [INC] Customflags 0.5 Beta
Reply With Quote #3

Smlib has an wonderful makro called LOOP_CLIENTS where you can pass one or more clientfilters defines.
Something like i wrote i saw nowhere till now.
Maybe you can show me this function?

Yours sincerely
Impact
__________________

Last edited by Impact123; 05-15-2012 at 20:35.
Impact123 is offline
ReFlexPoison
☠☠☠
Join Date: Jul 2011
Location: ☠☠☠
Old 05-15-2012 , 22:25   Re: [INC] Customflags 0.6 Beta
Reply With Quote #4

Oh nvm, I was only looking at the IS_ADMIN. That's the only a-like function I see from SMLIB.
ReFlexPoison is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 05-17-2012 , 11:43   Re: [INC] Customflags 0.6 Beta
Reply With Quote #5

The problem with the given example is that it ignores anything other plugins are doing. For example, if another plugin does this:
PHP Code:
SetAdminFlag(adminIdAdmin_Generictrue
your plugin will never set their IS_ADMIN flag and your plugin's idea of what the users admin flags are would be wrong.

This is the same problem you'd have if you called GetConVarSomething in OnAllPluginsLoaded or OnConfigsExecuted rather than when you actually need the value... something else may change it first.

For that matter, to check if a flag is present, you can already just do this:
PHP Code:
new flags;

...

// Add the HAS_MESSAGE flag
flags |= HAS_MESSAGE

// Remove the HAS_MESSAGE flag
flags &= ^HAS_MESSAGE

// Check the HAS_MESSAGE flag
if (flags HAS_MESSAGE)
{
    
// They have a message

(Edit: Adjusted code for removing flags)

You're limited to 32 flags because that's how many bits you have in a cell.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 10-23-2012 at 15:01.
Powerlord is offline
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 05-17-2012 , 23:58   Re: [INC] Customflags 0.6 Beta
Reply With Quote #6

Yes you are right, it really depends on what you doing with it.
If you use if as a cache for like IsPlayerAlive (together with a hook ofc) or IsFakeClient that should work fine.
&= seems to be correct, at least it is docmented like that, for example here and don't get the right results with =.
I think it is better to look over a function, than over a statement for the same reason we use defines for some variables.

Note: I think the spelling is bad, i might fix it later.

Yours sincerely
Impact
__________________

Last edited by Impact123; 05-18-2012 at 00:03.
Impact123 is offline
FaTony
Veteran Member
Join Date: Aug 2008
Old 10-23-2012 , 04:17   Re: [INC] Customflags 0.6 Beta
Reply With Quote #7

Not quite sure about the purpose of this. The only reason you would want to stack boolean states into 1 cell is when you are very low on memory and really really care about speed. But in this case, your interface is quite cumbersome. In my plugins, if I want to store a boolean state of client, I just do
PHP Code:
new bool:IsClientBlahBlah[MAXPLAYERS 1]; 
__________________
FaTony is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 10-23-2012 , 15:04   Re: [INC] Customflags 0.6 Beta
Reply With Quote #8

Wow, looking at this topic again, I have no idea why I was using ^= HAS_MESSAGE to unset flags rather than &= ^HAS_MESSAGE

Quote:
Originally Posted by FaTony View Post
Not quite sure about the purpose of this. The only reason you would want to stack boolean states into 1 cell is when you are very low on memory and really really care about speed. But in this case, your interface is quite cumbersome. In my plugins, if I want to store a boolean state of client, I just do
PHP Code:
new bool:IsClientBlahBlah[MAXPLAYERS 1]; 
Well, if you want to pass them to a function or native, then you'd want to use flags like this. Also, it can get kind of messy when you're dealing with a bunch of related booleans. Doesn't help that bool is still a cell, so 4 bytes per entry.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 10-23-2012 at 15:08.
Powerlord is offline
FaTony
Veteran Member
Join Date: Aug 2008
Old 10-24-2012 , 03:30   Re: [INC] Customflags 0.6 Beta
Reply With Quote #9

Let's solve it once and for all:
Get
PHP Code:
new value flags SOME_FLAG
Set
PHP Code:
flags |= SOME_FLAG
Unset
PHP Code:
flags &= ~SOME_FLAG
Toggle
PHP Code:
flags ^= SOME_FLAG
__________________
FaTony 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 13:15.


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