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
PHP Code:
#include <sourcemod>
#include <basics/basics/customflags.inc>
#pragma semicolon 1
public Plugin:myinfo =
{
name = "Customflags example",
author = "Impact",
description = "Shows an simple Customflags example",
}
// LateLoad
new bool:g_bLateLoaded;
public APLRes:AskPluginLoad2(Handle:myself, bool:late, String:error[], err_max)
{
g_bLateLoaded = late;
return APLRes_Success;
}
public OnPluginStart()
{
RegConsoleCmd("sm_test", Command_Test);
// Set our flags if you reload the Plugin
if(g_bLateLoaded)
{
for(new i; i <= MaxClients; i++)
{
if(IsClientValid(i))
{
OnClientPostAdminCheck(i);
}
}
}
}
// Define example flags for us to use
// If a player has genericflags we set this to him
#define IS_ADMIN (1 << 0)
// If a players has rootflags set this to him
#define HAS_MESSAGE (1 << 1)
public Action:Command_Test(client, args)
{
if(IsClientValid(client))
{
if(HasCustomClientFlag(client, IS_ADMIN))
{
PrintToChat(client, "Congratulations, you are an admin!");
}
if(HasCustomClientFlag(client, HAS_MESSAGE))
{
PrintToChat(client, "Congratulations, you will get an message!");
}
}
return Plugin_Handled;
}
public OnClientPostAdminCheck(client)
{
if(IsClientValid(client))
{
if(CheckCommandAccess(client, "somecommand", ADMFLAG_GENERIC, false))
{
AddCustomClientFlag(client, IS_ADMIN);
}
if(CheckCommandAccess(client, "somecommand", ADMFLAG_ROOT, false))
{
AddCustomClientFlag(client, HAS_MESSAGE);
}
}
}
public OnClientDisconnect(client)
{
if(IsClientValid(client))
{
// If an client has custom flags set, remove them
if(GetCustomClientFlags(client) > 0)
{
SetCustomClientFlags(client, 0);
}
}
}
stock bool:IsClientValid(id)
{
if(id > 0 && id <= MaxClients && IsClientInGame(id))
{
return true;
}
return false;
}
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
PHP Code:
HasCustomClientFlag(client, flag1^flag2);
SetCustomClientFlags(client, flag1|flag2);
Tell me what you think
Yours sincerely
Impact