[Include] Bit_Flags 0.1
#1

Bit Flags 0.1
Target Skill Set
This include is geared towards beginner & intermediate scripter's.
While bit manipulation is not new at all, it is often a hard concept to understand.
You have to know about how binary numbers work and the likes...
However this could save even an advanced scripter some time.

Introduction

What is Bit_Flag 0.1 you ask?
Bit_Flag 0.1 is a set of functions to make it simple to store/check/update/delete multiply flags in a single variable.

What kinda advantages does this have?
In short you could store 32 values in one integer.
The advantage should be clear here, saving memory.

How does this save memory?
Well look at this example..
You have 32 vars in your mode,
each var is one integer(cell in pawn) making you use the memory space of 32 integers
You could store ALL 32 values in one single integer letting you use only the memory
space of 1 integer.

Functions
Check for ALL Flags :
pawn Code:
public Check_All_Bit_Flags(flags,{Float,_}:...);
Check for ANY Flags:
pawn Code:
public Check_Any_Bit_Flags(flags,{Float,_}:...);
Check for a single Flag:
pawn Code:
public Check_Bit_Flag(flags,flag);
Add A Flag:
pawn Code:
public Add_Bit_Flag(&flags,flag);
Add a more than one Flag:
pawn Code:
public Add_Bit_Flags(&flags,{Float,_}:...);
Remove A Flag:
pawn Code:
public Remove_Bit_Flag(&flags,flag);
Remove more than one Flag:
pawn Code:
public Remove_Bit_Flags(&flags,{Float,_}:...);
Remove ALL Flags:
pawn Code:
public Remove_All_Bit_Flags(&flags);
I WILL update later with usage on the rest of the functions,
But basically they just allow you to set/check/remove more than 1 flag at a time
and all depend on the functions I have already shown.


Useage
I will describe in short the usage of these functions.
I plan a tutorial later with more details on this.
For the sake of this demo I will not be going into how to load/save these flags.
You'll already need some sort of saving system, be it MYSQL, file based or one of the
many ini systems.

Scenario:
I am setting up a GM that will have 4 Different Types of groups.(not teams)
I will have 4 different Flags for deciding what player can access what .
So I start out by defining my flags.

pawn Code:
BitFlag:
{
    FLAG_ADMIN= 1,
    FLAG_MOD,
    FLAG_VIP,
    FLAG_Dev
}
notice the first flag = 1,
this is very important as it sets up the rest of the values in our BitFlag define.
this is the same as doing
pawn Code:
enum (<<= 1)
{
    FLAG_ADMIN= 1,
    FLAG_MOD,
    FLAG_VIP,
    FLAG_Dev
}
so we have our flags now on to our player info var.

normally your PlayerInfo would look along the lines of this(i dont have ALL my info just enough for this demo)
pawn Code:
enum ePlayerInfo
{
//other vars up here
    pAdmin,
    pMod,
    pDev,
    pVip
}
now mine looks like this
pawn Code:
enum ePlayerInfo
{
//other vars up here
    pFlags
}
aPlayerIno[MAX_USERS][ePlayerInfo];
I will store pAdmin,pMod,pDev,pVip all in one var (pFlags)

Now i will show you how to add/check/remove these flags
WHERE you do this is up to you and your system of loading/saving.

Add a flag
in this case the admin flag.
pawn Code:
Add_Bit_Flag(aPlayerIno[playerid][pFlags],FLAG_ADMIN);
Check if a flag is set
pawn Code:
Check_Bit_Flag(aPlayerIno[playerid][pFlags],FLAG_ADMIN);
Remove a flag
pawn Code:
Remove_Bit_Flag(aPlayerIno[playerid][pFlags],FLAG_ADMIN);
Please post any Questions you may have on something that was not covered in this topic.

Download
SolidFiles
PasteBin


Credits
Jonny5 : Creator of Bit_Flags0.1
SAMP Team : For this wonderful mod you have given us!
SAMP Community Gurus(all you guys help us to improve our skill sets, thank you!)

Authors Notes...
This is my first release to the SAMP community,
I EXPECT you guys to find flaws with this include and topic.
Please post any bugs and suggestions in this topic.
Reply
#2

"Example Usage Section"

pawn Code:
//Setup our user flags.
enum(<<=1)
{
    pSpawned =1,
    pDev,
    pAdmin,
    pVip,
    pAFK
}

//define out player flags array
new gPlayerFlags[MAX_PLAYERS];


public OnPlayerSpawn(playerid)
{
    //add the flag of spawned once player spawns
    Add_Bit_Flag(gPlayerFlags[playerid],pSpawned);
    return 1;
}

public OnPlayerDeath(playerid, killerid, reason)
{  
    //remove the spawned flag when they die
    Remove_Bit_Flag(gPlayerFlags[playerid],pSpawned);
    return 1;
}


public OnPlayerCommandText(playerid, cmdtext[])
{
    if(!strcmp(cmdtext, "/admin", true, 6))
    {
        SendClientMessage(playerid, 0xFFFFFFFF, "/admin : Make yourself an admin");
        //add admin flag
        Add_Bit_Flag(gPlayerFlags[playerid],pAdmin);
        return 1;
    }
    if(!strcmp(cmdtext, "/radmin", true, 7))
    {
        SendClientMessage(playerid, 0xFFFFFFFF, "/radmin : Remove yourself as an admin");
        //remove admin flag
        Remove_Bit_Flag(gPlayerFlags[playerid],pAdmin);
        return 1;
    }
    return 0;
}
Reply
#3

I don't think beginner scripters would use this. I use flags on my script to set permissions and I think it's waaaay easier use the normal style. With ANDs and ORs.
Reply
#4

Well it would be nice if they starting learning things the correct way
from the start, Im still seeing strcmp and strok questions
and Im new here but from what I understand they where replaced long ago..

anyhow I wrote these functions for myself to use on another project and thought id post them here
and maybe even teach someone something. Its there for there takings.. lol


thanks for the response.
Reply
#5

Please update with usage examples.
Reply
#6

I updated the second post if there is something more you want just ask and Ill try to mock it up.
Reply
#7

Damn good work Johnny. Good to see this here. Repped+4
Reply
#8

thank you!
I know its small and simple but
good to see its usefull for someone besides me!!

~j5
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)