Godot TileMap (4.7 TileMapLayer)
Author tile-based levels with TileMapLayer + TileSet, add per-tile collision and
custom data, autotile with terrains, and manipulate cells at runtime. Targets
Godot 4.7, where TileMapLayer replaces the now-deprecated TileMap node.
When to use
- Use when designing 2D levels from a tile grid, configuring a
TileSet (collision,
navigation, custom data, terrains), or reading/writing tiles from code.
- Use when migrating a
TileMap node (single node, many layers) to multiple
TileMapLayer nodes (one layer each).
When not to use: moving the player across the tiles → godot-2d-movement; general
physics bodies/raycasts → godot-physics; procedural map generation algorithms →
procedural-gen; level design practice → level-design.
Core workflow
- Add a
TileMapLayer node (one per visual/logical layer: background, walls,
foreground). Each holds exactly one layer of tiles.
- Create or assign a
TileSet on the layer's tile_set property. Add an atlas
source (a texture sliced into tiles) in the TileSet editor. Save the TileSet as an
external .tres so multiple layers/levels reuse it.
- Add tile data in the TileSet editor: physics layers (collision polygons),
navigation layers, occlusion, and custom data layers (typed per-tile values like
damage or is_ladder).
- Paint in the TileMap bottom panel (Paint/Line/Rectangle/Bucket). For self-
connecting tiles, define a terrain set and paint with Connect/Path mode.
- Enable per-layer collision/navigation via the layer's
collision_enabled /
navigation_enabled properties.
- Read/write from code with
local_to_map, set_cell, get_cell_source_id, and
get_cell_tile_data(...).get_custom_data(...).
Patterns
1. Convert mouse position to a cell and read its custom data
extends TileMapLayer
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseButton and event.pressed:
# local_to_map expects local coords; convert from global first.
var cell := local_to_map(to_local(event.position))
var data := get_cell_tile_data(cell) # TileData or null
if data:
var dmg: int = data.get_custom_data("damage") # custom data layer
print("Cell %s deals %d damage" % [cell, dmg])
2. Place and erase tiles at runtime
# set_cell(coords, source_id, atlas_coords, alternative_tile = 0)
func place_wall(cell: Vector2i) -> void:
set_cell(cell, 0, Vector2i(2, 1)) # source 0, atlas tile at column 2, row 1
func dig(cell: Vector2i) -> void:
erase_cell(cell) # same as set_cell(cell, -1)
func clear_level() -> void:
clear() # remove every tile on this layer
3. Autotiling a region with a terrain set
# Paint a filled area with terrain `terrain` of terrain set `terrain_set`;
# Godot picks the correct edge/corner tiles to connect them.
func fill_with_grass(cells: Array[Vector2i]) -> void:
var terrain_set := 0
var grass_terrain := 0
set_cells_terrain_connect(cells, terrain_set, grass_terrain, true)
4. Iterate placed tiles (e.g. find all spawn tiles)
func find_spawns() -> Array[Vector2i]:
var spawns: Array[Vector2i] = []
for cell in get_used_cells():
var data := get_cell_tile_data(cell)
if data and data.get_custom_data("is_spawn"):
spawns.append(cell)
return spawns
Pitfalls
TileMap node is deprecated in 4.3. Use TileMapLayer nodes (one layer per node);
group them under a parent Node2D. Old TileMap calls that took a layer argument
(set_cell(layer, ...)) do not apply to TileMapLayer.
local_to_map needs local coordinates. Mouse/global positions must be converted
with to_local(...) first, or cells will be offset.
get_cell_tile_data returns null for empty cells or non-atlas sources — always
null-check before get_custom_data.
- Custom data is typed. A layer declared as
int returns int; reading it as the
wrong type or referencing a non-existent layer name errors. Define the layer in the
TileSet first.
- Terrains need every combination defined.
set_cells_terrain_connect produces odd
results if the TileSet's terrain bitmask peering is incomplete.
- Runtime edits are batched to end-of-frame. If you must read updated internals
immediately after
set_cell, call update_internals() (expensive — avoid in loops).
- Collision not working? Check the layer's
collision_enabled, that the tile has a
physics layer with a polygon, and that the TileSet's physics layer mask matches your
bodies.
References
- For TileSet setup (atlas sources, physics/navigation/custom-data layers, terrain
bitmasks), scene tiles, Y-sorting, and runtime tile-data overrides
(
_use_tile_data_runtime_update), read references/tileset-and-terrains.md.
Related skills
godot-2d-movement — characters that walk on these tiles.
godot-physics — collision layers/masks the tile collisions participate in.
procedural-gen — generating tilemaps from noise/RNG.
level-design / roguelike — design practice and grid-based genres.
1---2name: godot-tilemap3description: Build and edit tile-based 2D levels in Godot 4.7 with TileMapLayer and TileSet: paint layers, set up collision/navigation/custom-data on tiles, autotile with terrain sets, and read/write cells from code (set_cell, get_cell_tile_data, local_to_map). Use when working with TileMapLayer nodes, .tres TileSets, autotiling, or migrating a deprecated TileMap node to TileMapLayer.4---5
6# Godot TileMap (4.7 TileMapLayer)
7
8Author tile-based levels with `TileMapLayer` + `TileSet`, add per-tile collision and
9custom data, autotile with terrains, and manipulate cells at runtime. Targets
10**Godot 4.7**, where `TileMapLayer` replaces the now-deprecated `TileMap` node.
11
12## When to use
13
14- Use when designing 2D levels from a tile grid, configuring a `TileSet` (collision,
15 navigation, custom data, terrains), or reading/writing tiles from code.
16- Use when migrating a `TileMap` node (single node, many layers) to multiple
17 `TileMapLayer` nodes (one layer each).
18
19**When *not* to use:** moving the player across the tiles → `godot-2d-movement`; general
20physics bodies/raycasts → `godot-physics`; procedural map *generation* algorithms →
21`procedural-gen`; level *design* practice → `level-design`.
22
23## Core workflow
24
251. **Add a `TileMapLayer` node** (one per visual/logical layer: background, walls,
26 foreground). Each holds exactly one layer of tiles.
272. **Create or assign a `TileSet`** on the layer's `tile_set` property. Add an atlas
28 source (a texture sliced into tiles) in the TileSet editor. Save the TileSet as an
29 external `.tres` so multiple layers/levels reuse it.
303. **Add tile data in the TileSet editor:** physics layers (collision polygons),
31 navigation layers, occlusion, and **custom data layers** (typed per-tile values like
32 `damage` or `is_ladder`).
334. **Paint** in the TileMap bottom panel (Paint/Line/Rectangle/Bucket). For self-
34 connecting tiles, define a **terrain set** and paint with Connect/Path mode.
355. **Enable per-layer collision/navigation** via the layer's `collision_enabled` /
36 `navigation_enabled` properties.
376. **Read/write from code** with `local_to_map`, `set_cell`, `get_cell_source_id`, and
38 `get_cell_tile_data(...).get_custom_data(...)`.
39
40## Patterns
41
42### 1. Convert mouse position to a cell and read its custom data
43
44```gdscript
45extends TileMapLayer
46
47func _unhandled_input(event: InputEvent) -> void:
48 if event is InputEventMouseButton and event.pressed:
49 # local_to_map expects local coords; convert from global first.
50 var cell := local_to_map(to_local(event.position))
51 var data := get_cell_tile_data(cell) # TileData or null
52 if data:
53 var dmg: int = data.get_custom_data("damage") # custom data layer
54 print("Cell %s deals %d damage" % [cell, dmg])
55```
56
57### 2. Place and erase tiles at runtime
58
59```gdscript
60# set_cell(coords, source_id, atlas_coords, alternative_tile = 0)
61func place_wall(cell: Vector2i) -> void:
62 set_cell(cell, 0, Vector2i(2, 1)) # source 0, atlas tile at column 2, row 1
63
64func dig(cell: Vector2i) -> void:
65 erase_cell(cell) # same as set_cell(cell, -1)
66
67func clear_level() -> void:
68 clear() # remove every tile on this layer
69```
70
71### 3. Autotiling a region with a terrain set
72
73```gdscript
74# Paint a filled area with terrain `terrain` of terrain set `terrain_set`;
75# Godot picks the correct edge/corner tiles to connect them.
76func fill_with_grass(cells: Array[Vector2i]) -> void:
77 var terrain_set := 0
78 var grass_terrain := 0
79 set_cells_terrain_connect(cells, terrain_set, grass_terrain, true)
80```
81
82### 4. Iterate placed tiles (e.g. find all spawn tiles)
83
84```gdscript
85func find_spawns() -> Array[Vector2i]:
86 var spawns: Array[Vector2i] = []
87 for cell in get_used_cells():
88 var data := get_cell_tile_data(cell)
89 if data and data.get_custom_data("is_spawn"):
90 spawns.append(cell)
91 return spawns
92```
93
94## Pitfalls
95
96- **`TileMap` node is deprecated in 4.3.** Use `TileMapLayer` nodes (one layer per node);
97 group them under a parent `Node2D`. Old `TileMap` calls that took a `layer` argument
98 (`set_cell(layer, ...)`) do not apply to `TileMapLayer`.
99- **`local_to_map` needs local coordinates.** Mouse/global positions must be converted
100 with `to_local(...)` first, or cells will be offset.
101- **`get_cell_tile_data` returns `null`** for empty cells or non-atlas sources — always
102 null-check before `get_custom_data`.
103- **Custom data is typed.** A layer declared as `int` returns `int`; reading it as the
104 wrong type or referencing a non-existent layer name errors. Define the layer in the
105 TileSet first.
106- **Terrains need every combination defined.** `set_cells_terrain_connect` produces odd
107 results if the TileSet's terrain bitmask peering is incomplete.
108- **Runtime edits are batched** to end-of-frame. If you must read updated internals
109 immediately after `set_cell`, call `update_internals()` (expensive — avoid in loops).
110- **Collision not working?** Check the layer's `collision_enabled`, that the tile has a
111 physics layer with a polygon, and that the TileSet's physics layer mask matches your
112 bodies.
113
114## References
115
116- For TileSet setup (atlas sources, physics/navigation/custom-data layers, terrain
117 bitmasks), scene tiles, Y-sorting, and runtime tile-data overrides
118 (`_use_tile_data_runtime_update`), read `references/tileset-and-terrains.md`.
119
120## Related skills
121
122- `godot-2d-movement` — characters that walk on these tiles.
123- `godot-physics` — collision layers/masks the tile collisions participate in.
124- `procedural-gen` — generating tilemaps from noise/RNG.
125- `level-design` / `roguelike` — design practice and grid-based genres.