Boolean Basics

By: InstantKafka (Jan 2015)
Copied from Project Spark.com from Wayback Machine

I noticed that quite a few people struggle with the concept of booleans and what they’re used for. Booleans are fundamental to how Project Spark works and it’s important to know what they are. Really, the biggest mystery to booleans is the name: ‘boolean’. It’s not a word that comes up in everyday conversation, but the concept should be familiar.

A boolean is nothing more than a value that can either be TRUE or FALSE. That’s it! Mystery solved! Any time you say something starting with the word ‘if’ or ‘when’, you’re using a boolean. “If it’s cloudy, I’ll bring my umbrella.” It will either be cloudy (TRUE) or it will not (FALSE). “When the water boils, add the pasta”. The water will either be boiling (TRUE) or it will not (FALSE).

Every line of Kode has a WHEN side and a DO side. Whatever is put into the WHEN side of a Kode line will be evaluated as a boolean. If the result is TRUE, the rest of the line is executed (along with any child Kode lines). If the result is FALSE, then the next line of Kode that isn’t a child of the current line is executed (unless you use [else]. I’ll discuss that in a bit).

Some examples:

Quote:

WHEN: [player][is dead] DO: … something if the player is dead …
WHEN: [on ground] DO: … something if the current object is on the ground …
WHEN: [has potion][equals][true] DO: … something if the value of [has potion] is TRUE …

That last one is interesting because it uses a user-defined boolean variable called “has potion”. This is a variable that is set to TRUE or FALSE somewhere else in the Kode prior to executing this line. Maybe the character killed a goblin earlier and looted a potion from the goblin’s corpse. Who knows? The important thing is that we can store whether or not the character has a potion in a variable that can be used later. We can check the value of this variable as many times as we want, like just before drinking the potion or before the character can pass a magic door. We can change the value of this variable any time we want, like setting it to FALSE if the character drinks the potion.

Here’s another example:

Quote:

WHEN: [number of lives][less than][1] DO: … game over …

This one doesn’t actually check a boolean value, but it’s still a boolean expression (i.e. a condition that evaluates to either TRUE or FALSE). The character’s number of lives, which happens to be a user-defined number variable, will either be less than 1 (TRUE); or greater than or equal to 1 (FALSE). There are many comparisons that can be made that result in TRUE or FALSE, such as [equals], [not equals], [greater than], [less than], [greater than or equal to], [less than or equal to], and [contains].

Many inputs from the player are booleans. The A, B, X & Y buttons can either be TRUE (pressed) or FALSE (not pressed). Same with the mouse buttons and keys on the keyboard. Other inputs, like left/right triggers, left/right sticks, and mouse position can also be evaluated as TRUE or FALSE, but they’re kind of special because they are not intrinsically boolean values. Trigger values are actually numbers from 0.0 (not pressed) to 1.0 (fully pressed) with many values in-between to represent the amount the trigger is pressed in. Stick values are vectors where the x-component can be from -1 to 1 representing how far to the left (-1) or right (1) the stick is; and the y-component can be from -1 to 1 representing how far down (-1) or up (1) the stick is; the z-coordinate is always zero because the stick only moves in two axes. The reason we can also use these inputs as boolean values is because zero is always converted to FALSE and any other non-zero value is converted to TRUE. The value of a trigger that is not pressed is 0, so it’s converted to FALSE. If it’s pressed in at any amount other than 0, it’s converted to TRUE. The value of a stick that is not moved is (0,0,0), so it’s converted to FALSE. If it’s moved in any direction by any amount, it’s converted to TRUE.

