[Tutorial] Use bool variables: It's easy.
#1

Hello.

Some NOT use bool variable which is quite a shame.

To fast they can afford to pass a variable to true or false state for different systems as an OOC channel on the roleplay server etc.

Let's get into the nitty-gritty, how is a bool variable?

It looks like this:
(In theory variables like that are global.)

pawn Code:
new bool:Easy;
You can boot to a command or server start that this variable is the state as true or false:

pawn Code:
Easy = true;
Easy = false;
From it you can quickly make an order, here's a quick example very concrete:

pawn Code:
if(strcmp(cmd, "/command", true) == 0)
{
    if(Easy == false)
    {
        SendClientMessage(playerid, -1, "You just activate your system variable is set to true.");
        Easy = true;
    }
    else
    {
        SendClientMessage(playerid, -1, "You just disable your system variable is set to false.");
        Easy = false;
    }
    return 1;
}
As you can see it is very simple to use and it can be really useful.

++
Reply
#2

There is an other way to check for booleans :

pawn Code:
if(Easy == true)
Can be replaced by

pawn Code:
if(Easy)
And of course "if(Easy == false)" (not in your tutorial but very useful when you needs booleans) can be replaced by

pawn Code:
if(!Easy)
Basically, when you use "!something", the compiler checks if "something" is equal to 0. If it is, then the condition returns true and it executes what's between brackets.
And checking a value like this

pawn Code:
if(something)
Don't check if the variable is equal to 1 but checks if the variable is different from 0.
You should add these informations in your tutorial.
Reply
#3

Again the tutorials are for beginners, if you start to fill their heads with other methods they will ultimately understand anything.

Nothing beats a simple and detailed tutorial.

However, I can make another part where I pointed out that fat is a bit more complex to understand is possible.
Reply
#4

It's not global in theory. It's global.

This isn't even a tutorial. People can choose whether to use booleans or the 0 and 1 method.

Because that's all the boolean is is doing, checking if the value differentiates from 0.
Reply
#5

@sammp : The only differences between this :

pawn Code:
new variable = 0;
if(!variable) // or if(variable == 0)
      variable = 1;
if(variable) // or if(variable == 1)
      return 1;
And this

pawn Code:
new bool:variable = false;
if(!variable)
      variable = true;
if(variable)
      return 1;
Are :

• The use of booleans just takes a byte (or 4, i don't exactly remember) in memory (instead of a 32 bytes integer, which, as its name says, takes 32 bytes).
• Using booleans and the direct way of checking a value ( "if(variable)" or "if(!variable)"), you code faster and you are sure to check only a value.

And btw, if using booleans is useless, then using programming convention is useless. (all the programming convention does is to make a code clearer, what a pointless feature don't you think ?)
This way, using the #define directive becomes also useless because all it does is to define a macro that we can code ourself each time. (again, so useless)

Just use your common sense : if the booleans were created, it's because of a very good reason.
Reply
#6

Those arguments are invalid. A boolean variable in Pawn is just a regular 32 bit (not byte) integer with a special tag. Second point also applies to all other types of variables. Anything that's not 0 (or false) is per definition true. That means -1 is true and values like INVALID_VEHICLE_ID (0xFFFF or 65535) are also true.

Regular booleans are quite obsolete actually. They can be useful in function headers maybe. But otherwise they don't really have a use, given that you can squeeze up to 32 booleans in one regular integer variable.
Reply
#7

It's not like C/++ where the booleans are smaller than regular 32 bits integer ?

Okay, but for the second point i don't agree.
It just allows you a better variable managing than using integers. Particularly (and that's the only point of booleans) if you know that your variable will not contain something else than false or true (0 or 1).
It's very useful for example in player stats for RP(G) server ("hasMask", "hasPhone", etc).
Reply
#8

That's why I mentioned squeezing them in a 32 bit integer;
pawn Code:
enum (<<= 1)
{
    hasMask = 1,  // 0000 0001 (1)
    hasPhone,     // 0000 0010 (2)
    hasRadio,     // 0000 0100 (4)    
    hasLicense,   // 0000 1000 (8)
    hasGunPermit, // 0001 0000 (16)
    // ...
    // up to 32 values in here
}

new gPlayerBooleanVars[MAX_PLAYERS];
pawn Code:
#define setvar(%0,%1) ((%0) |= (%1))
#define unsetvar(%0,%1) ((%0) &= ~(%1))
#define isvarset(%0,%1) ((%0) & (%1))
pawn Code:
if(isvarset(gPlayerBooleanVars[playerid], hasMask))
There are tutorials on this.
Reply
#9

ternary is very usefull too

you can see that here:

https://sampforum.blast.hk/showthread.php?tid=216730
Reply
#10

Quote:
Originally Posted by Baltimore
View Post
Again the tutorials are for beginners, if you start to fill their heads with other methods they will ultimately understand anything.
Even it's for beginners, you should mention everything. Why it's called a tutorial if they won't understand it ?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)