Spielerando

Doors, Keys and Switches

Doors, keys and switches decide where the player may go and when. A door opens by hand, with a key that unlocks it for good, or by a lever that holds it open. It swings for a moment and never shuts on someone standing in the doorway.

Also called: Locked doors, Lever and door, Key and lock, Switch-operated doors.

A small mole in a mint apron turns a big brass key in a glowing lock as a tall arched door begins to swing open, with a pulled lever and a cord to a half-raised shutter beside it.
Quillfern Gatehouse, an invented puzzle adventure game. Invented game, image generated with AI.

Paste this to your AI

Copy it into Claude, Cursor or ChatGPT. It already has good values and the usual mistakes in it.

Add "Doors, Keys and Switches" to my game.

What it should do: Doors give a level its shape: some ways are open, some need a key found elsewhere, and some wait for a lever. The player learns where to go next from what is shut.

Build it exactly as this state machine. First check that it fits my game, then build it like this:

```mermaid
stateDiagram-v2
  [*] --> locked
  locked: Locked
  closed: Closed
  opening: Swinging open
  open: Open
  closing: Swinging shut
  locked --> locked: waits or rattles
  locked --> opening: key turns or lever on
  closed --> closed: waits
  closed --> opening: pressed or lever on
  opening --> opening: keeps swinging
  opening --> open: fully open
  opening --> closing: lever turned off
  open --> open: stays open
  open --> closing: wait ends or pressed
  closing --> closing: keeps swinging
  closing --> opening: blocked or pressed
  closing --> open: swings back, all open
  opening --> closed: lever off, near shut
  opening --> locked: lever off, still locked
  closing --> closed: fully shut
  closing --> locked: shut, lock still on
```

Use these starting values:
- openTime: 0.6 s
- holdOpen: 4 s
- keyCost: 1 items

Watch out for these common mistakes:
- The door shuts on the player: Tell your AI: a door never closes while anything stands in the doorway, and a door that is already closing swings back open.
- A double press undoes itself: Tell your AI: a press shuts the door only when it is fully open. A press while it is still swinging open does nothing.
- A lever opens a locked door and breaks the lock: Tell your AI: the lever only moves the door and never touches the lock. When the lever goes off the door shuts, and it is still locked.
- Keys vanish on the wrong door: Tell your AI: spend keys only at the moment a lock comes off, and only as many as the lock takes. A door without a lock never takes a key.
- A key that should stay is used up: Tell your AI: every lock asks for at least one key in hand, even when it keeps the key.
- Standing in the doorway holds a door open for good: Tell your AI: only count the wait down while the door is fully open and the doorway is clear.

Do not change anything else in the game. Do not claim anything without proof: model what it takes, and show me what your solution rests on and how I can see that it works. If you cannot show something, say you do not know. When you are done, tell me in plain words how to try it.

Or skip this and connect the MCP: your AI then finds all of this by itself.Connect the MCP

What it does

Doors give a level its shape: some ways are open, some need a key found elsewhere, and some wait for a lever. The player learns where to go next from what is shut.

How it runs, as a diagram

Everything it can be in, and what moves it on. Your AI builds from exactly this.

waits or rattleskey turns or lever onwaitspressed or lever onkeeps swingingfully openlever turned offstays openwait ends or pressedkeeps swingingblocked or pressedswings back, all openlever off, near shutlever off, still lockedfully shutshut, lock still onLockedClosedSwinging openOpenSwinging shut
How it runs, as a diagram
  • Locked → Locked: waits or rattles
  • Locked → Swinging open: key turns or lever on
  • Closed → Closed: waits
  • Closed → Swinging open: pressed or lever on
  • Swinging open → Swinging open: keeps swinging
  • Swinging open → Open: fully open
  • Swinging open → Swinging shut: lever turned off
  • Open → Open: stays open
  • Open → Swinging shut: wait ends or pressed
  • Swinging shut → Swinging shut: keeps swinging
  • Swinging shut → Swinging open: blocked or pressed
  • Swinging shut → Open: swings back, all open
  • Swinging open → Closed: lever off, near shut
  • Swinging open → Locked: lever off, still locked
  • Swinging shut → Closed: fully shut
  • Swinging shut → Locked: shut, lock still on

Good starting values

ValueStart withWhat it changes
openTime0.6 sHow long a full swing takes, open or shut. Long enough to see and hear, short enough that nobody waits for it.
holdOpen4 sHow long a door opened by hand stays open once it is all the way open. It has to cover the walk through, with some room for a slow player.
keyCost1 itemsHow many keys the lock takes when it opens. Zero means the key is kept and can open other doors. A lock always asks for at least one key.

What usually goes wrong

The door shuts on the player

A door on a timer closes while the player is still in the doorway. The player is pushed out, stuck in the wall, or trapped on the wrong side.

Fix: Tell your AI: a door never closes while anything stands in the doorway, and a door that is already closing swings back open.

A double press undoes itself

The player taps twice. The first press starts the door opening and the second one closes it again, so the door seems to ignore the player.

Fix: Tell your AI: a press shuts the door only when it is fully open. A press while it is still swinging open does nothing.

A lever opens a locked door and breaks the lock

A lever opens a locked door. When the lever goes off the door either stays open for good or loses its lock, and the key becomes pointless.

Fix: Tell your AI: the lever only moves the door and never touches the lock. When the lever goes off the door shuts, and it is still locked.

Keys vanish on the wrong door

A key is spent when the player presses a door that was already unlocked, or when the lock does not open, and the player is stuck without a key for the next door.

Fix: Tell your AI: spend keys only at the moment a lock comes off, and only as many as the lock takes. A door without a lock never takes a key.

A key that should stay is used up

Some games keep the key after it opens the lock, others use it up. If the code treats a kept key as a cost of zero, a player with no key at all opens the door.

Fix: Tell your AI: every lock asks for at least one key in hand, even when it keeps the key.

Standing in the doorway holds a door open for good

The wait before closing counts down while something blocks the doorway. The moment the player steps out the door slams, with no time left to react.

Fix: Tell your AI: only count the wait down while the door is fully open and the doorway is clear.

How to check it works

Works well with

Common questions

how do locked doors and keys work in games

The player finds a key somewhere in the level and carries it. Pressing a locked door with enough keys takes the lock off for good and opens the door. Without a key the door only rattles.

how long should a door stay open

Start with about four seconds once it is fully open, and a swing of about 0.6 seconds. That covers a walk through with room to spare. Shorter feels like a trap, longer leaves the level looking broken open.

should a key be used up when it opens a door

It depends on the game. A used-up key makes each lock a choice and keeps keys scarce. A kept key is easier to track and suits one master key. Pick one and use it for every lock in the level.

what do i tell my ai to add doors keys and levers

Paste this: Add doors. A press opens a door and it closes after four seconds, never while something stands in the doorway. A locked door needs a key and keeps its lock off once opened. A lever holds a door open until it is switched off and never changes the lock.

Where it comes from

We could not find a reliable origin for this one yet.

Where this mechanic comes from has not been researched yet.

Games are named only as references. Names and marks belong to their owners, and nothing here is endorsed by them.

What was checked

This is what separates the blueprint from a catalogue entry: the values on this page come from code that runs.

Your AI gets the code

Tested example code comes through the Spielerando connection, so your AI can use it right away. How to connect it