Sub topic 8
Unlock Conditions and Prerequisites
This chapter covers how to express the rules that decide whether a player may enter an area, use a feature, or learn an ability. It assumes you have read the previous chapter on unlocks and gating, so you know why a game gates content at all. You should also be able to read short Python programs and understand dictionaries and sets.
The working model
An unlock condition is a yes/no question about the player's state: may this player have this thing right now? A prerequisite is a condition that depends on something the player has already done or owns. The vocabulary is loose in practice, so this chapter uses these terms:
- Target: the thing being unlocked, such as an area, a quest, a recipe, or a skill.
- Condition: one test on the player's state.
- Rule: the full set of conditions attached to one target.
Most conditions fall into a few kinds:
| Kind | Question it asks | Typical use |
|---|---|---|
| Level or stat threshold | Is the player strong enough? | Zone entry, equipment |
| Completed content | Has the player finished X? | Story chains, dungeon sequences |
| Item or currency | Does the player own X? | Keys, tickets, crafting |
| Time or event | Is it the right day or season? | Live events, daily resets |
| Social or account | Is the player in a guild, or did they buy the expansion? | Guild halls, paid content |
The first three kinds match the code later in this chapter. The last two work the same way but depend on state outside the player's save file, such as a server clock or an account record. They need extra care because the player cannot control them.
Combining conditions
A rule with several conditions usually means "all of these" (AND). That is the simplest model and the easiest for players to read: a checklist where every box must be ticked.
Sometimes you want alternatives (OR). For example, a locked door might open with a silver key or a lockpicking skill of 30. OR rules give players more routes, which suits games that reward different builds. They also make rules harder to explain, because the interface has to show several valid paths.
A sensible default is AND within a rule and separate rules for alternative routes. If you need many OR cases, write a small structure such as {"any": [...]} instead of adding special cases to the checking code.
Rules as data
Storing rules as data keeps design decisions out of the code. A designer can change "caves unlock at level 5" without touching program logic, and one function checks every target the same way.
UNLOCKS = {
"forest": {"level": 1},
"caves": {"level": 5, "requires": ["forest"]},
"boss_gate": {"level": 10, "requires": ["caves"], "items": ["silver_key"]},
}
def is_unlocked(name, player):
rule = UNLOCKS[name]
if player["level"] < rule.get("level", 1):
return False
if any(r not in player["completed"] for r in rule.get("requires", [])):
return False
return all(i in player["items"] for i in rule.get("items", []))
player = {"level": 6, "completed": {"forest"}, "items": set()}
print(is_unlocked("caves", player))
print(is_unlocked("boss_gate", player))This prints True then False. The player is level 6 and has finished the forest, so the caves are open. The boss gate fails for two reasons: the level is below 10, and the player has no silver key. The function stops at the first failure, which is correct for a yes/no answer but hides the second problem. That leads to the first common error.
Common error: silent locks
A player who sees a locked door and no explanation will assume the game is broken. A locked target should always say what is missing. To do that, collect every failing condition instead of stopping at the first one.
UNLOCKS = {
"forest": {"level": 1},
"caves": {"level": 5, "requires": ["forest"]},
"boss_gate": {"level": 10, "requires": ["caves"], "items": ["silver_key"]},
}
def lock_reasons(name, player):
"""Return a list of messages describing what is still missing."""
rule = UNLOCKS[name]
reasons = []
need = rule.get("level", 1)
if player["level"] < need:
reasons.append(f"Reach level {need} (you are level {player['level']})")
for r in rule.get("requires", []):
if r not in player["completed"]:
reasons.append(f"Complete {r}")
for i in rule.get("items", []):
if i not in player["items"]:
reasons.append(f"Obtain {i}")
return reasons
player = {"level": 6, "completed": {"forest"}, "items": set()}
for reason in lock_reasons("boss_gate", player):
print("-", reason)The output is:
- Reach level 10 (you are level 6)
- Complete caves
- Obtain silver_keyNow is_unlocked can be defined as not lock_reasons(...), so the yes/no answer and the explanation can never disagree. Keeping a single source of truth is the main design benefit of this approach.
The check order below is the one both functions follow.
Common error: broken prerequisite graphs
Prerequisites form a graph. Each requires entry is an arrow from one target to another. Two mistakes are easy to make as the data grows.
- Unknown references. A rule names content that does not exist, often after a rename. The target can never unlock, and nothing reports why.
- Cycles. Content A requires B and B requires A, directly or through a longer chain. Nobody can ever enter any of them.
Both are data errors, so catch them with a validator that runs on every build, not by hoping a playtester finds them.
def validate(unlocks):
errors = []
for name, rule in unlocks.items():
for r in rule.get("requires", []):
if r not in unlocks:
errors.append(f"{name} requires unknown content {r}")
state = {}
def visit(node, path):
if state.get(node) == "done":
return
if state.get(node) == "active":
errors.append("cycle: " + " -> ".join(path + [node]))
return
state[node] = "active"
for r in unlocks[node].get("requires", []):
if r in unlocks:
visit(r, path + [node])
state[node] = "done"
for node in unlocks:
visit(node, [])
return errors
BAD = {
"a": {"requires": ["b"]},
"b": {"requires": ["a"]},
"c": {"requires": ["ghost"]},
}
print(validate(BAD))This prints ['c requires unknown content ghost', 'cycle: a -> b -> a']. The function uses depth-first search. A node marked active is on the current path, so reaching it again means a cycle. A node marked done was fully explored earlier and is safe to skip.
A third check, that every target is reachable from the starting content, needs the same traversal and is worth adding for large games.
Designing conditions that are fair
Correct code can still produce unfair gates. Keep these points in mind.
- Gate on the right thing. A level requirement says "you are strong enough". A completion requirement says "you have seen the earlier story". Use whichever matches your reason for the gate. Do not stack a level gate on top of a story gate if only one of them matters.
- Avoid hard locks. A hard lock is a state from which the player cannot progress. Consumable keys are the classic cause. If a boss gate consumes the silver key and the player then loses a fight, they must be able to get another key. Either make the key reusable, or make sure it can be obtained again.
- Make requirements visible before they matter. Show the locked target and its requirements early. Players plan around goals they can see.
- Watch missable content. If a prerequisite is a one-time event, a player who skips it may be locked out of later content permanently. Either allow a catch-up path or flag the content clearly.
- Keep completion durable. Store "completed" flags in the save data so they survive death, patches, and respecs. Recomputing completion from current inventory is fragile, because selling a quest item would then re-lock the area.
Testing rules
Because rules are data and the checker is a pure function (the same inputs always give the same answer, and it changes nothing), tests are short. Build a few sample players, such as a brand-new player, a mid-game player, and a player with every item and no progress, and assert the expected result for each target. Add one test that walks the whole graph by repeatedly unlocking whatever is available and confirms every target is eventually reached. If that walk stalls before it covers everything, the data has a hard lock or a cycle.
Exercise
A rule says "boss_gate": {"level": 10, "requires": ["caves"], "items": ["silver_key"]}. A player is level 12, has completed the caves, and owns no items. What does is_unlocked("boss_gate", player) return, and what does lock_reasons return?
Show answer
is_unlocked returns False because the item check fails. lock_reasons returns one message, "Obtain silver_key", since the level and completion conditions pass.
Exercise
Content X requires Y, Y requires Z, and Z requires X. The validator's cycle check marks each node active while it explores. Explain how it detects this loop, and say what the player experiences if the loop reaches production.
Show answer
Starting at X, the search visits Y, then Z, then asks about X again. X is still marked active, so the validator reports the cycle X -> Y -> Z -> X. In production none of the three can ever be unlocked, because each waits on another that is also locked, and the player sees locked content with no way to open it.