Thread: [Include] Bit_Flags 0.1
View Single Post
Old 08/04/2012, 09:58 PM   #1
Jonny5
Gangsta
 
Join Date: Mar 2012
Location: Mi, USA
Posts: 929
Reputation: 158
Default Bit_Flags 0.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.
Jonny5 is offline   Reply With Quote