Engineering lab
Who gets to unpause the game?
A player pauses. A system panel opens, then closes. Closing it should lift the panel’s hold, not the player’s. Run two small game loops side by side to see exactly where they part ways.
One sequence, two game loops
Each loop has a clock and a piece on a track. “Pass one second” tests game time; “Move one square” sends a player input. Nothing moves on its own. The track wraps after eight squares, but the move count keeps going.
The player pauses. The panel opens, then closes. Now send one second and one move to both games.
Active pause reasons
- Player pauseYes
- Panel openNo
AOne flag
Running
- Game time
- 1 s
- Moves taken
- 1
BBy reason
Paused
- Game time
- 0 s
- Moves taken
- 0
A has resumed. B stays paused because a pause reason is still active.
Send the next event
Event log
| Incoming event | A · One flag | B · By reason |
|---|---|---|
| 1. Player pauses | Paused | Paused |
| 2. Open panel | Paused | Paused |
| 3. Close panelThe results diverge here | Running | Paused |
| 4. Pass one second | Time +1 s | Clock held |
| 5. Move one square | Moved one square | Move ignored |
What a single flag forgets
Consider two starting points: the game is running, or the player has already paused it. When the panel opens, A writes true to the same flag in both cases. Closing the panel writes false. Two different starting points now have the same ending: play resumes.
B keeps track of who is asking the game to stop. A player pause adds player; opening the panel adds panel. Closing it removes only panel. The clock and movement controls work again when the set is empty. A boolean is still useful here: derive it from the set instead of letting every button overwrite it.
const holds = new Set();
const pauseFor = owner => holds.add(owner);
const release = owner => holds.delete(owner);
const canPlay = () => holds.size === 0;
pauseFor("player");
pauseFor("panel");
release("panel");
canPlay(); // falseThis example has one system panel. If inventory and settings can be open together, give them different keys. If two instances of the same panel can overlap, issue a separate token each time one opens and release that same token when it closes. A shared key would let the first panel release the second one’s hold.
Turn the cases into regression tests
- Check what happens after a manual pause. Run “Player pauses first”. The first disagreement is event 3, when the panel closes. B still has zero seconds and zero moves. Choose “Player resumes”: both counts should stay at zero until you send a new second and a new move.
- Also test the path that should resume. Run “Panel only”. Events 2 and 3 land while the panel is open; events 5 and 6 come after it closes. Both games should finish with one second and one move. A game that never resumes would pass a test that only checked whether it stopped.
- Deliver an old notification twice. Run “A late close”. Event 4 is a duplicate close from a panel that is already gone. The player has paused in the meantime. Send “Close panel” once more: B should still be holding the player’s pause.
In a game, gate world updates and gameplay inputs, but leave the resume and close controls working. If the update loop measures the time since the last frame, reset that baseline on resume so the world doesn’t advance by the whole pause. This example also drops moves made while paused; it never plays them back later.