SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Includes

Reply
 
Thread Tools Display Modes
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
Old 08/04/2012, 10:00 PM   #2
Jonny5
Gangsta
 
Join Date: Mar 2012
Location: Mi, USA
Posts: 929
Reputation: 158
Default Re: Bit_Flags 0.1

"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;
}

Last edited by Jonny5; 13/05/2012 at 04:06 PM.
Jonny5 is offline   Reply With Quote
Old 08/04/2012, 10:07 PM   #3
steki.
High-roller
 
steki.'s Avatar
 
Join Date: Nov 2010
Location: São Paulo, Brasil
Posts: 2,557
Reputation: 317
Default Re: Bit_Flags 0.1

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.
__________________
Rio
steki. is offline   Reply With Quote
Old 08/04/2012, 10:15 PM   #4
Jonny5
Gangsta
 
Join Date: Mar 2012
Location: Mi, USA
Posts: 929
Reputation: 158
Default Re: Bit_Flags 0.1

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.
Jonny5 is offline   Reply With Quote
Old 09/05/2012, 02:50 PM   #5
mprofitt
Huge Clucker
 
Join Date: Jul 2008
Posts: 459
Reputation: 14
Default Re: Bit_Flags 0.1

Please update with usage examples.
mprofitt is offline   Reply With Quote
Old 13/05/2012, 04:07 PM   #6
Jonny5
Gangsta
 
Join Date: Mar 2012
Location: Mi, USA
Posts: 929
Reputation: 158
Default Re: Bit_Flags 0.1

I updated the second post if there is something more you want just ask and Ill try to mock it up.
__________________
If your topic says "REP+", I ignore it.
Jonny5 is offline   Reply With Quote
Old 14/05/2012, 12:53 AM   #7
Ballu Miaa
High-roller
 
Ballu Miaa's Avatar
 
Join Date: Apr 2010
Location: New Delhi,India
Posts: 1,849
Reputation: 323
Default Re: Bit_Flags 0.1

Damn good work Johnny. Good to see this here. Repped+4
__________________


I will take half payment as an advance to script for you.
Click here to request my Scripting Service.

Ballu Miaa is offline   Reply With Quote
Old 14/05/2012, 06:31 PM   #8
Jonny5
Gangsta
 
Join Date: Mar 2012
Location: Mi, USA
Posts: 929
Reputation: 158
Default Re: Bit_Flags 0.1

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

~j5
__________________
If your topic says "REP+", I ignore it.
Jonny5 is offline   Reply With Quote
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



All times are GMT. The time now is 04:26 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.