Gridwright Start for $1

Test an Autotile in Godot: Fix Terrain Peering Bits

How to test an autotile in Godot 4 and set its terrain peering bits: the three terrain modes, the bit names, and why a missing tile leaves a hole. Checked in 4.7.2.

Autotile Tester at work
Autotile Tester at work: 47-tile and 16-tile autotiles checked on a real map, with Godot's peering bits set for you.

You drew a 47-tile autotile, or bought one, and it looks right on its sheet. You import it into Godot and spend an evening clicking tiles in the TileSet editor, one peering bit at a time, 8 bits on each of 47 tiles. Then you paint a first lake and a corner is a hole, a coast is two pixels off, or one tile sits in the wrong place. You find out hours after you made the set, in the middle of a level.

The bits are the slow part, and a broken set is the part you want to know about first. Here is what Godot does with the bits, checked in Godot 4.7.2, and how to test a set before it goes anywhere near your game.

The three terrain modes

A terrain set in Godot has a mode. It decides which neighbours a tile has to agree with, and so how many tiles you need.

Top-Down Tileset
Top-Down Tileset: 16x16 top-down pixel art tileset with real autotiles: grass, sand, snow, water, cliffs, props. One of the 150 packs in Gridwright.
  • Match Corners and Sides (TileSet.TERRAIN_MODE_MATCH_CORNERS_AND_SIDES, value 0): 8 neighbours. This is the blob set, 47 distinct tiles out of 256 possible masks.
  • Match Corners (TERRAIN_MODE_MATCH_CORNERS, value 1): only the 4 corners count. A full set is 16 tiles.
  • Match Sides (TERRAIN_MODE_MATCH_SIDES, value 2): only the 4 sides count. A full set is 16 tiles. Fences and walls usually use it.

Pick the mode that matches how your tiles were drawn. A 16-tile set in the 8-neighbour mode has most masks missing, and a 47-tile set in a 4-neighbour mode ignores half of what you drew.

We asked a square-grid TileSet which bits each mode accepts, with TileData.is_valid_terrain_peering_bit(). The result:

ModeValid bits
Match Corners and Sides0, 3, 4, 7, 8, 11, 12, 15 (all 8)
Match Corners3, 7, 11, 15
Match Sides0, 4, 8, 12

The peering bit names

Each number is a TileSet.CellNeighbor value. These are the ones a square tile uses, as Godot 4.7.2 prints them:

Farm
Farm: 16x16 top-down pixel art farm: soil autotiles, 20 crops x 6 stages, animals, props and tools. One of the 150 packs in Gridwright.
TileSet.CELL_NEIGHBOR_RIGHT_SIDE          # 0
TileSet.CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER # 3
TileSet.CELL_NEIGHBOR_BOTTOM_SIDE         # 4
TileSet.CELL_NEIGHBOR_BOTTOM_LEFT_CORNER  # 7
TileSet.CELL_NEIGHBOR_LEFT_SIDE           # 8
TileSet.CELL_NEIGHBOR_TOP_LEFT_CORNER     # 11
TileSet.CELL_NEIGHBOR_TOP_SIDE            # 12
TileSet.CELL_NEIGHBOR_TOP_RIGHT_CORNER    # 15

A bit set to a terrain number says "on this side, this tile expects that terrain". For a tile of terrain 0 in the middle of a grass field, all 8 bits are 0. For the top-left corner of a lake, the right and bottom sides are 0, the bottom-right corner is 0, and the rest is left empty.

You set them in code like this:

var data: TileData = source.get_tile_data(Vector2i(1, 0), 0)
data.terrain_set = 0
data.terrain = 0
data.set_terrain_peering_bit(TileSet.CELL_NEIGHBOR_RIGHT_SIDE, 0)
data.set_terrain_peering_bit(TileSet.CELL_NEIGHBOR_BOTTOM_SIDE, 0)
data.set_terrain_peering_bit(TileSet.CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER, 0)

If you set bits in a loop, check each with is_valid_terrain_peering_bit() first, so you only set the ones the mode uses.

What happens when a tile is missing

This is the part that bites. TileMapLayer.set_cells_terrain_connect() takes the cells you give it, works out each cell's neighbours, and looks for the tile whose bits match. We tested two cases.

Only a full tile in the set. We made a TileSet with one tile (all 8 bits set) and asked Godot to connect a 3 x 3 block of cells. Only the middle cell got a tile (get_cell_atlas_coords() returned (1, 0)). The other eight cells came back as source -1, coordinates (-1, -1): empty. There was no error and no warning. The edge needed tiles the set did not have, so Godot placed nothing.

