AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [INC] Customflags 0.6 Beta (https://forums.alliedmods.net/showthread.php?t=185330)

Impact123 05-15-2012 19:03

[INC] Customflags 0.6 Beta
 
2 Attachment(s)
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

ReFlexPoison 05-15-2012 20:20

Re: [INC] Customflags 0.5 Beta
 
Doesn't SMLIB have these functions? Or somewhat similar ones? Correct me if I'm wrong (which I probably am)

Impact123 05-15-2012 20:34

Re: [INC] Customflags 0.5 Beta
 
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

ReFlexPoison 05-15-2012 22:25

Re: [INC] Customflags 0.6 Beta
 
Oh nvm, I was only looking at the IS_ADMIN. That's the only a-like function I see from SMLIB.

Powerlord 05-17-2012 11:43

Re: [INC] Customflags 0.6 Beta
 
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.

Impact123 05-17-2012 23:58

Re: [INC] Customflags 0.6 Beta
 
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

FaTony 10-23-2012 04:17

Re: [INC] Customflags 0.6 Beta
 
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]; 


Powerlord 10-23-2012 15:04

Re: [INC] Customflags 0.6 Beta
 
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 (Post 1823977)
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.

FaTony 10-24-2012 03:30

Re: [INC] Customflags 0.6 Beta
 
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



All times are GMT -4. The time now is 11:43.

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