Sub topic 13
Implementing Progression as Data
Assume you can read short Python programs, use dictionaries and sets, and work with simple functions. This chapter focuses on a design decision: store progression rules as data, then write general code that interprets those rules.
Separate Rules from Behavior
A data-driven system stores values such as level requirements, prerequisites, and costs outside the functions that process them. The same function can then handle many areas, skills, or rewards.
This separation has practical benefits:
- Designers can tune numbers without changing control flow.
- Content can be added by editing data.
- Tests can check rules independently from the user interface.
- Save files can store player progress using stable names.
The data should describe what is required. The code should explain how to evaluate those requirements.
UNLOCKS = {
"forest": {"level": 1},
"caves": {"level": 5, "requires": ["forest"]},
"boss_gate": {
"level": 10,
"requires": ["caves"],
"items": ["silver_key"],
},
}
SKILLS = {
"slash": {"cost": 1, "requires": []},
"whirlwind": {"cost": 2, "requires": ["slash"]},
"berserk": {"cost": 2, "requires": ["slash"]},
"execute": {"cost": 3, "requires": ["whirlwind", "berserk"]},
}
def is_unlocked(name, player):
rule = UNLOCKS[name]
return (
player["level"] >= rule.get("level", 1)
and all(
requirement in player["completed"]
for requirement in rule.get("requires", [])
)
and all(item in player["items"] for item in rule.get("items", []))
)
def can_learn(name, player):
skill = SKILLS[name]
return (
name not in player["learned"]
and player["skill_points"] >= skill["cost"]
and all(
requirement in player["learned"]
for requirement in skill["requires"]
)
)
player = {
"level": 6,
"completed": {"forest"},
"items": set(),
"learned": {"slash"},
"skill_points": 2,
}
print(is_unlocked("caves", player)) # True
print(is_unlocked("boss_gate", player)) # False
print(can_learn("whirlwind", player)) # True
print(can_learn("execute", player)) # FalseThe get method supplies defaults for optional fields. A rule without requires has no content prerequisites. A rule without items needs no special item. The all function returns true only when every required value exists.
Validate Data Before Play
Data-driven code moves some errors from programming into content authoring. A misspelled prerequisite might leave a skill impossible to learn. A circular dependency might create a tree with no valid starting point.
Validate references when loading the game, not after a player encounters a broken rule.
Useful validation checks include:
- Every prerequisite names an existing skill or area.
- Every cost is zero or greater.
- Every level requirement is at least one.
- No skill requires itself.
- Required content does not form an unintended cycle.
- Each identifier is unique and stable.
Keep player state separate from definitions. The definition says that whirlwind costs two points. The player state says whether this player learned it. This prevents one player's choices from changing the rules for everyone else.
Exercise
Exercise
Why should a skill's cost be stored in the skill data instead of written inside can_learn?
Show answer
Storing the cost in data lets designers tune or add skills without changing the evaluation code.