WHAT IS A BOOLEAN?
A boolean is an offspring of a guy named George Boole. So any child of George Boole is a boolean. hahaha.
NO REALLY! WHAT IS A BOOLEAN?
A boolean is a DATA TYPE (a type of data) that has only two possible values, TRUE and FALSE.
WHAT IS A DATA TYPE?
Well, text is a type of data. A number is a type of data. Data is considered the building blocks of information, and we store information in the form of text/words and numbers/integers.
SO WHAT IS A BOOLEAN AGAIN?
A boolean is data that can only be one of two values, TRUE and FALSE, which are represented by the numbers 1 and 0 (1 is true, 0 is false)
WHAT ARE BOOLEANS USED FOR?
Booleans are used to ACTIVATE and DEACTIVATE stuff. It actually goes back to how computers work. A computer is just a bunch of electrical components at the end of the day, which are either ON or OFF depending on the flow of current.
Imagine you had a computer screen that was set up like this:
xxxxxxxxx
xxxxxxxxx
xxxxxxxxx
The resolution of your computer screen would be 9 by 3 Xs.
Each x represents a light bulb, and at any given time, some lightbulbs will be on, and others will be off. In fact, if these lightbulbs flicker fast enough, your screen will look like one solid color.
Using some very complex circuitry tricks you can control the way the Xs flicker.
These ONs and OFFs or 1s and 0s can be manipulated to create things like DISPLAYING PIXELS on your computer.
That is what a boolean is used for.
Say we have a variable:
If we want to make it a BOOLEAN, we can either give it the value TRUE or the value FALSE. By default a light is off so:
What does this mean in our program? Well, that light = false! Nothing more.
But we can use this light variable to control things. We can use it with a CONDITIONAL STATEMENT to control when things happen:
lightBulb = getObject("LightBulb")
if light==false then
setObjectIntensity(lightBulb,0)
end
So, as long as light = false, then the intensity of the lighBulb object will be 0.
But, as soon as we change the light boolean variable to TRUE, then we can can change the intensity to full brightness.
lightBulb = getObject("LightBulb")
if light==false and isKeyPressed("L")then
setLightIntensity(lightBulb,1)
light = true
elseif light==true and isKeyPressed("L")then
setLightIntensity(lightBulb,0)
light = false
end
So, this says that if light=false and you press the "L" key, set the intensity of the lightbulb object to 100%. Also, set light to true (now light=true and not false)
On the other hand, if light=true (it's true now because we made it true by pressing "L") and you press the "L" key, then set the lighbtbulb's intenstity to 0. Also, set light equal to false (so that if we press "L" again it will work, because light has to be equal to false for it to work).
This is how we can use booleans to TOGGLE
I always like to put these types of things into a nice little function:
light_1 = getObject("Light1")
light_2 = getObject("Light2")
function ToggleLight(object)
if light==false and isKeyPressed("L")then
setLightIntensity(object,1)
light = true
elseif light==true and isKeyPressed("L")then
setLightIntensity(object,0)
light = false
end
end
Notice how I used an ARGUMENT so that I can make whichever light I put into the parenthesis able to turn on and off. Used like this:
ToggleLight(light1)
ToggleLight(light2)
Etc.
OTHER POSSIBLE USES OF BOOLEANS:
We can use booleans to have things happen based on a character's current state/emotion
george = getObject("George")
happy=false
sad=false
normal=true
function BeEmotional()
if normal==true then
changeAnimation(george,0)
sad=false
happy=false
elseif happy==true then
changeAnimation(george,1)
normal = false
sad=false
elseif sad== true then
changeAnimation(george,2)
happy=false
normal=false
end
end
If I were to use Object Oriented Programming and a few ARGMENTS I could make this a bit easier to use and re-use.