A tile with no bits as well. With a second tile added that has no bits at all, the same call filled every cell, but with that bare tile. Godot picked the closest tile it had, so the map looked complete and wrong.

Both results are a quiet failure. A missing mask shows up as a hole in one case and as the wrong tile in the other, and the editor does not tell you which masks your set lacks. With 47 tiles and 256 masks, you only find the gap by painting a map that happens to need it.

Test it on a real map first

The Autotile Tester does this check before you import anything. It runs in your browser, and your art is never uploaded.

  1. Drop the sheet. A 47-tile blob, a 16-tile corner set or a 16-tile sides set, in any layout, or as loose tiles. It reads each tile's neighbour bits from its pixels, so you do not type any.
  2. Check what it read. It shows the kind of set, every tile's bits, which masks are missing and which tiles are variants. Click any tile to see its bits and change them.
  3. Paint a test map. Paint and erase with the mouse, roll a random map, or put every tile on the map once. The map picks tiles the way Godot's terrains and Tiled's wangsets do.
  4. Read the marks. A mask your set lacks shows as a red cross. Every seam where two tiles' edges step apart is boxed red and named by its two tiles, which is the two-pixels-off edge you would otherwise find in the game.
  5. Export what you need. A Godot 4 TileSet (.tres) with every terrain peering bit already set on your sheet as it is, a Tiled tileset (.tsx) with its wangset, the test map as a Tiled .tmx and a Godot .tscn, a JSON with every tile's bits and the tile for each of the 256 masks, the lookup as GDScript, JavaScript and C#, your tiles re-laid in mask order, and the test map as a PNG.
Your game, looking finished

Every Gridwright asset pack, made to fit together, and all 44 tools for the fiddly work, with every new release, for $17 a month. Try it in your own game first: 3 days for $1 with every tool and up to 10 packs. Cancel any time.

Start: 3 days for $1 See everything it includes

You can try the tool on a sample set, a grass blob, a dirt corner set or a wood fence, with no file of your own: try it here. Everything works on your own files with a free Gridwright account, made with only your email, and your work is saved. Exporting what you make comes with Pro.

Using the tileset in Godot

Once the .tres is in your project, assign it to a TileMapLayer and paint with terrains, or let code do it:

extends TileMapLayer

func paint_lake(cells: Array[Vector2i]) -> void:
	# terrain set 0, terrain 0
	set_cells_terrain_connect(cells, 0, 0)

The signature is set_cells_terrain_connect(cells, terrain_set, terrain, ignore_empty_terrains = true). Pass the whole area you want changed in one call, so Godot sees every cell's neighbours together.

If a call leaves holes, as in the test above, the set lacks a tile for that mask. Open the tester, look for the red crosses, and draw the missing tiles. If the map shows the wrong tile instead, a tile has the wrong bits: click it in the tester, compare its bits to what is drawn, and fix them there.

FAQ

How many tiles does a Godot autotile need?

47 for Match Corners and Sides (out of 256 possible masks), 16 for Match Corners and 16 for Match Sides. Each tile needs its peering bits set for the mode.

Why does set_cells_terrain_connect leave empty cells?

No tile in the TileSet has the bits the cell needs. In our test Godot 4.7.2 left the cell empty (source -1) with no error. Add the missing tile, or check that its bits are set.

Which peering bits are valid for Match Sides?

0, 4, 8 and 12: right, bottom, left and top (CELL_NEIGHBOR_RIGHT_SIDE, BOTTOM_SIDE, LEFT_SIDE, TOP_SIDE). Corners are not used.

Does the Autotile Tester need a sheet in a fixed order?

No. It reads each tile's bits from its pixels, so the layout can be any, and the export puts the tiles in mask order for you.

Test your set before the level

Open the Autotile Tester, drop your sheet, paint every tile once and fix the red marks. Then take the TileSet with its bits set into Godot. For sets that are already tested and ready to paint with, Top-Down Tileset has the autotiles the tool's samples come from. The tool and the pack are part of Gridwright: 44 tools and 150 asset packs.

Your game, looking finished

Every Gridwright asset pack, made to fit together, and all 44 tools for the fiddly work, with every new release, for $17 a month. Try it in your own game first: 3 days for $1 with every tool and up to 10 packs. Cancel any time.

Start: 3 days for $1 See everything it includes