Hold to Use
Before this: Waiting
A door that opens only for a player who holds its button down for two seconds. A status message tells them to keep holding. If they let go early, nothing happens.
Contents
You need
- a Button beside the door
- a door: a few bricks, one of them with a Brick Property Changer
button Held -> chip held
button Character -> chip holder
chip solid -> changer "Collision Enabled"
chip solid -> changer Visibility
Code
// Seconds the button must be held.
const HOLD = 2.0
// Seconds the door stays open.
const OPEN_SECONDS = 5.0
@left in held: bool
@left in holder: character
var opened: bool = false
@right out solid = !opened.Value
let unlock: exec
on held {
holder.ShowStatusMessage("Keep holding...")
await !held || Sleep(_, delay = HOLD)
if held {
holder.ShowStatusMessage("Done!")
emit unlock
} else {
holder.ShowStatusMessage("Too short")
}
}
on unlock {
opened = true
await Sleep(_, delay = OPEN_SECONDS)
opened = false
}
Wire it
- Compile the chip and paste it in-game.
- Wire the Button’s Held to
heldand its Character toholder. - Wire
solidto the Brick Property Changer’s Collision Enabled and Visibility inputs.
Check it in game
- Tap the button: the message says “Keep holding…”, then “Too short”, and the door stays shut.
- Hold the button: after two seconds the message says “Done!” and the door vanishes.
- Five seconds later the door is back.
- Hold for one second, let go, and hold again: the two seconds start over from the second press.
Change it
- Change
HOLDfor a longer or shorter hold, andOPEN_SECONDSfor how long the door stays open. - Count the uses on a sign: add
var uses: int = 0, putuses += 1at the top ofon unlock, and add@right out text = "Used ${uses.Value} times", wired to a Text Decal’s Text. - Put a second button on the far side of the door: add a second pair of
pins and a copy of the
on heldhandler that reads them, and fire the sameunlock.
How it works
The Button’s Held is on for as long as the button is held down, and
on held runs when it turns on (Pure and Exec). The
handler then races two waits
(Waiting): !held turns on the moment the button is
let go, and the Sleep after two seconds. Whichever comes first resumes the
chain, and if held tells them apart: still held means the full two
seconds passed. The unlock signal starts a second handler that holds the
door open with another await. Character is the player who pressed, so
the messages go to them alone.