Stencyl For Noobs : Intro to Boolean (True/False) Logic

This is a “Stencyl For Noobs” guide! Take 5-10 minutes to go over this guide for a better understanding of the topic at hand! 

Intro to Boolean (True/False) Logic

What we’re learning: Booleans (true/false values): what they are, and why they are so important to the design of your game.

Contents

Just What Are Booleans?
Why Are They Important?
Booleans Are Picky!
A Simple “If-Statement”

* * * * *

Just What Are Booleans?

As stated in the summary, booleans are values that either read out with “true” or “false”. They’re like a “yes/no” or “correct/incorrect”. That’s really all there is to it. At the core of the matter, just remember these two things:

  • A boolean in Stencyl can only evaluate to “true” or “false”. For most of our purposes, there is no in-between.*
  • The two values (true/false) are completely opposite of one another.

*(If you are more computer science savvy like I am, you may recognize the one [rare] exception I am talking about; otherwise, don’t concern yourself with it for right now.)

Why Are They Important?

Booleans are often what drive the heart of your decision-making. Does a player move left or right? Should I slow him down? Did he get hurt? How does a game know all of these things?

You use booleans. Booleans check to see if a condition is met, then return “true” or “false” based on what it found. There true/false values help determine what your game does. Look at the following blocks below:

Boolean blocks in Stencyl are displayed as hexagonal blocks.

Boolean blocks in Stencyl are displayed as hexagonal blocks.

See how each of them have a clear condition? These are absolute conditions, meaning either they were met or they were not met; no in-between!

So if the stated condition is correct, you get “true”. If not, you get “false”. Its just that simple… or is it?

Booleans Are Picky!

Stay with me; this gets a little detailed but this stuff is important. And also, the above is simple. Here’s where you run into problems though: sometimes you think you’ve told the computer one thing, when in reality you’ve told it something perhaps completely different. Computers are wildly notorious for doing… well, what you tell them to do. The same goes for what you don’t tell them to do… they typically don’t do it. Now, when you buy a computer or get some software (like Stencyl), often the programmer(s) on the other end has already told the computer or software to do certain things for you already. That’s normal; someone somewhere told it to do something. Otherwise, why would we buy software? Its up to us to figure out what it is doing for us already and determine what else we want it to do.

Booleans (conditions) are the same way. Let’s have a trial condition to evaluate:

<Bob has an apple>

For our purposes, if Bob does, indeed, have an apple, the statement is true. If Bob doesn’t have an apple, the statement is false.

OK, that makes sense. But what if Bob throws away the apple and picks up an orange? Now the condition is false. What if he picks up a banana? The condition is still false. What if he picks up an orange, banana, and a bucket of kiwis? Its STILL false so long as he no longer has the apple. Our condition does not care about anything other than the fact that Bob has an apple or not. Bananas, oranges, kiwis, jetpack hippos… whatever… they are irrelevant for how we currently evaluate the condition. Look at the apple! Does Bob have an apple or not?

But let’s get more technical. What if Bob has three apples? This is where you have to understand what exactly a condition is asking. Is it asking if Bob has only one apple, or if Bob just has at least one apple? Since we are using a very loose example and its not a strict computer-based condition, its open to interpretation. For our instance we will go with the latter. We just want to know if Bob has at least one apple, in which case the statement is true.

Are you getting it? Now look at these:

1.) <Bob has exactly three apples>
2.) <Bob has at least three apples>

Those two conditions are DIFFERENT. They essentially ask:

1.) Bob’s Apples = 3?
2.) Bob’s Apples >= 3?

That distinction is important. You need to know exactly what you are looking for. If you want the value “true” ONLY when you have three apples, then you want to use the first. If you want the value “true” if Bob has 3, 4, 5, or more apples, then you want the second. Your entire game’s logic could be thrown off by how the number 4 is evaluated in your chosen condition! You need to think carefully about what your conditions are, because this is what drives your game’s decision making!

Stencyl, unlike our first example, is very specific about what each of its conditions mean. You need to look carefully at the meaning of each condition in Stencyl. Explore your options and try to understand all the different conditions and how they are different from each other.

A Simple “If-Statement”

If-statements are part of how we direct our decision-making. That is to say, “If condition A, then do B”.

This block can be found under Flow >> Conditional.

This block can be found under Flow >> Conditional.

(NOTE: Despite everything I’ve done up to this point, WordPress seems to keep mussing up the above picture’s display. You’ll have to click on it to see it right.)

Notice that empty space wrapped around the if-statement? That’s where we stick the code to execute when the if-statement evaluates to true. Look at the example below:

Boolean 3

Notice the code inside the block. This only executes IF Attribute X is true. If Attribute X is false, then the code inside the if statement is skipped. Realize also that, as it stands right now, the “Set Number B” runs regardless of what happens with the if-statement. Currently the if-statement only controls what happens with the code inside of it (Set Number A), and does not control anything else.

* * * * *

And that’s simple boolean logic for you. Of course, game decision-making can often call for much more complicated logic. Look for a future “Stencyl For Noobs” installment that will explain in greater detail how to build more complex decision making.

Advertisements
Posted in Stencyl
2 comments on “Stencyl For Noobs : Intro to Boolean (True/False) Logic
  1. blohger says:

    Thanks for this !! Much appreciated…
    I know a lot of people understand tech verbage but sadly I’m not one of them.

    This is the first newbie friendly explanation I have come across and needless to say I have now bookmarked your blog 🙂

    As this was the first post I came across here, I’m off to read the rest of you have to teach and say.

    I seeyou wrote this post 4 months ago so I hope to come across your above mentioned “future ”Stencyl For Noobs” installment” at some point 😉

    • Photon says:

      Thanks! I actually started writing the next installment but wasn’t particularly pleased with how it was coming out. It then ended up sitting around as a draft, as it currently is now. Looks like I need to take a second look and get it out there!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: