[Tutorial] Booleans and what they can be used for (Checking states, etc)
#1

Hello! I saw no tutorial on the type of variables named 'booleans,' hence, I am here to make one.
Without much delay, let's start:

What are boolean variables or in short - bools?

George Boole is the credit for all this. This awesome guy's ideas runs your PC, to be honest. There are 2 voltages - 5 Volts and 0 Volts and anyone with a bit of hardware knowledge must know that the computer runs on these voltages.
There are two logic states, i.e. they are:
Logic true/Logic 1 == Around 5 volts (yes, actual voltage, that's why your computer needs electricity)
Logic false/Logic 0 == Around 0 Volts.
Basically, this idea runs all of the things we see, transistors, chips, motherboards, etc. They send the logic state 5, then 0, then 5 then 5, meaning they just created a logic number: 1011 ! This helps the PC in understanding the state.
There is boolean algebra for computers, and so on. To talk about boolean algebra would take a whole thread itself!
It's different, 1 + 1 in boolean algebra is equal to 1. (Carry over 1, if you have that option (in logic gates) )

So, if you have read that, you can notice that:
Boolean variables are variables that can take the value of:
-------------------------
True OR False
1 OR 0
-------------------------
How can I declare a bool in PAWN(o)?

pawn Код:
new bool:yourvarname;
So now, you can assign the value at the start or whatever! For example:
pawn Код:
new bool:yourvarname = false;
So now, that variable 'yourvarname' is false.


What for can I use this?


Most scripters use bools to check the state of a player (in SA-MP) or check the state of a certain event.
Well, I have seen many guys asking how to check in my command if he is in god mode?

pawn Код:
new bool:isplayerongodmode = false;
CMD:god(playerid)
{
    if(isplayerongodmode == false) //Checking if the 'state' of the player is NOT GOD MODE.
{
 SetPlayerHealth(playerid, 999); //Setting the player's health to infinity.
 isplayerongodmode = true; //Setting the player's mode to 'ON GOD MODE'.
 }
 if(isplayerongodmode == true) //Checking if the 'state' of the player is GOD MODE.
{
 SendClientMessage(... "You are already on god mode!"); //Sending him the error message.
 }
return 1;
}
That is it! No switching.
You can use boolean variables to check the state of anything, just set it and get going! For example, if an event is being held, if a race is on, if a player is in god mode, if a player is in skin selection state, etc.
They can be used for many other things too!
Sometimes, they are used without your knowledge! You must have seen this:
pawn Код:
return true;
This is nothing but
pawn Код:
return 1;
We are so into using the great George Boole's methods, we don't notice it!
I will be adding more uses of this var in the tutorial.
Till then, thanks and cheers!
Please suggest any more things that can be added here, and anything I didn't know of. Also do notify me of any mistakes.
Reply
#2

Not bad, but just pointing out somethings -

It should be -
pawn Код:
new bool:isplayerongodmode[MAX_PLAYERS] = false;

CMD:god(playerid)
{
    if(isplayerongodmode[playerid] == false) //Checking if the 'state' of the player is NOT GOD MODE.
    {
         SetPlayerHealth(playerid, 999); //Setting the player's health to infinity.
         isplayerongodmode[playerid] = true; //Setting the player's mode to 'ON GOD MODE'.
     }
     if(isplayerongodmode == true) //Checking if the 'state' of the player is GOD MODE.
     {
          SendClientMessage(... "You are already on god mode!"); //Sending him the error message.
      }
     return 1;
}
Also, you should show an example of how to use return true; and return false;


pawn Код:
stock bool:IsAdminConnected()
{
    for(new x= 0; x < MAX _PLAYERS; x++)
    {
          if(IsPlayerAdmin(x)) return true;
    }
    return false;
}
and a little more further explain HOW a normal variable differs from a boolean, what's the advantage/disadvantage etc.
Reply
#3

Quote:
Originally Posted by Rajat_Pawar
Посмотреть сообщение
This awesome guy's ideas runs your PC, to be honest. There are 2 voltages - 5 Volts and 0 Volts and anyone with a bit of hardware knowledge must know that the computer runs on these voltages.
Actually, a computer mostly runs on a 12V rail. That 5 Volt shizzle is something they teach you in electronics class if you're working with electronic components (transistors, capacitors, logic gates and shit).

Other than that, the idea of the regular boolean is quite obsolete in SA-MP, since you are in fact wasting 31 perfectly good bits (time 500 if you use MAX_PLAYERS). That's why people have come up with bit shifted enums. I'm sure that there's a tutorial somewhere in this forum or on the wiki.
Reply
#4

Код:
if(isplayerongodmode == true)
=
Код:
if(isplayerongodmode)
Код:
if(isplayerongodmode == false)
=
Код:
if(!isplayerongodmode)
Reply
#5

Quote:
Originally Posted by Mellnik
Посмотреть сообщение
Код:
if(isplayerongodmode == true)
=
Код:
if(isplayerongodmode)
Not really, ANY non zero value (Even negative) is treated as true if you use if(isplayerongodmode). So if you want any value less than or equal to 0, false, then that line wont work.
Reply
#6

At the first poster, yes, I did forget to make it an array. At vince, I do know that, but that shizzle is what basically made the PC what it is now! I mean, this was the beginning. At the 3rd poster, it's not much of a big difference, for explanation's sake. And yes, I will be adding all those returning examples with stocks, thanks!
Reply
#7

(-∞; +∞) \ {0} == true
0 == false
Reply
#8

+ Infinity / 0 = Infinity == true? Really?
So you mean, that there are two logic levels,
∞ (true) and 0 (false)? This is not sarcasm, I am really asking, curious.
Reply
#9

@ Rajat
What Misiur posted means that everything except for 0 (\ is for except) is true, 0 is false.
Reply
#10

Quote:
Originally Posted by Vince
Посмотреть сообщение
Actually, a computer mostly runs on a 12V rail. That 5 Volt shizzle is something they teach you in electronics class if you're working with electronic components (transistors, capacitors, logic gates and shit).

Other than that, the idea of the regular boolean is quite obsolete in SA-MP, since you are in fact wasting 31 perfectly good bits (time 500 if you use MAX_PLAYERS). That's why people have come up with bit shifted enums. I'm sure that there's a tutorial somewhere in this forum or on the wiki.
Yep http://forum.sa-mp.com/showthread.ph...22#post1037822
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)