using_tilemaps.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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:`TileMap <class_TileMap>` 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. .. note::
  19. Godot 4.0 has moved several per-tile properties, such as tile rotation, from
  20. TileMap to TileSet. Individual tiles can no longer be rotated while in the
  21. TileMap editor. Instead, the TileSet editor must be used to create
  22. alternative rotated tiles.
  23. This change allows for greater design consistency, as not every tile needs to be
  24. rotated or flipped within a TileSet.
  25. Specifying the TileSet in the TileMap
  26. -------------------------------------
  27. If you've followed the previous page on :ref:`doc_using_tilesets`, you should
  28. have a TileSet resource that is built-in to the TileMap node. This is good for
  29. prototyping, but in a real world project, you will generally have multiple
  30. levels reusing the same tileset.
  31. The recommended way to reuse the same TileSet in several TileMap nodes is to save
  32. the TileSet to an external resource. To do so, click the dropdown next to the TileSet
  33. resource and choose **Save**:
  34. .. figure:: img/using_tilemaps_save_tileset_to_resource.webp
  35. :align: center
  36. :alt: Saving the built-in TileSet resource to an external resource file
  37. Saving the built-in TileSet resource to an external resource file
  38. Creating TileMap layers
  39. -----------------------
  40. As of Godot 4.0, you can place several *layers* in a single TileMap node. For
  41. example, this allows you to distinguish foreground tiles from background tiles
  42. for better organization. You can place one tile per layer at a given location,
  43. which allows you to overlap several tiles together if you have more than one layer.
  44. By default, a TileMap node automatically has one premade layer. You do not have
  45. to create additional layers if you only need a single layer, but if you wish to
  46. do so now, select the TileMap node and unfold the **Layers** section in the
  47. inspector:
  48. .. figure:: img/using_tilemaps_create_layers.webp
  49. :align: center
  50. :alt: Creating layers in a TileMap node (example with "background" and "foreground")
  51. Creating layers in a TileMap node (example with "background" and "foreground")
  52. Each layer has several properties you can adjust:
  53. - **Name:** A human-readable name to display in the TileMap editor. This can be
  54. something like "background", "buildings", "vegetation", etc.
  55. - **Enabled:** If ``true``, the layer is visible in the editor and when running
  56. the project.
  57. - **Modulate:** The color to use as a multiplier for all tiles on the layer.
  58. This is also multiplied with the per-tile **Modulate** property and the
  59. TileMap node's **Modulate** property. For example, you can use this to darken
  60. background tiles to make foreground tiles stand out more.
  61. - **Y Sort Enabled:** If ``true``, sorts tiles based on their Y position on the
  62. TileMap. This can be used to prevent sorting issues with certain tile setups,
  63. especially with isometric tiles.
  64. - **Y Sort Origin:** The vertical offset to use for Y-sorting on each tile (in pixels).
  65. Only effective if **Y Sort Enabled** is ``true``.
  66. - **Z Index:** Controls whether this layer is drawn in front of or behind other
  67. TileMap layers. This value can be positive or negative; the layer with the highest Z
  68. Index is drawn on top of other layers. If several layers have an equal Z Index
  69. property, the layer that is *last* in the list of layers (the one which
  70. appears at the bottom in the list) is drawn on top.
  71. You can reorder layers by drag-and-dropping the "three horizontal bars" icon on
  72. the left of the entries in the **Layers** section.
  73. .. note::
  74. You can create, rename or reorder layers in the future without affecting
  75. existing tiles. Be careful though, as *removing* a layer will also remove
  76. all tiles that were placed on the layer.
  77. Opening the TileMap editor
  78. --------------------------
  79. Select the TileMap node, then open the TileMap panel at the bottom
  80. of the editor:
  81. .. figure:: img/using_tilemaps_open_tilemap_editor.webp
  82. :align: center
  83. :alt: Opening the TileMap panel at the bottom of the editor. The TileMap node must be selected first.
  84. Opening the TileMap panel at the bottom of the editor. The TileMap node must be selected first.
  85. Selecting tiles to use for painting
  86. -----------------------------------
  87. First, if you've created additional layers above, make sure you've selected the
  88. layer you wish to paint on:
  89. .. figure:: img/using_tilemaps_select_layer.webp
  90. :align: center
  91. :alt: Selecting a layer to paint on in the TileMap editor
  92. Selecting a layer to paint on in the TileMap editor
  93. .. tip::
  94. In the 2D editor, the layers you aren't currently editing from the same
  95. TileMap node will appear grayed out while in the TileMap editor. You can
  96. disable this behavior by clicking the icon next to the layer selection menu
  97. (**Highlight Selected TileMap Layer** tooltip).
  98. You can skip the above step if you haven't created additional layers, as the
  99. first layer is automatically selected when entering the TileMap editor.
  100. Before you can place tiles in the 2D editor, you must select one or more tiles
  101. in the TileMap panel located at the bottom of the editor. To do so, click a tile
  102. in the TileMap panel, or hold down the mouse button to select multiple tiles:
  103. .. figure:: img/using_tilemaps_select_single_tile_from_tileset.webp
  104. :align: center
  105. :alt: Selecting a tile in the TileMap editor by clicking it
  106. Selecting a tile in the TileMap editor by clicking it
  107. .. tip::
  108. Like in the 2D and TileSet editors, you can pan across the TileMap panel using
  109. the middle or right mouse buttons, and zoom using the mouse wheel or buttons in
  110. the top-left corner.
  111. You can also hold down :kbd:`Shift` to append to the current selection. When
  112. selecting more than one tile, multiple tiles will be placed every time you
  113. perform a painting operation. This can be used to paint structures composed of
  114. multiple tiles in a single click (such as large platforms or trees).
  115. The final selection does not have to be contiguous: if there is empty space
  116. between selected tiles, it will be left empty in the pattern that will be
  117. painted in the 2D editor.
  118. .. figure:: img/using_tilemaps_select_multiple_tiles_from_tileset.webp
  119. :align: center
  120. :alt: Selecting multiple tiles in the TileMap editor by holding down the left mouse button
  121. Selecting multiple tiles in the TileMap editor by holding down the left mouse button
  122. If you've created alternative tiles in your TileSet, you can select them for
  123. painting on the right of the base tiles:
  124. .. figure:: img/using_tilemaps_use_alternative_tile.webp
  125. :align: center
  126. :alt: Selecting an alternative tile in the TileMap editor
  127. Selecting an alternative tile in the TileMap editor
  128. Lastly, if you've created a *scenes collection* in the TileSet, you can place scene tiles in the TileMap:
  129. .. figure:: img/using_tilemaps_placing_scene_tiles.webp
  130. :align: center
  131. :alt: Placing a scene tile containing particles using the TileMap editor
  132. Placing a scene tile containing particles using the TileMap editor
  133. Painting modes and tools
  134. ------------------------
  135. Using the toolbar at the top of the TileMap editor, you can choose between
  136. several painting modes and tools. These modes affect operation when clicking in
  137. the 2D editor, **not** the TileMap panel itself.
  138. From left to right, the painting modes and tools you can choose are:
  139. Selection
  140. ^^^^^^^^^
  141. Select tiles by clicking a single tile, or by holding down the left mouse button to
  142. select multiple with a rectangle in the 2D editor. Note that empty space cannot be
  143. selected: if you create a rectangle selection, only non-empty tiles will be selected.
  144. To append to the current selection, hold :kbd:`Shift` then select a tile.
  145. To remove from the current selection, hold :kbd:`Ctrl` then select a tile.
  146. The selection can then be used in any other painting mode to quickly create copies
  147. of an already-placed pattern.
  148. While in Selection mode, you can't place new tiles, but you can still erase
  149. tiles by right-clicking after making a selection. The whole selection will be erased,
  150. regardless of where you click in the selection.
  151. You can toggle this mode temporarily while in Paint mode by holding :kbd:`Ctrl`
  152. then performing a selection.
  153. .. tip::
  154. You can copy and paste tiles that were already placed by performing a
  155. selection, pressing :kbd:`Ctrl + C` then pressing :kbd:`Ctrl + V`.
  156. The selection will be pasted after left-clicking. You can press
  157. :kbd:`Ctrl + V` another time to perform more copies this way.
  158. Right-click or press :kbd:`Escape` to cancel pasting.
  159. Paint
  160. ^^^^^
  161. The standard Paint mode allows you to place tiles by clicking or holding
  162. down the left mouse button.
  163. If you right-click, the currently selected tile will be erased from the tilemap.
  164. In other words, it will be replaced by empty space.
  165. If you have selected multiple tiles in the TileMap or using the Selection tool,
  166. they will be placed every time you click or drag the mouse while holding down
  167. the left mouse button.
  168. .. tip::
  169. While in Paint mode, you can draw a line by holding :kbd:`Shift` *before*
  170. holding down the left mouse button, then dragging the mouse to the line's end
  171. point. This is identical to using the Line tool described below.
  172. You can also draw a rectangle by holding :kbd:`Ctrl` and :kbd:`Shift`
  173. *before* holding down the left mouse button, then dragging the mouse to the
  174. rectangle's end point. This is identical to using the Rectangle tool
  175. described below.
  176. Lastly, you can pick existing tiles in the 2D editor by holding :kbd:`Ctrl`
  177. then clicking on a tile (or holding and dragging the mouse).
  178. This will switch the currently painted tile(s) to the tile(s) you've just clicked.
  179. This is identical to using the Picker tool described below.
  180. Line
  181. ^^^^
  182. After selecting Line Paint mode, you can draw in a line that is
  183. always 1 tile thick (no matter its orientation).
  184. If you right-click while in Line Paint mode, you will erase in a line.
  185. If you have selected multiple tiles in the TileMap or using the Selection tool,
  186. you can place them in a repeating pattern across the line.
  187. You can toggle this mode temporarily while in Paint or Eraser mode by holding
  188. :kbd:`Shift` then drawing.
  189. .. figure:: img/using_tilemaps_bucket_fill.webp
  190. :align: center
  191. :alt: Using the line tool after selecting two tiles to draw platforms diagonally
  192. Using the line tool after selecting two tiles to draw platforms diagonally
  193. Rectangle
  194. ^^^^^^^^^
  195. After selecting Rectangle Paint mode, you can draw in an axis-aligned
  196. rectangle.
  197. If you right-click while in Rectangle Paint mode, you will erase in
  198. an axis-aligned rectangle.
  199. If you have selected multiple tiles in the TileMap or using the Selection tool,
  200. you can place them in a repeating pattern within the rectangle.
  201. You can toggle this mode temporarily while in Paint or Eraser mode by holding
  202. :kbd:`Ctrl` and :kbd:`Shift` then drawing.
  203. Bucket Fill
  204. ^^^^^^^^^^^
  205. After selecting Bucket Fill mode, you can choose whether painting should be
  206. limited to contiguous areas only by toggling the **Contiguous** checkbox that
  207. appears on the right of the toolbar.
  208. If you enable **Contiguous** (the default), only matching tiles that touch the
  209. current selection will be replaced. This contiguous check is performed
  210. horizontally and vertically, but *not* diagonally.
  211. If you disable **Contiguous**, all tiles with the same ID in the entire TileMap will
  212. be replaced by the currently selected tile. If selecting an empty tile with
  213. **Contiguous** unchecked, all tiles in the rectangle that encompasses the
  214. TileMap's effective area will be replaced instead.
  215. If you right-click while in Bucket Fill mode, you will replace matching tiles
  216. with empty tiles.
  217. If you have selected multiple tiles in the TileMap or using the Selection tool,
  218. you can place them in a repeating pattern within the filled area.
  219. .. figure:: img/using_tilemaps_bucket_fill.webp
  220. :align: center
  221. :alt: Using the Bucket Fill tool
  222. Using the Bucket Fill tool
  223. Picker
  224. ^^^^^^
  225. After selecting Picker mode, you can pick existing tiles in the 2D editor by
  226. holding :kbd:`Ctrl` then clicking on a tile. This will switch the currently
  227. painted tile to the tile you've just clicked. You can also pick multiple tiles
  228. at once by holding down the left mouse button and forming a rectangle selection.
  229. Only non-empty tiles can be picked.
  230. You can toggle this mode temporarily while in Paint mode by holding :kbd:`Ctrl`
  231. then clicking or dragging the mouse.
  232. Eraser
  233. ^^^^^^
  234. This mode is combined with any other painting mode (Paint, Line, Rectangle,
  235. Bucket Fill). When eraser mode is enabled, tiles will be replaced by empty tiles
  236. instead of drawing new lines when left-clicking.
  237. You can toggle this mode temporarily while in any other mode by right-clicking
  238. instead of left-clicking.
  239. Painting randomly using scattering
  240. ----------------------------------
  241. While painting, you can optionally enable *randomization*. When enabled,
  242. a random tile will be chosen between all the currently selected tiles when
  243. painting. This is supported with the Paint, Line, Rectangle and Bucket Fill
  244. tools. For effective paint randomization, you must select multiple tiles
  245. in the TileMap editor or use scattering (both approaches can be combined).
  246. If **Scattering** is set to a value greater than 0, there is a chance that no tile
  247. will be placed when painting. This can be used to add occasional, non-repeating
  248. detail to large areas (such as adding grass or crumbs on a large top-down
  249. TileMap).
  250. Example when using Paint mode:
  251. .. figure:: img/using_tilemaps_scatter_tiles.webp
  252. :align: center
  253. :alt: Selecting from several times to randomly choose, then painting by holding down the left mouse button
  254. Selecting from several times to randomly choose, then painting by holding down the left mouse button
  255. Example when using Bucket Fill mode:
  256. .. figure:: img/using_tilemaps_bucket_fill_scatter.webp
  257. :align: center
  258. :alt: Using Bucket Fill tool with a single tile, but with randomization and scattering enabled
  259. Using Bucket Fill tool with a single tile, but with randomization and scattering enabled
  260. .. note::
  261. Eraser mode does not take randomization and scattering into account.
  262. All tiles within the selection are always removed.
  263. Saving and loading premade tile placements using patterns
  264. ---------------------------------------------------------
  265. While you can copy and paste tiles while in Select mode, you may wish to save
  266. premade *patterns* of tiles to place together in a go. This can be done on a
  267. per-TileMap basis by choosing the **Patterns** tab of the TileMap editor.
  268. To create a new pattern, switch to Select mode, perform a selection and press
  269. :kbd:`Ctrl + C`. Click on empty space within the Patterns tab (a blue focus
  270. rectangle should appear around the empty space), then press :kbd:`Ctrl + V`:
  271. .. figure:: img/using_tilemaps_create_pattern.webp
  272. :align: center
  273. :alt: Creating a new pattern from a selection in the TileMap editor
  274. Creating a new pattern from a selection in the TileMap editor
  275. To use an existing pattern, click its image in the **Patterns** tab, switch to
  276. any painting mode, then left-click somewhere in the 2D editor:
  277. .. figure:: img/using_tilemaps_use_pattern.webp
  278. :align: center
  279. :alt: Placing an existing pattern using the TileMap editor
  280. Placing an existing pattern using the TileMap editor
  281. Like multi-tile selections, patterns will be repeated if used with the Line,
  282. Rectangle or Bucket Fill painting modes.
  283. .. note::
  284. Despite being edited in the TileMap editor, patterns are stored in the
  285. TileSet resource. This allows reusing patterns in different TileMap nodes
  286. after loading a TileSet resource saved to an external file.
  287. Handling tile connections automatically using terrains
  288. ------------------------------------------------------
  289. To use terrains, the TileMap node must feature at least one terrain set and a
  290. terrain within this terrain set. See
  291. :ref:`doc_using_tilesets_creating_terrain_sets` if you haven't created a terrain
  292. set for the TileSet yet.
  293. There are 3 kinds of painting modes available for terrain connections:
  294. - **Connect**, where tiles are connected to surrounding tiles on the same
  295. TileMap layer.
  296. - **Path**, where tiles are connected to tiles painted in the same stroke (until
  297. the mouse button is released).
  298. - Tile-specific overrides to resolve conflicts or handle situations not covered
  299. by the terrain system.
  300. The Connect mode is easier to use, but Path is more flexible as it allows for
  301. more artist control during painting. For instance, Path can allow roads to be
  302. directly adjacent to each other without being connected to each other, while
  303. Connect will force both roads to be connected.
  304. .. figure:: img/using_tilemaps_terrain_select_connect_mode.webp
  305. :align: center
  306. :alt: Selecting Connect mode in the TileMap editor's Terrains tab
  307. Selecting Connect mode in the TileMap editor's Terrains tab
  308. .. figure:: img/using_tilemaps_terrain_select_path_mode.webp
  309. :align: center
  310. :alt: Selecting Path mode in the TileMap editor's Terrains tab
  311. Selecting Path mode in the TileMap editor's Terrains tab
  312. Lastly, you can select specific tiles from the terrain to resolve conflicts in
  313. certain situations:
  314. .. figure:: img/using_tilemaps_terrain_paint_specific_tiles.webp
  315. :align: center
  316. :alt: Painting with specific tiles in the TileMap editor's Terrains tab
  317. Painting with specific tiles in the TileMap editor's Terrains tab
  318. Any tile that has at least one of its bits set to a value set to the
  319. corresponding terrain ID will appear in the list of tiles to choose from.
  320. Handling missing tiles
  321. ----------------------
  322. If you remove tiles in the TileSet that are referenced in a TileMap, the TileMap
  323. will display a placeholder to indicate that an invalid tile ID is placed:
  324. .. figure:: img/using_tilemaps_missing_tiles.webp
  325. :align: center
  326. :alt: Missing tiles in the TileMap editor due to the TileSet reference being broken
  327. Missing tiles in the TileMap editor due to the TileSet reference being broken
  328. These placeholders are **not** visible in the running project, but the tile data
  329. is still persisted to disk. This allows you to safely close and reopen such
  330. scenes. Once you re-add a tile with the matching ID, the tiles will appear with
  331. the new tile's appearance.
  332. .. note::
  333. Missing tile placeholders may not be visible until you select the TileMap
  334. node and open the TileMap editor.