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.

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.
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
| Value | Start with | What it changes |
|---|---|---|
| openTime | 0.6 s | How long a full swing takes, open or shut. Long enough to see and hear, short enough that nobody waits for it. |
| holdOpen | 4 s | How 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. |
| keyCost | 1 items | How 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
- A locked door and a player with one key. the player presses the door: the lock comes off, the key is used up and the door starts to swing open.
- A locked door and a player with no key. the player presses the door: the door rattles and stays locked and shut.
- A lock that keeps the key, and a player with one key. the player presses the door: the door opens and the player still has the key, but a player with no key still cannot open it.
- A door without a lock and a player with two keys. the player presses it: the door opens and the player still has both keys.
- A door the player opened by hand. nobody touches it and the wait is over: it stays open for the whole wait and then swings shut.
- A door that is fully open. the player presses it: it starts to close at once.
- A door that has just started to swing open. the player presses it a second time: the door keeps opening until it is fully open.
- A door that is closing. something steps into the doorway: the door swings back open and stays open until the doorway is clear.
- A door and a lever. the lever goes on, a long time passes, and the lever goes off: the door opens, stays open past the usual wait, and shuts only after the lever is off.
- A locked door and a lever. the lever goes on and later off: the door opens and shuts again, and it is still locked.
Works well with
- Key Items: The item keeps the player's key count, and the door only asks whether there are enough. Let the key be an item the player picks up, and let this mechanic decide what it does at the door.
- Pressure Plate Puzzles: A plate puzzle can drive the same lever input, so one door works for both. Let the plates decide when the door is wanted open, and let the door handle the swing.
- World State Flags: Without that, a door the player has already unlocked locks itself again after a reload. Save the lock and the lever as flags, and load them before the first step.
- Ability Gates: Use a door when a key opens the way and a gate when an ability does. Both can guard one corridor.
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.
What was checked
- 17 tests pass on the reference code.
- The state diagram was checked against the code, at the default values and at every listed low and high value.
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