godot-tilemap
它能做什么
该技能讲解如何使用 TileMapLayer 和 TileSet,以瓦片网格在 Godot 中构建 2D 关卡。适合制作平台跳跃、俯视地图或类似关卡的游戏制作者。
适用场景
- ✓当你想用瓦片图像在 Godot 中绘制 2D 关卡时。
- ✓当你需要瓦片具有碰撞、导航或伤害等自定义数值时。
- ✓当你想把旧的 TileMap 节点迁移到较新的 TileMapLayer 设置时。
技能链接
https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/godot/godot-tilemap/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我在 Godot 中绘制一个平台关卡,其中尖刺碰到玩家时会造成伤害。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能讲解如何设置图层和瓦片集,为瓦片添加碰撞和自定义数据,使用地形让边缘自动衔接,并通过代码读取或修改瓦片。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
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
TileMapnode (single node, many layers) to multipleTileMapLayernodes (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
TileMapLayernode (one per visual/logical layer: background, walls, foreground). Each holds exactly one layer of tiles. - Create or assign a
TileSeton the layer'stile_setproperty. Add an atlas source (a texture sliced into tiles) in the TileSet editor. Save the TileSet as an external.tresso 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
damageoris_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_enabledproperties. - Read/write from code with
local_to_map,set_cell,get_cell_source_id, andget_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:
# event.position is in viewport pixels and ignores Camera2D scroll/zoom.
# make_input_local() converts it into this layer's local space.
var local_event := make_input_local(event) as InputEventMouseButton
var cell := local_to_map(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
TileMapnode is deprecated in 4.3. UseTileMapLayernodes (one layer per node); group them under a parentNode2D. OldTileMapcalls that took alayerargument (set_cell(layer, ...)) do not apply toTileMapLayer.local_to_mapneeds layer-local coordinates. A world position needsto_local(...)first. A mouse event'spositionis in viewport pixels, not world space, so it is wrong as soon as aCamera2Dscrolls or zooms; convert it withmake_input_local(event)(or pollget_local_mouse_position()) instead.get_cell_tile_datareturnsnullfor empty cells or non-atlas sources — always null-check beforeget_custom_data.- Custom data is typed. A layer declared as
intreturnsint; reading it as the wrong type or
常见问题
什么是 TileSet?
TileSet 是瓦片图层所使用的瓦片图像及其设置的集合。
什么是自动拼接?
自动拼接会在你绘制时,让不同瓦片类型之间的边缘自动连接。
它涵盖让玩家在瓦片之间移动吗?
不涵盖。玩家移动由另一份 2D 移动技能负责。
详情
作者的原始描述
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.
热度
- GitHub 星标
- 6.6k
- 排名
- #13830
- 许可证
- Apache-2.0
- 更新时间
- 2026/10/28
评论
- …