[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


Messages In This Thread

Forum Jump:


Users browsing this thread: 2 Guest(s)