using_tilemaps.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. .. _doc_using_tilemaps:
  2. Using TileMaps
  3. ==============
  4. .. seealso::
  5. This page assumes you have created or downloaded a TileSet already. If not,
  6. please read :ref:`doc_using_tilesets` first as you will need a TileSet
  7. to create a TileMap.
  8. Introduction
  9. ------------
  10. A tilemap is a grid of tiles used to create a game's layout. There are several
  11. benefits to using :ref:`TileMapLayer <class_TileMapLayer>` nodes to design your levels.
  12. First, they make it possible to draw the layout by "painting" the tiles onto a
  13. grid, which is much faster than placing individual :ref:`Sprite2D <class_Sprite2D>`
  14. nodes one by one. Second, they allow for much larger levels because they are
  15. optimized for drawing large numbers of tiles. Finally, you can add collision,
  16. occlusion, and navigation shapes to tiles, adding greater functionality to
  17. the TileMap.
  18. Specifying the TileSet in the TileMapLayer
  19. ------------------------------------------
  20. If you've followed the previous page on :ref:`doc_using_tilesets`, you should
  21. have a TileSet resource that is built-in to the TileMapLayer node. This is good for
  22. prototyping, but in a real world project, you will generally have multiple
  23. levels reusing the same tileset.
  24. The recommended way to reuse the same TileSet in several TileMapLayer nodes is to save
  25. the TileSet to an external resource. To do so, click the dropdown next to the TileSet
  26. resource and choose **Save**:
  27. .. figure:: img/using_tilemaps_save_tileset_to_resource.webp
  28. :align: center
  29. :alt: Saving the built-in TileSet resource to an external resource file
  30. Saving the built-in TileSet resource to an external resource file
  31. Multiple TileMaplayers and settings
  32. -----------------------------------
  33. When working with tilemaps it's generally advised that you use multiple TileMapLayer
  34. nodes when appropriate. Using multiple layers can be advantageous, for example,
  35. this allows you to distinguish foreground tiles from background tiles for better
  36. organization. You can place one tile per layer at a given location, which allows you
  37. to overlap several tiles together if you have more than one layer.
  38. Each TileMapLayer node has several properties you can adjust:
  39. - **Enabled:** If ``true``, the layer is visible in the editor and when running
  40. the project.
  41. - **TileSet** The tileset used by the TileMapLayer node.
  42. Rendering
  43. ^^^^^^^^^
  44. - **Y Sort Origin:** The vertical offset to use for Y-sorting on each tile (in pixels).
  45. Only effective if **Y Sort Enabled** under CanvasItem settings is ``true``.
  46. - **X Draw Order Reversed** Reverses the order tiles are drawn on the X axis. Requires
  47. that **Y Sort Enabled** under CanvasItem settings is ``true``.
  48. - **Rendering Quadrant Size** A quadrant is a group of tiles drawn together on a single
  49. CanvasItem for optimization purposes. This setting defines the length of a square's
  50. side in the map's coordinate system. The quadrant size does not apply to a Y sorted
  51. TileMapLayer since tiles are grouped by Y position in that case.
  52. Physics
  53. ^^^^^^^
  54. - **Collision Enabled** Enables or disables collision.
  55. - **Use Kinematic Bodies** When true TileMapLayer collision shapes will be instantiated
  56. as kinematic bodies.
  57. - **Collision Visibility Mode** Whether or not the TileMapLayer's collision shapes are
  58. visible. If set to default, then it depends on the show collision debug settings.
  59. Navigation
  60. ^^^^^^^^^^
  61. - **Navigation Enabled** Whether or not navigation regions are enabled.
  62. - **Navigation Visible** Whether or not the TileMapLayer's navigation meshes are
  63. visible. If set to default then it depends on the show navigation debug settings.
  64. .. tip::
  65. TileMap built-in navigation has many practical limitations that result in inferior pathfinding performance and pathfollowing quality.
  66. After designing the TileMap consider baking it to a more optimized navigation mesh (and disabling the TileMap NavigationLayer) using a :ref:`NavigationRegion2D <class_NavigationRegion2D>` or the :ref:`NavigationServer2D <class_NavigationServer2D>`. See :ref:`doc_navigation_using_navigationmeshes` for additional information.
  67. .. warning::
  68. 2D navigation meshes can not be "layered" or stacked on top of each other like visuals or physic shapes. Attempting to stack navigation meshes on the same navigation map will result in merge and logical errors that break the pathfinding.
  69. You can reorder layers by drag-and-dropping their node in the Scene tab. You can
  70. also switch between which TileMapLayer node you're working on by using the buttons
  71. in the top right corner of the TileMap editor.
  72. .. note::
  73. You can create, rename or reorder layers in the future without affecting
  74. existing tiles. Be careful though, as *removing* a layer will also remove
  75. all tiles that were placed on the layer.
  76. Opening the TileMap editor
  77. --------------------------
  78. Select the TileMapLayer node, then open the TileMap panel at the bottom
  79. of the editor:
  80. .. figure:: img/using_tilemaps_open_tilemap_editor.webp
  81. :align: center
  82. :alt: Opening the TileMap panel at the bottom of the editor. The TileMap node must be selected first.
  83. Opening the TileMap panel at the bottom of the editor. The TileMap node must be selected first.
  84. Selecting tiles to use for painting
  85. -----------------------------------
  86. First, if you've created additional layers above, make sure you've selected the
  87. layer you wish to paint on:
  88. .. figure:: img/using_tilemaps_select_layer.webp
  89. :align: center
  90. :alt: Selecting a layer to paint on in the TileMap editor
  91. Selecting a layer to paint on in the TileMap editor
  92. .. tip::
  93. In the 2D editor, the layers you aren't currently editing from the same
  94. TileMap node will appear grayed out while in the TileMap editor. You can
  95. disable this behavior by clicking the icon next to the layer selection menu
  96. (**Highlight Selected TileMap Layer** tooltip).
  97. You can skip the above step if you haven't created additional layers, as the
  98. first layer is automatically selected when entering the TileMap editor.
  99. Before you can place tiles in the 2D editor, you must select one or more tiles
  100. in the TileMap panel located at the bottom of the editor. To do so, click a tile
  101. in the TileMap panel, or hold down the mouse button to select multiple tiles:
  102. .. figure:: img/using_tilemaps_select_single_tile_from_tileset.webp
  103. :align: center
  104. :alt: Selecting a tile in the TileMap editor by clicking it
  105. Selecting a tile in the TileMap editor by clicking it
  106. .. tip::
  107. Like in the 2D and TileSet editors, you can pan across the TileMap panel using
  108. the middle or right mouse buttons, and zoom using the mouse wheel or buttons in
  109. the top-left corner.
  110. You can also hold down :kbd:`Shift` to append to the current selection. When
  111. selecting more than one tile, multiple tiles will be placed every time you
  112. perform a painting operation. This can be used to paint structures composed of
  113. multiple tiles in a single click (such as large platforms or trees).
  114. The final selection does not have to be contiguous: if there is empty space
  115. between selected tiles, it will be left empty in the pattern that will be
  116. painted in the 2D editor.
  117. .. figure:: img/using_tilemaps_select_multiple_tiles_from_tileset.webp
  118. :align: center
  119. :alt: Selecting multiple tiles in the TileMap editor by holding down the left mouse button
  120. Selecting multiple tiles in the TileMap editor by holding down the left mouse button
  121. If you've created alternative tiles in your TileSet, you can select them for
  122. painting on the right of the base tiles:
  123. .. figure:: img/using_tilemaps_use_alternative_tile.webp
  124. :align: center
  125. :alt: Selecting an alternative tile in the TileMap editor
  126. Selecting an alternative tile in the TileMap editor
  127. Lastly, if you've created a *scenes collection* in the TileSet, you can place scene tiles in the TileMap:
  128. .. figure:: img/using_tilemaps_placing_scene_tiles.webp
  129. :align: center
  130. :alt: Placing a scene tile containing particles using the TileMap editor
  131. Placing a scene tile containing particles using the TileMap editor
  132. Painting modes and tools
  133. ------------------------
  134. Using the toolbar at the top of the TileMap editor, you can choose between
  135. several painting modes and tools. These modes affect operation when clicking in
  136. the 2D editor, **not** the TileMap panel itself.
  137. From left to right, the painting modes and tools you can choose are:
  138. Selection
  139. ^^^^^^^^^
  140. Select tiles by clicking a single tile, or by holding down the left mouse button to
  141. select multiple with a rectangle in the 2D editor. Note that empty space cannot be
  142. selected: if you create a rectangle selection, only non-empty tiles will be selected.
  143. To append to the current selection, hold :kbd:`Shift` then select a tile.
  144. To remove from the current selection, hold :kbd:`Ctrl` then select a tile.
  145. The selection can then be used in any other painting mode to quickly create copies
  146. of an already-placed pattern.
  147. You can remove the selected tiles from the TileMap by pressing :kbd:`Del`.
  148. You can toggle this mode temporarily while in Paint mode by holding :kbd:`Ctrl`
  149. then performing a selection.
  150. .. tip::
  151. You can copy and paste tiles that were already placed by performing a
  152. selection, pressing :kbd:`Ctrl + C` then pressing :kbd:`Ctrl + V`.
  153. The selection will be pasted after left-clicking. You can press
  154. :kbd:`Ctrl + V` another time to perform more copies this way.
  155. Right-click or press :kbd:`Escape` to cancel pasting.
  156. Paint
  157. ^^^^^
  158. The standard Paint mode allows you to place tiles by clicking or holding
  159. down the left mouse button.
  160. If you right-click, the currently selected tile will be erased from the tilemap.
  161. In other words, it will be replaced by empty space.
  162. If you have selected multiple tiles in the TileMap or using the Selection tool,
  163. they will be placed every time you click or drag the mouse while holding down
  164. the left mouse button.
  165. .. tip::
  166. While in Paint mode, you can draw a line by holding :kbd:`Shift` *before*
  167. holding down the left mouse button, then dragging the mouse to the line's end
  168. point. This is identical to using the Line tool described below.
  169. You can also draw a rectangle by holding :kbd:`Ctrl` and :kbd:`Shift`
  170. *before* holding down the left mouse button, then dragging the mouse to the
  171. rectangle's end point. This is identical to using the Rectangle tool
  172. described below.
  173. Lastly, you can pick existing tiles in the 2D editor by holding :kbd:`Ctrl`
  174. then clicking on a tile (or holding and dragging the mouse).
  175. This will switch the currently painted tile(s) to the tile(s) you've just clicked.
  176. This is identical to using the Picker tool described below.
  177. Line
  178. ^^^^
  179. After selecting Line Paint mode, you can draw in a line that is
  180. always 1 tile thick (no matter its orientation).
  181. If you right-click while in Line Paint mode, you will erase in a line.
  182. If you have selected multiple tiles in the TileMap or using the Selection tool,
  183. you can place them in a repeating pattern across the line.
  184. You can toggle this mode temporarily while in Paint or Eraser mode by holding
  185. :kbd:`Shift` then drawing.
  186. .. figure:: img/using_tilesets_line_tool_multiple_tiles.webp
  187. :align: center
  188. :alt: Using the line tool after selecting two tiles to draw platforms diagonally
  189. Using the line tool after selecting two tiles to draw platforms diagonally
  190. Rectangle
  191. ^^^^^^^^^
  192. After selecting Rectangle Paint mode, you can draw in an axis-aligned
  193. rectangle.
  194. If you right-click while in Rectangle Paint mode, you will erase in
  195. an axis-aligned rectangle.
  196. If you have selected multiple tiles in the TileMap or using the Selection tool,
  197. you can place them in a repeating pattern within the rectangle.
  198. You can toggle this mode temporarily while in Paint or Eraser mode by holding
  199. :kbd:`Ctrl` and :kbd:`Shift` then drawing.
  200. Bucket Fill
  201. ^^^^^^^^^^^
  202. After selecting Bucket Fill mode, you can choose whether painting should be
  203. limited to contiguous areas only by toggling the **Contiguous** checkbox that
  204. appears on the right of the toolbar.
  205. If you enable **Contiguous** (the default), only matching tiles that touch the
  206. current selection will be replaced. This contiguous check is performed
  207. horizontally and vertically, but *not* diagonally.
  208. If you disable **Contiguous**, all tiles with the same ID in the entire TileMap will
  209. be replaced by the currently selected tile. If selecting an empty tile with
  210. **Contiguous** unchecked, all tiles in the rectangle that encompasses the
  211. TileMap's effective area will be replaced instead.
  212. If you right-click while in Bucket Fill mode, you will replace matching tiles
  213. with empty tiles.
  214. If you have selected multiple tiles in the TileMap or using the Selection tool,
  215. you can place them in a repeating pattern within the filled area.
  216. .. figure:: img/using_tilemaps_bucket_fill.webp
  217. :align: center
  218. :alt: Using the Bucket Fill tool
  219. Using the Bucket Fill tool
  220. Picker
  221. ^^^^^^
  222. After selecting Picker mode, you can pick existing tiles in the 2D editor by
  223. holding :kbd:`Ctrl` then clicking on a tile. This will switch the currently
  224. painted tile to the tile you've just clicked. You can also pick multiple tiles
  225. at once by holding down the left mouse button and forming a rectangle selection.
  226. Only non-empty tiles can be picked.
  227. You can toggle this mode temporarily while in Paint mode by holding :kbd:`Ctrl`
  228. then clicking or dragging the mouse.
  229. Eraser
  230. ^^^^^^
  231. This mode is combined with any other painting mode (Paint, Line, Rectangle,
  232. Bucket Fill). When eraser mode is enabled, tiles will be replaced by empty tiles
  233. instead of drawing new lines when left-clicking.
  234. You can toggle this mode temporarily while in any other mode by right-clicking
  235. instead of left-clicking.
  236. Painting randomly using scattering
  237. ----------------------------------
  238. While painting, you can optionally enable *randomization*. When enabled,
  239. a random tile will be chosen between all the currently selected tiles when
  240. painting. This is supported with the Paint, Line, Rectangle and Bucket Fill
  241. tools. For effective paint randomization, you must select multiple tiles
  242. in the TileMap editor or use scattering (both approaches can be combined).
  243. If **Scattering** is set to a value greater than 0, there is a chance that no tile
  244. will be placed when painting. This can be used to add occasional, non-repeating
  245. detail to large areas (such as adding grass or crumbs on a large top-down
  246. TileMap).
  247. Example when using Paint mode:
  248. .. figure:: img/using_tilemaps_scatter_tiles.webp
  249. :align: center
  250. :alt: Selecting from several times to randomly choose, then painting by holding down the left mouse button
  251. Selecting from several times to randomly choose, then painting by holding down the left mouse button
  252. Example when using Bucket Fill mode:
  253. .. figure:: img/using_tilemaps_bucket_fill_scatter.webp
  254. :align: center
  255. :alt: Using Bucket Fill tool with a single tile, but with randomization and scattering enabled
  256. Using Bucket Fill tool with a single tile, but with randomization and scattering enabled
  257. .. note::
  258. Eraser mode does not take randomization and scattering into account.
  259. All tiles within the selection are always removed.
  260. Saving and loading premade tile placements using patterns
  261. ---------------------------------------------------------
  262. While you can copy and paste tiles while in Select mode, you may wish to save
  263. premade *patterns* of tiles to place together in a go. This can be done on a
  264. per-TileMap basis by choosing the **Patterns** tab of the TileMap editor.
  265. To create a new pattern, switch to Select mode, perform a selection and press
  266. :kbd:`Ctrl + C`. Click on empty space within the Patterns tab (a blue focus
  267. rectangle should appear around the empty space), then press :kbd:`Ctrl + V`:
  268. .. figure:: img/using_tilemaps_create_pattern.webp
  269. :align: center
  270. :alt: Creating a new pattern from a selection in the TileMap editor
  271. Creating a new pattern from a selection in the TileMap editor
  272. To use an existing pattern, click its image in the **Patterns** tab, switch to
  273. any painting mode, then left-click somewhere in the 2D editor:
  274. .. figure:: img/using_tilemaps_use_pattern.webp
  275. :align: center
  276. :alt: Placing an existing pattern using the TileMap editor
  277. Placing an existing pattern using the TileMap editor
  278. Like multi-tile selections, patterns will be repeated if used with the Line,
  279. Rectangle or Bucket Fill painting modes.
  280. .. note::
  281. Despite being edited in the TileMap editor, patterns are stored in the
  282. TileSet resource. This allows reusing patterns in different TileMapLayer nodes
  283. after loading a TileSet resource saved to an external file.
  284. Handling tile connections automatically using terrains
  285. ------------------------------------------------------
  286. To use terrains, the TileMap node must feature at least one terrain set and a
  287. terrain within this terrain set. See
  288. :ref:`doc_using_tilesets_creating_terrain_sets` if you haven't created a terrain
  289. set for the TileSet yet.
  290. There are 3 kinds of painting modes available for terrain connections:
  291. - **Connect**, where tiles are connected to surrounding tiles on the same
  292. TileMapLayer.
  293. - **Path**, where tiles are connected to tiles painted in the same stroke (until
  294. the mouse button is released).
  295. - Tile-specific overrides to resolve conflicts or handle situations not covered
  296. by the terrain system.
  297. The Connect mode is easier to use, but Path is more flexible as it allows for
  298. more artist control during painting. For instance, Path can allow roads to be
  299. directly adjacent to each other without being connected to each other, while
  300. Connect will force both roads to be connected.
  301. .. figure:: img/using_tilemaps_terrain_select_connect_mode.webp
  302. :align: center
  303. :alt: Selecting Connect mode in the TileMap editor's Terrains tab
  304. Selecting Connect mode in the TileMap editor's Terrains tab
  305. .. figure:: img/using_tilemaps_terrain_select_path_mode.webp
  306. :align: center
  307. :alt: Selecting Path mode in the TileMap editor's Terrains tab
  308. Selecting Path mode in the TileMap editor's Terrains tab
  309. Lastly, you can select specific tiles from the terrain to resolve conflicts in
  310. certain situations:
  311. .. figure:: img/using_tilemaps_terrain_paint_specific_tiles.webp
  312. :align: center
  313. :alt: Painting with specific tiles in the TileMap editor's Terrains tab
  314. Painting with specific tiles in the TileMap editor's Terrains tab
  315. Any tile that has at least one of its bits set to a value set to the
  316. corresponding terrain ID will appear in the list of tiles to choose from.
  317. Handling missing tiles
  318. ----------------------
  319. If you remove tiles in the TileSet that are referenced in a TileMap, the TileMap
  320. will display a placeholder to indicate that an invalid tile ID is placed:
  321. .. figure:: img/using_tilemaps_missing_tiles.webp
  322. :align: center
  323. :alt: Missing tiles in the TileMap editor due to the TileSet reference being broken
  324. Missing tiles in the TileMap editor due to the TileSet reference being broken
  325. These placeholders are **not** visible in the running project, but the tile data
  326. is still persisted to disk. This allows you to safely close and reopen such
  327. scenes. Once you re-add a tile with the matching ID, the tiles will appear with
  328. the new tile's appearance.
  329. .. note::
  330. Missing tile placeholders may not be visible until you select the TileMapLayer
  331. node and open the TileMap editor.