Sub topic 2
Specifying a verb: preconditions, costs, outcomes
This chapter assumes you know what a player verb is, can read simple Python, and have seen a game loop. It defines a verb with four parts: a precondition, a cost, a cooldown, and an outcome. It then shows how one verb gives different results on different targets, and how verbs combine.
Defining the four parts
A verb is a small specification. Each part answers one question:
- Precondition: Can the player use this verb right now?
- Cost: What does using the verb consume?
- Cooldown: How long must the player wait before using it again?
- Outcome: What happens when the verb succeeds?
| Verb | Precondition | Stamina cost | Cooldown (s) | Outcome on a crate |
|---|---|---|---|---|
| grab | Hands free, target in reach | 5 | 0.5 | Picks up the crate |
| jump | Standing on ground | 10 | 0.2 | Climbs onto the crate |
The following code checks the precondition and then resolves the outcome:
from dataclasses import dataclass
@dataclass(frozen=True)
class Verb:
name: str
stamina_cost: int
cooldown: float # seconds
VERBS = {
"jump": Verb("jump", 10, 0.2),
"grab": Verb("grab", 5, 0.5),
"throw": Verb("throw", 15, 1.0),
}
INTERACTIONS = {
("grab", "crate"): "pick_up",
("throw", "crate"): "launch",
("jump", "crate"): "climb_on",
("grab", "lever"): "pull",
}
def can_use(verb, stamina, now, last_used):
last = last_used.get(verb.name, float("-inf"))
off_cooldown = now - last >= verb.cooldown
return off_cooldown and stamina >= verb.stamina_cost
def resolve(verb_name, target):
return INTERACTIONS.get((verb_name, target), "no_effect")
last_used = {}
print(can_use(VERBS["grab"], 8, 0.0, last_used)) # True
print(resolve("grab", "crate")) # pick_up
print(resolve("throw", "lever")) # no_effectCosts and cooldowns as limits
Costs and cooldowns stop a verb from being spammed. A verb with no limit quickly becomes the only sensible choice. The diagram shows the order of checks.
One verb, many targets
The same verb can produce different outcomes on different targets. The INTERACTIONS table holds this mapping. Any pair missing from the table falls through to no_effect, so gaps in the design are easy to find.
Combining verbs
Combinations create options without adding new verbs. For example, grab followed by throw on a crate can launch it at an enemy. Each combination is a decision, so check that a combination changes the outcome in a way the player can see.
Exercise
The table has ("grab", "crate") but not ("jump", "lever"). What does resolve("jump", "lever") return, and why might that be a problem in a level?
Show answer
It returns "no_effect". In a level, a player who expects a lever to respond to jumping will see nothing happen. Either add the pair to the table or signal clearly that jumping does not work on levers.