Toggle Light
Before this: Reacting to the Game
One button turns a light on, and the next press turns it off again. Put it by a door to light a room.
Contents
You need
- a Button
- a brick with a Point Light
button Held -> chip pressed
chip light -> light Enabled
Code
const START_ON = false // whether the light is on when the chip is pasted
@left in pressed: exec
var lit: bool = START_ON
@right out light = lit.Value
on pressed {
lit = !lit
}
Wire it
- Compile the chip and paste it in-game. Place the Button and the Point Light.
- Wire the Button’s Held to
pressed. - Wire
lightto the Point Light’s Enabled.
Check it in game
- Press the button: the light comes on, and stays on after you let go.
- Press it again: the light goes off.
- Hold the button down: nothing more happens until you let go and press again.
Change it
- Start with the light on: set
START_ON = true. - Add a second button at the other end of the room. Add
@left in pressed2: bool, wire the second Button’s Held to it, makepressedabooltoo, and change the handler toon pressed || pressed2. The OR reads both pins’ values, which is why they arebools. A press on one button while the other is held down does nothing: the OR is already on. - Dim the light instead of switching it off:
@right out light: float = if lit.Value then 4.0 else 0.5.if A then X else Ygives X while A is true and Y otherwise. Wirelightto the Point Light’s Brightness, and leave its Enabled on.
How it works
lit is a variable the chip remembers (Remembering Values). on pressed
runs once each time the button’s Held turns on, and lit = !lit flips
the variable (Pure and Exec). light follows the
variable all the time, so the light shows whatever was stored last.
Toggles and Latches builds the same part
from the game’s Toggle gate, and a latch with separate on and off
buttons.