Sub topic 2
Stocks and flows: what accumulates and what changes it
This chapter assumes you can read simple arithmetic formulas and percentages. The code examples are in Python, but you can follow the chapter without running them. The previous chapter treated a game as a system. Here we take that system apart into two kinds of parts: the things that pile up and the things that change the piles.
Stocks: what you can count when time stops
A stock is a quantity that accumulates over time. It has a value at any instant, and you could read it off the screen if the game were paused. Gold, health, ammunition, territory, cards in hand, and points are all stocks.
A flow is a rate of change. It is measured per unit of time (per turn, per second, per round). Income of 5 gold per turn, poison damage of 3 health per turn, and a build queue that finishes 1 unit per turn are all flows.
A freeze test tells them apart. Stop the game and ask whether the quantity still has a value.
- "I have 120 gold" still makes sense when time is frozen, so gold is a stock.
- "I earn 8 gold per turn" has no meaning in a single frozen frame, so it is a flow.
A bathtub is the usual picture. The water level is the stock. The tap is an inflow, and the drain is an outflow.
The one equation
Every stock follows the same rule:
stock(next) = stock(now) + inflows - outflowsThe difference between inflows and outflows is the net flow. The sign of the net flow tells you what the stock does:
| Net flow | What the stock does |
|---|---|
| Positive (inflow larger) | Grows |
| Negative (outflow larger) | Shrinks |
| Zero | Holds steady (an **equilibrium**) |
Two consequences matter for design:
- A stock can only change through its flows. If a player's gold jumps unexpectedly, some flow you did not account for caused it.
- A stock has memory and a flow does not. A big income this turn is gone next turn. The gold it produced stays until something drains it. This is why early advantages in many games last so long.
Stocks in a real game
Pick any game and list its stocks, then list the flows touching each one.
| Game | Stock | Inflows | Outflows |
|---|---|---|---|
| Real-time strategy | Gold | Mining, selling | Building, upkeep |
| Real-time strategy | Army size | Training | Combat losses |
| Racing | Boost meter | Drifting, pickups | Using boost |
| Card game | Cards in hand | Drawing | Playing, discarding |
| Platformer | Lives | Extra-life pickups | Deaths |
Some stocks are less obvious. These hidden stocks matter just as much:
- Map control or board position. Territory you hold keeps paying off after the action that won it.
- Tempo. Having more actions available than your opponent is a stock that accumulates.
- Player knowledge. What a player has learned about the game builds up with play and does not drain.
- Frustration. A player's tolerance for repeated losses behaves like a stock, even though no counter shows it.
Hidden stocks are easy to miss when you analyze a game, so ask what lingers after the action that created it.
Flows that depend on the stock
In the simplest case, flows are constants. The gold example below adds 5 per turn and removes 3. Many flows in real games are not constants, because they read the current level of a stock:
- Interest pays 10% of the gold you hold. The income depends on the stock.
- Regeneration might restore a fixed fraction of missing health.
- An army's casualties depend on how many units are in the fight.
When a flow reads the stock it feeds or drains, you have the raw material of a feedback loop. This chapter only sets up that idea, and the next chapter develops it. For now, notice that "flow depends on stock" is a different situation from "flow is fixed."
Simulating a stock
Here is the smallest useful model: one stock, one fixed inflow, one fixed outflow.
def simulate(turns, start_gold=20, income=5, upkeep=3):
gold = start_gold
history = [gold]
for _ in range(turns):
inflow = income
outflow = upkeep
gold += inflow - outflow
history.append(gold)
return history
print(simulate(5))This prints [20, 22, 24, 26, 28, 30]. The net flow is +2 per turn, so the stock rises by 2 each turn. If you set upkeep=5, the net flow is 0 and the history stays at 20 forever. That is an equilibrium: the flows are still running, but the stock does not move.
Connecting stocks: conversion
Stocks often feed each other. Gold becomes soldiers, wood becomes buildings, and mana becomes spells. In a model, one flow's outflow from stock A is another flow's inflow into stock B.
def run(turns):
gold, army = 30, 0
log = []
for turn in range(1, turns + 1):
income = 8
training = min(gold // 10, 2) # soldiers trained: 10 gold each, max 2 per turn
gold += income - training * 10
army += training
log.append((turn, gold, army))
return log
for row in run(6):
print(row)The output is:
(1, 18, 2)
(2, 16, 3)
(3, 14, 4)
(4, 12, 5)
(5, 10, 6)
(6, 8, 7)Three details in this code are worth studying:
- Both flows are computed before either stock is updated.
trainingreads the gold at the start of the turn. If you updatedgoldfirst and then computedtraining, the result would depend on the order of your code lines rather than on the design. - The
min(...)limits the outflow to what the stock can pay. Gold cannot be spent below zero, and training cannot exceed 2 per turn. These are rules about flows. - Gold falls while the army grows. Gold is not "lost." It converts into a different stock. Tracking this conversion is how you check that your design does not create or destroy value by accident.
Edge cases and common errors
Confusing a flow with a stock. "Damage per second" is a flow. "Total damage dealt" is a stock. Balance discussions go wrong when people argue about one while meaning the other. A weapon with high damage per second can still lose to one with a bigger magazine if the stock of ammunition runs out first.
Stocks that go negative. A stock of gold, units, or ammo cannot hold less than zero. In code, either cap the outflow at the current stock, as min(gold // 10, 2) does, or clamp afterward with max(0, ...). Decide which behavior you want, because they differ: capping the flow makes a purchase fail, while clamping silently forgives a debt.
Stocks with no outflow. If a stock only has inflows, it grows without limit. Score is the standard acceptable example. A resource with no sink, though, will pile up until it stops being interesting. Players hoard a currency when nothing worth buying drains it. A drain is called a sink, and a source of new value is called a source. Economy designers check that every source has a matching sink.
Units of time. Make sure every flow in a model uses the same time unit. Mixing "per second" and "per turn" is a frequent bug, and it produces numbers that look plausible but are wrong.
Derived values are not stocks. A player's "power level" computed from gold, army, and tech is a readout, not something that accumulates on its own. Model the underlying stocks and compute the readout from them.
What to carry forward
- A stock is a quantity you can read in a frozen moment. A flow is its rate of change.
- A stock changes only through its inflows and outflows, and its next value is the current value plus the net flow.
- Zero net flow means equilibrium. A positive or negative net flow means growth or decline.
- Flows can be fixed, or they can read stocks. The second kind sets up feedback loops in the next chapter.
- In a model, compute all flows from the same snapshot before updating any stock.
Exercise
A player starts with 40 health. Each turn they regenerate 2 health and take 5 poison damage, and health cannot go below 0. After how many turns does their health first reach 0? Name the stock, the inflow, and the outflow.
Show answer
The stock is health. The inflow is regeneration (+2 per turn) and the outflow is poison damage (5 per turn), so the net flow is -3 per turn. After 13 turns health is 40 - 39 = 1. After 14 turns it would be -2, so it is clamped to 0. Health first reaches 0 on turn 14.
Exercise
In a shooter, which of these are stocks and which are flows: kills per minute, total kills, ammunition in the magazine, reload speed in rounds per second?
Show answer
Total kills and ammunition in the magazine are stocks, because each has a value you can read when the game is paused. Kills per minute and reload speed (rounds per second) are flows, because each is a rate that only makes sense over time.