godot-tilemap
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGodot TileMap (4.3+ TileMapLayer)
Godot TileMap(4.3+ TileMapLayer)
Author tile-based levels with + , add per-tile collision and
custom data, autotile with terrains, and manipulate cells at runtime. Targets
Godot 4.3+, where replaces the now-deprecated node.
TileMapLayerTileSetTileMapLayerTileMap使用 + 制作基于瓦片的关卡,添加逐瓦片碰撞与自定义数据,通过地形实现自动瓦片,并在运行时操作单元格。本内容针对**Godot 4.3+**版本,其中已取代现已弃用的节点。
TileMapLayerTileSetTileMapLayerTileMapWhen to use
适用场景
- Use when designing 2D levels from a tile grid, configuring a (collision, navigation, custom data, terrains), or reading/writing tiles from code.
TileSet - Use when migrating a node (single node, many layers) to multiple
TileMapnodes (one layer each).TileMapLayer
When not to use: moving the player across the tiles → ; general
physics bodies/raycasts → ; procedural map generation algorithms →
; level design practice → .
godot-2d-movementgodot-physicsprocedural-genlevel-design- 适用于从瓦片网格设计2D关卡、配置(碰撞、导航、自定义数据、地形),或通过代码读写瓦片的场景。
TileSet - 适用于将单个多层的节点迁移为多个单层
TileMap节点的场景。TileMapLayer
不适用场景: 让玩家在瓦片上移动 → ;通用物理体/射线检测 → ;程序化地图生成算法 → ;关卡设计实践 → 。
godot-2d-movementgodot-physicsprocedural-genlevel-designCore workflow
核心工作流程
- Add a node (one per visual/logical layer: background, walls, foreground). Each holds exactly one layer of tiles.
TileMapLayer - Create or assign a on the layer's
TileSetproperty. Add an atlas source (a texture sliced into tiles) in the TileSet editor. Save the TileSet as an externaltile_setso multiple layers/levels reuse it..tres - Add tile data in the TileSet editor: physics layers (collision polygons),
navigation layers, occlusion, and custom data layers (typed per-tile values like
or
damage).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_enabledproperties.navigation_enabled - Read/write from code with ,
local_to_map,set_cell, andget_cell_source_id.get_cell_tile_data(...).get_custom_data(...)
- 添加节点(每个视觉/逻辑图层对应一个节点:背景、墙体、前景)。每个节点仅包含一层瓦片。
TileMapLayer - **创建或分配**至图层的
TileSet属性。在TileSet编辑器中添加图集资源(将一张纹理切片为瓦片)。将TileSet保存为外部tile_set文件,以便多个图层/关卡复用。.tres - 在TileSet编辑器中添加瓦片数据:物理图层(碰撞多边形)、导航图层、遮挡,以及自定义数据图层(逐瓦片的类型化值,如或
damage)。is_ladder - 在TileMap底部面板绘制(画笔/直线/矩形/桶填充工具)。对于自动连接的瓦片,定义地形集并使用连接/路径模式绘制。
- 通过图层的/
collision_enabled属性启用逐图层碰撞/导航。navigation_enabled - 通过代码读写,使用、
local_to_map、set_cell和get_cell_source_id方法。get_cell_tile_data(...).get_custom_data(...)
Patterns
实践模式
1. Convert mouse position to a cell and read its custom data
1. 将鼠标位置转换为单元格并读取其自定义数据
gdscript
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])gdscript
extends TileMapLayer
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseButton and event.pressed:
# local_to_map需要本地坐标;先将全局坐标转换为本地坐标。
var cell := local_to_map(to_local(event.position))
var data := get_cell_tile_data(cell) # TileData或null
if data:
var dmg: int = data.get_custom_data("damage") # 自定义数据图层
print("Cell %s deals %d damage" % [cell, dmg])2. Place and erase tiles at runtime
2. 在运行时放置与删除瓦片
gdscript
undefinedgdscript
undefinedset_cell(coords, source_id, atlas_coords, alternative_tile = 0)
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
undefinedfunc place_wall(cell: Vector2i) -> void:
set_cell(cell, 0, Vector2i(2, 1)) # 资源ID为0,图集瓦片位于第2列第1行
func dig(cell: Vector2i) -> void:
erase_cell(cell) # 等同于set_cell(cell, -1)
func clear_level() -> void:
clear() # 删除本图层的所有瓦片
undefined3. Autotiling a region with a terrain set
3. 使用地形集自动瓦片填充区域
gdscript
undefinedgdscript
undefinedPaint a filled area with terrain terrain
of terrain set terrain_set
;
terrainterrain_set使用地形集terrain_set
中的terrain
地形填充指定区域;
terrain_setterrainGodot picks the correct edge/corner tiles to connect them.
Godot会自动选择正确的边缘/拐角瓦片实现连接。
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)
undefinedfunc 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)
undefined4. Iterate placed tiles (e.g. find all spawn tiles)
4. 遍历已放置的瓦片(例如查找所有出生点瓦片)
gdscript
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 spawnsgdscript
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 spawnsPitfalls
常见陷阱
- node is deprecated in 4.3. Use
TileMapnodes (one layer per node); group them under a parentTileMapLayer. OldNode2Dcalls that took aTileMapargument (layer) do not apply toset_cell(layer, ...).TileMapLayer - needs local coordinates. Mouse/global positions must be converted with
local_to_mapfirst, or cells will be offset.to_local(...) - returns
get_cell_tile_datafor empty cells or non-atlas sources — always null-check beforenull.get_custom_data - Custom data is typed. A layer declared as returns
int; reading it as the wrong type or referencing a non-existent layer name errors. Define the layer in the TileSet first.int - Terrains need every combination defined. produces odd results if the TileSet's terrain bitmask peering is incomplete.
set_cells_terrain_connect - Runtime edits are batched to end-of-frame. If you must read updated internals
immediately after , call
set_cell(expensive — avoid in loops).update_internals() - Collision not working? Check the layer's , that the tile has a physics layer with a polygon, and that the TileSet's physics layer mask matches your bodies.
collision_enabled
- 节点在4.3版本中已弃用。 请使用
TileMap节点(每个节点对应一个图层);将它们归到父节点TileMapLayer下。旧Node2D中接受TileMap参数的调用(如layer)不适用于set_cell(layer, ...)。TileMapLayer - 需要本地坐标。 鼠标/全局位置必须先通过
local_to_map转换,否则单元格会出现偏移。to_local(...) - 可能返回
get_cell_tile_data:空单元格或非图集资源会返回null,在调用null前务必进行空值检查。get_custom_data - 自定义数据是类型化的。 声明为的图层会返回整数类型;若读取时使用错误类型或引用不存在的图层名称会报错。请先在TileSet中定义该图层。
int - 地形需要定义所有组合。 如果TileSet的地形位掩码配对不完整,会产生异常结果。
set_cells_terrain_connect - 运行时编辑会批量处理至帧末。 如果必须在调用后立即读取更新后的内部数据,请调用
set_cell(性能开销大,避免在循环中使用)。update_internals() - 碰撞不生效? 检查图层的属性、瓦片是否带有包含多边形的物理图层,以及TileSet的物理图层掩码是否与你的物理体匹配。
collision_enabled
References
参考资料
- For TileSet setup (atlas sources, physics/navigation/custom-data layers, terrain
bitmasks), scene tiles, Y-sorting, and runtime tile-data overrides
(), read
_use_tile_data_runtime_update.references/tileset-and-terrains.md
- 关于TileSet配置(图集资源、物理/导航/自定义数据图层、地形位掩码)、场景瓦片、Y轴排序以及运行时瓦片数据覆盖(),请阅读
_use_tile_data_runtime_update。references/tileset-and-terrains.md
Related skills
相关技能
- — characters that walk on these tiles.
godot-2d-movement - — collision layers/masks the tile collisions participate in.
godot-physics - — generating tilemaps from noise/RNG.
procedural-gen - /
level-design— design practice and grid-based genres.roguelike
- — 在这些瓦片上行走的角色。
godot-2d-movement - — 瓦片碰撞所属的碰撞图层/掩码。
godot-physics - — 通过噪声/随机数生成瓦片地图。
procedural-gen - /
level-design— 关卡设计实践与基于网格的游戏类型。roguelike