← Pixel Dreamer Studio

Fire that spreads one cell per step, in every direction

Read the grid as it was, write the changes into a copy, then swap.

If your loop reads a cell and sets its neighbours on fire in the same grid it is reading, it reaches those new fires later in the same loop and spreads them again, so the fire races in the direction the loop runs. In our Godot 4.7 test a fire in the middle of a 21 by 21 field burned 121 cells in one step, 10 cells right and 10 down but only 1 left and 1 up. A TileMapLayer read and written in one loop did exactly the same. Writing the changes into a copy of the grid gave 4 burning cells, one in each direction.

The code

Save it as fire_spread.gd, load it with const FireSpread := preload("res://fire_spread.gd") and keep the field as an array of rows (copy your TileMapLayer into one). Call grid = FireSpread.step(grid) once per tick, for example from a Timer, then draw the grid. For fire that only catches some of the time, pass a chance and your own RandomNumberGenerator: FireSpread.step(grid, 0.5, rng).

extends RefCounted
## Spreads fire across a grid one step at a time (grass catches, fire burns
## out to ash). Works the same for water, poison gas, infection or a growing
## crop field: any cell that changes its neighbours.
## step() reads the old grid and writes a new one, so fire moves at most one
## cell per step in every direction, instead of racing across the grid in the
## direction your loop runs.
##   grid = FireSpread.step(grid)               # every grass neighbour catches
##   grid = FireSpread.step(grid, 0.5, rng)     # each one catches half the time

const EMPTY := 0
const GRASS := 1
const FIRE := 2
const ASH := 3

const SIDES: Array[Vector2i] = [Vector2i(1, 0), Vector2i(-1, 0), Vector2i(0, 1), Vector2i(0, -1)]


static func step(grid: Array, chance := 1.0, rng: RandomNumberGenerator = null) -> Array:
	var h := grid.size()
	var w: int = (grid[0] as Array).size()
	var next: Array = []
	for row in grid:
		next.append((row as Array).duplicate())  # write here, read the old grid
	for y in h:
		for x in w:
			if grid[y][x] != FIRE:
				continue
			next[y][x] = ASH  # burns for one step
			for d in SIDES:
				var nx := x + d.x
				var ny := y + d.y
				if nx < 0 or ny < 0 or nx >= w or ny >= h:
					continue
				if grid[ny][nx] == GRASS and (chance >= 1.0 or _roll(rng) < chance):
					next[ny][nx] = FIRE
	return next


static func _roll(rng: RandomNumberGenerator) -> float:
	return rng.randf() if rng != null else randf()


static func count(grid: Array, what: int) -> int:
	var n := 0
	for row in grid:
		n += (row as Array).count(what)
	return n

What we measured

A 21 by 21 field of grass with fire in the middle cell. A burning cell turns to ash after one step and sets its four grass neighbours on fire. Two runs gave the same numbers.

Why it happens

The loop goes through the rows top to bottom and each row left to right. When it sets the cell on its right on fire, that is the very next cell it looks at, and it cannot tell that this fire started a moment ago, so it spreads it again, all the way to the edge. The row below gets the same treatment. Cells up and to the left were already passed, so they wait for the next step. Any rule where cells change their neighbours has the same trap: water flowing, gas spreading, a disease moving through a town. The fix is two grids: decide every change from the old one and write it into the new one.

What to change

The flames themselves: Fire, Ice, Wind & Earth Elemental VFX for Godot 4 has 52 ready-made spells, including a fire wall and a flamethrower.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Fire, Ice, Wind & Earth Elemental VFX for Godot 4 has 52 ready-made spells, including a fire wall and a flamethrower.

More how-tos for Fire, Ice, Wind & Earth Elemental VFX for Godot 4: Fire that turns to smoke in a cone · Bright fire with dark smoke

More how-tos: Game systems