A roguelite run map in GDScript: branches that never cross, modules that add up into one object, and a save that keeps only the seed

My last post here was about the battle side of a tower defense (enemies as arrays, targeting buckets, budget waves). This one is the layer above it: the run. A branching map you climb, shops and rest stops, and modules that change your towers for the rest of the run. Here’s how it’s put together in plain GDScript, in case it helps with any roguelite.

1. The map is rows of nodes in lanes

The map is 8 rows. Each row gets 2 or 3 nodes in random lanes out of 4, and the top row is the boss. Every node is a small dictionary: its row, lane, kind (battle, elite, shop, rest, boss), the nodes it leads to, the encounter it holds and its own seed.

Node kinds come from weights ({BATTLE: 7, ELITE: 2, SHOP: 1, REST: 1}) with a few caps: at most one shop and one rest stop per row, and at most 3 shops in a whole run.

2. Links that never cross

Crossing lines make a map hard to read, so the rows are joined by “zipping” them left to right, like merging two sorted lists:

var i := 0
var j := 0
_link(a[0], b[0])
while i < a.size() - 1 or j < b.size() - 1:
	if i == a.size() - 1:
		j += 1
	elif j == b.size() - 1:
		i += 1
	else:
		var la: float = nodes[a[i + 1]]["lane"]
		var lb: float = nodes[b[j + 1]]["lane"]
		if la < lb:
			i += 1
		elif lb < la:
			j += 1
		else:
			i += 1
			j += 1
	_link(a[i], b[j])

That gives every node at least one way in and one way out, with no crossings. Then each node has a 50% chance of one extra branch to a neighbor, added only if it still doesn’t cross anything. That’s where the real choices come from.

3. A few rules so every path feels fair

After the map is built, one pass walks it row by row and fixes the dull cases: no elite straight after an elite, no shop straight after a shop, no fight that repeats the one just before it on any path. There’s always a rest stop in the row before the boss and a shop somewhere in the middle. These few lines did more for how runs feel than any amount of weight tuning.

4. Modules add up into one object the battle reads

A module is a resource with an effect (DAMAGE, RANGE, RATE, SPLASH, SLOW, CHAIN, START_GOLD, BOUNTY, DISCOUNT, WAVE_BONUS, BLUEPRINT), an amount, and optionally the tower it applies to. The battle never looks at the module list. Instead, once per battle, the run adds everything up into a RunMods:

func add(m: ModuleData, stacks: int = 1) -> void:
	var a := m.amount * stacks
	match m.effect:
		ModuleData.Effect.DAMAGE:
			damage[String(m.tower)] = float(damage.get(String(m.tower), 0.0)) + a / 100.0
		ModuleData.Effect.DISCOUNT:
			discount = minf(discount + a / 100.0, 0.5)
		# ...

The tower code just asks, for example, damage * (1.0 + mods.pct(mods.damage, tower_id)), where pct() adds the bonus for every tower to the bonus for that tower. Stacking works for free, caps live in one place (discounts stop at 50%), and a new module that uses an existing effect is a new resource file, no code.

5. The save keeps the seed, not the map

Between battles the run saves lives, cores, modules, the path so far and the random generator’s state, but not the map. On load it seeds the generator, rebuilds the map from that seed, then restores the generator’s state so everything after carries on as if nothing happened:

r.rng.seed = r.seed_value
r.map = RunMap.generate(r.rng, Game.encounters)    # same seed, same map
r.rng.state = int(String(d["rng"]))

The save file stays tiny, and there’s no chance of the saved map and the code drifting apart.


This is all in Tower Defense Kit, a complete roguelite tower defense template for Godot 4: 6 towers, 16 modules, elites and a boss, the run map, a shop and permanent upgrades, with everything as resources you edit in the Inspector:

The free browser demo plays the first 3 steps of a run, map included: Tower Defense Kit - Playable Demo by Spare Parts Studio

Happy to answer questions about any of it.

1 Like