TileMaps & Procedural Generation in Godot 4
πΊοΈ TileMapLayer, Terrain Autotiling & Procedural Generation
In Godot 4.2+, the monolithic TileMap node was refactored into modular TileMapLayer nodes, providing superior multi-layer depth sorting, y-sorting, and physics navigation polygon baking.
π§± 1. Procedural Dungeon Generation with TileMapLayer
# DungeonGenerator.gd
extends Node2D
@onready var ground_layer: TileMapLayer = $GroundLayer
@onready var wall_layer: TileMapLayer = $WallLayer
const TILE_SET_SOURCE_ID: int = 0
const ATLAS_FLOOR: Vector2i = Vector2i(1, 0)
const ATLAS_WALL: Vector2i = Vector2i(0, 0)
func generate_simple_room(room_rect: Rect2i) -> void:
for x in range(room_rect.position.x, room_rect.end.x):
for y in range(room_rect.position.y, room_rect.end.y):
var cell_pos := Vector2i(x, y)
# Floor tile
ground_layer.set_cell(cell_pos, TILE_SET_SOURCE_ID, ATLAS_FLOOR)
# Place surrounding perimeter walls
if x == room_rect.position.x or x == room_rect.end.x - 1 or \
y == room_rect.position.y or y == room_rect.end.y - 1:
wall_layer.set_cell(cell_pos, TILE_SET_SOURCE_ID, ATLAS_WALL)
π Related Notes
- Godot Basics & Architecture β SceneTree and Node2D systems.
- Pixel Art, Sprites & Paletted Workflow β Authoring tile sheets in Aseprite.
- Intro Into Navigation In Godot β Generating 2D navigation polygons from TileMaps.