Introduction to TileMaps
TileMaps are a powerful system for creating grid-based levels efficiently. They allow you to paint levels using tiles from a TileSet.As of Godot 4.0+, the recommended approach is to use
TileMapLayer nodes. The legacy TileMap node is deprecated but still functional.TileMap Basics
Creating a TileMap
- Add a
TileMapnode to your scene - Create or assign a
TileSetresource in the Inspector - Open the TileMap editor at the bottom of the screen
- Select tiles and paint them in the viewport
Setting Up in Code
TileMap Layers
TileMaps support multiple layers for organizing tiles:Coordinate System
TileSet
A TileSet is a resource containing all tile definitions.Creating a TileSet from Texture
- Create a new TileSet resource
- Add an Atlas source
- Select your texture atlas
- Configure tile size
- Define tiles and their properties
TileSet in Code
Working with Tiles
Setting Tiles
Reading Tiles
Getting Used Cells
Autotiling and Terrains
Terrains automatically choose the correct tile based on neighboring tiles.Setting Up Terrain Sets
- In TileSet editor, create a Terrain Set
- Define terrains (grass, dirt, water, etc.)
- Paint terrain bits on each tile
- Godot will automatically pick correct tiles when painting
Using Terrains in Code
Alternative Tiles
Create variations of the same tile:Physics in TileMaps
Setting Up Collision
- Select a tile in the TileSet editor
- Switch to the Physics Layers tab
- Draw collision polygons for the tile
- Configure physics layers and masks
Physics Layer Configuration
Detecting Tile Collisions
Custom Data Layers
Store custom data per tile:Adding Custom Data
- In TileSet editor, add a Custom Data Layer
- Name it (e.g., “damage”, “speed_modifier”)
- Set the type (int, float, string, etc.)
- Set values for each tile
Reading Custom Data
TileMap Patterns
Save and reuse tile patterns:Navigation in TileMaps
TileMaps can generate navigation regions automatically:Setting Up Navigation
- In TileSet editor, add a Navigation Layer
- Draw navigation polygons for walkable tiles
- Enable layer navigation in TileMap
Procedural Generation
Performance Optimization
Rendering Quadrant Size
Clearing Cells Efficiently
Advanced Example: Destructible Terrain
Clicking on Tiles
Best Practices
Use terrain sets for natural transitions
Use terrain sets for natural transitions
Terrain sets automatically handle tile connections, saving time and ensuring consistency.
Organize tiles in layers
Organize tiles in layers
Use separate layers for ground, decorations, and foreground objects.
Use custom data for game logic
Use custom data for game logic
Store tile properties (damage, speed modifiers) in custom data layers instead of hard-coding.
Cache tile lookups in hot paths
Cache tile lookups in hot paths
If checking the same tile repeatedly, cache the result to avoid repeated lookups.
Next Steps
Physics
Learn more about collision and physics
Canvas Layers
Organize rendering with canvas layers