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.
- Changing the grid while looping over it: after one step, 22 cells burning and 121 burnt, reaching 10 cells right and 10 down, 1 left and 1 up.
- The same loop on a TileMapLayer, with
get_cell_atlas_coords()andset_cell(): the same 22 burning and 121 burnt. - The script: 4 burning, 1 burnt, 1 cell in every direction. After three steps, 12 burning, 13 burnt and 3 cells in every direction.
- A 50% chance per neighbour, 4 steps, 2,000 fires: changing the grid in the loop reached 5.29 cells right and 5.36 down on average but 2.38 left and up, and hit 49.2 cells. The script: 1.79 to 1.85 in every direction and 13.2 cells.
- Both burned the whole field in 21 steps, because the in-place fire still crawls up and left one cell per step. What is wrong is the shape and the speed in two directions.
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
- A sprinkler or area spell that should cover a round patch of tiles: tiles within the radius plus half a tile.
- For a very big map, keep a list of the burning cells and build the next list, instead of copying the whole grid each step.
- A fire that should burn the same way in a replay: give it a RandomNumberGenerator with a seed, see same seed, different results. Hurting whoever stands in the flames: a damage zone. Fire that turns to smoke as it dies: fire to smoke particles.
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
- Hover a 3D object inside a SubViewport
- A turned building that covers the cells it stands on
- An inventory that never loses an item and never overfills a stack
- A JSON save that loads back exactly what you saved
- A pooled enemy that comes back as fresh as a new one
- An enemy that stops at the dead player and finds the respawned one
- Poison, burn and bleed that keep ticking when applied again
- XP, qi and gold that keep growing without going negative
- Count slot line wins with wilds in front
- Random directions spread evenly over a sphere or a cone
- Count a lap only when the car really went round
- Make a seed repeat: daily runs, replays and roguelike seeds
- Offline progress that counts the time the game was closed
- A chance roll without the long bad-luck streaks
- Check crafting recipes in any order, with the right counts
- Make HP and mana regen the same at any frame rate
- Damage over time with the right number of ticks
- Spawn enemies where the camera cannot see them
- Keep a chasing horde from stacking into one enemy
- Make the fish bite after the same wait at any frame rate
- Snap buildings to the right grid cell on both sides of the origin
- Make a cooking timer fire once, on time
- Pick from a weighted loot table with the right chances
- Level up as many times as the XP covers
- Walk a patrol guard to each point without overshooting
- Remove items from an Array while checking them, without skipping any
- Scatter spikes, roots or trees in a circle without overlaps
- Count and take crafting items from every stack
- Play random sounds and events without obvious repeats
- Pick the growth image so the ripe one shows exactly when the crop can be picked
- Turn a day counter into season, day and year without a day 0
- An enemy that dies once, however many hits land
- A fair shuffle you can replay
- A ghost that drives the lap at real speed
- A vision cone that does not flicker at the edge
- Move a group of units without a pile-up
- A sprinkler that waters a round patch
- A slot reel that always lands forward on the result
- A crowd of NPCs that never emote or bob in step
- A song position that moves on every frame
- Nodes your editor tool adds that stay in the scene