True and False
Before this: Numbers
Next steps: Vectors and Colors, Object References
A bool is the simplest value: true or false. Switches and buttons give
bools, lights take them, and every comparison makes one. This lesson also
shows what happens when a bool meets a number.
Contents
switch1 Enabled -> chip a
switch2 Enabled -> chip b
switch3 Enabled -> chip c
chip text -> decal Text
chip passed -> light Enabled
Code
@left in a: bool
@left in b: bool
@left in c: bool
let votes = a + b + c
@right out text = "${votes} of 3 say yes"
@right out passed = votes >= 2
Each of three judges has a switch. When a bool is used in arithmetic, true
counts as 1 and false as 0, so a + b + c is the number of switches that
are on.
votes >= 2 compares two numbers and gives a bool: true when there are at
least two yes votes. The other comparisons work the same way: == (equal),
!= (not equal), <, >, <= and >=.
With two switches on, what does the sign say, and is the light on?
Run
- Compile the chip and paste it in-game. Place three Switches, a brick with a Text Decal, and a Point Light.
- Wire each Switch’s Enabled to
a,bandc. Wiretextto the Text Decal’s Text andpassedto the Point Light’s Enabled. - Flip the switches. The sign counts the yes votes, and the light is on with two or three of them.
Under the Hood
Open the chip. Two Add gates sum the three pins, and a Greater/Equal gate compares the total with 2. No gate turns the bools into numbers first: the Add gate takes them as they are.
A comparison is a gate like any other, and its bool output can go anywhere a
bool can: a light, an if, a variable, or another operator such as &&.
Modify
-
Show each vote on the sign:
"${a} ${b} ${c}: ${votes} of 3". A bool in text shows astrueorfalse. -
Give judge
atwo votes:let votes = a * 2 + b + c. Nowaand one other judge are enough, butbandcalone are not. -
Combine comparisons on a stored number. This chip counts presses and its light is on only for even counts above 5:
@left in pressed: exec var count: int = 0 let isEven = count.Value % 2 == 0 let big = count.Value > 5 @right out text = "${count.Value}: even ${isEven}, big ${big}" @right out lamp = isEven && big on pressed { count += 1 }count.Value % 2 == 0reads as “the remainder after dividing by 2 is 0”.%comes before==, as*comes before+.
Make
Make a “two-key” vault door: two switches far apart must both be on, and the door opens only if the number of presses on a third button is exactly 3. Wire the result to a Brick Property Changer’s Collision Enabled and Visibility so the door vanishes when it opens.
switch1 Enabled -> chip key1
switch2 Enabled -> chip key2
button Held -> chip pressed
chip solid -> changer "Collision Enabled"
chip solid -> changer Visibility
Good to Know
- A number used where a bool is wanted is
truewhen it is not zero, so-1as a condition is true. - But comparing a bool with a number compares it as exactly 1 or 0:
1 == trueis true, and-1 == trueis false. a == bon two bools is true when they match, both on or both off: the opposite of^^.
Next: Vectors and Colors.