Multiple boolean expressions can be combined in various interesting ways using AND, OR, and NOT. They’re fairly self-explanatory, but they can get confusing when grouped in different ways. AND means if both conditions are TRUE, then the result is TRUE. If either condition is FALSE, then the whole expression is FALSE. OR means if either condition is TRUE, then the result is TRUE. If both conditions are FALSE, then the whole expression is FALSE. NOT means if the condition is TRUE, then the result is FALSE and vice versa. Please note that both AND and OR combine two and ONLY two boolean expressions. NOT simply inverts a single boolean expression. That doesn’t mean you can only combine two boolean expressions on a single line of Kode, you just need to be aware that there is a particular order to how each pair is evaluated. Take a look at this example:

Quote:

WHEN: [false][and][true][or][true][and][true] DO: … stuff …

Does “stuff” happen? If you said, “yes”, then you win a lollipop! Each pair of boolean expressions are evaluated in order from left to right and the result is applied to the next pair. So first, combine the first two boolean expressions with AND:

Quote:

WHEN: [false][and][true][or][true][and][true] DO: … stuff …
WHEN: [false][or][true][and][true] DO: … stuff …

Next, combine the result with the next expression using OR:

Quote:

WHEN: [false][or][true][and][true] DO: … stuff …
WHEN: [true][and][true] DO: … stuff …

Obviously, TRUE and TRUE = TRUE, so the final result is TRUE and “stuff” is executed.

We can change how the expressions are grouped or the order in which they’re evaluated using parentheses ()’s. Consider the same line as before but grouped differently:

Quote:

WHEN: [false][and][(][true][or][true][)][and][true] DO: … stuff …

Any expressions inside parentheses are evaluated before anything else. They have a “higher order of precedence”. So the whole line is evaluated like this:

Quote:

WHEN: [false][and][(][true][or][true][)][and][true] DO: … stuff …
WHEN: [false][and][(][true][)][and][true] DO: … stuff …
WHEN: [false][and][true] DO: … stuff …
WHEN: [false] DO: … stuff …

In this case, “stuff” is never done. Evaluating the expressions in a different way produced a different result. The takeaway from all this is be mindful of the order in which boolean expressions are evaluated and use parentheses to force the order, if needed.

Oh! I almost forgot about the [else] tile. You can use [else] to specify that something else should happen when the parent boolean expression is FALSE. Here’s an example:

Quote:

WHEN: [is flying] DO: [say][Weeeee! I’m a bird!]
–> WHEN: [else] DO: [say][Awww! I wish I could fly.]

If the object is flying, then it says, “Weeeee! I’m a bird!”; Otherwise, it says, “Awww! I wish I could fly.” Notice that the line with [else] is a child of the first line (i.e. it’s indented). This is important in order to tie the [else] with the original boolean expression.

That’s about all I can think of. If I missed anything or you have any questions, please let me know.

Here’s the TL;DR bullet list

 

  • Booleans are either TRUE or FALSE
  • Numbers can be assigned to Boolean variables. The number zero (0) is interpreted as FALSE and any other number is interpreted as TRUE.
  • Unfortunately, the converse doesn’t seem to be true. I can’t find a way to assign a Boolean value to a number variable.
  • You can test if a Boolean is TRUE on the When side by just using the variable. No need for [equals] and [true]:

    W: [my Boolean variable] D: [stuff if it’s true]
    instead of
    W: [my Boolean variable][equals][true] D: [stuff if it’s true]

    Use an [else] tile in a child line for the logic if it’s false

  • Two Boolean expressions can be combined using AND. It’s TRUE if both expressions are TRUE.
  • Two Boolean expressions can be combined using OR. It’s TRUE if either expression (or both) are TRUE.
  • You can use the [not] tile to check the inverse or opposite value of a Boolean

    W: [not] [my Boolean variable] D: [stuff if it’s false]

  • Using [not] in an assignment switches the value from true to false or vice versa.

    W: [condition] D: [my Boolean variable] [equals] [not] [my Boolean variable]

  • Or, you can use the [toggle] tile to toggle the value

    W: [condition] D: [toggle][my Boolean variable]

Comments are closed.

Powered by WordPress.com.

Up ↑

%d bloggers like this: