Godot 3 Tutorial– Tiles and TileMaps

This video covers first how to create a TileSet, then how to use the TileSet to create maps using the Godot 3 TileMap object.  We also look at the code required to select an individual tile in a tilemap as well as how to dynamically add new tiles to your map.  The tutorial also briefly looks at how to set collisions up for use with the Godot physics engine.

The Video

Assets and Code Samples

The tiles used in this tutorial are available for download from here.  We used the file tileset.png from the Tiny RPG collection.  Feel free to substitute any tile map you might have, but be sure to update the tile size in both the tileset and tile map cell properties.

The following example code was attached to the root Node instead of to the tile map directly like it was in the video.

TileMap.gd

extends Node

func _process(delta):
  if Input.is_mouse_button_pressed(BUTTON_LEFT):
    # When the user clicks the mouse, conver the mouse coordinate to map coordinates
    # then get the cell at that location.  If there is a tile there, simply print
    # the name of the tile to the console, if there is no tile there, draw the first 
    # tile from the tile set to this location
    var mousePos = get_viewport().get_mouse_position()
    var loc = $TileMap.world_to_map(mousePos)
    var cell = $TileMap.get_cell(loc.x, loc.y)
    if(cell != -1):
      print($TileMap.tile_set.tile_get_name(cell))
    else:
      $TileMap.set_cell(loc.x,loc.y,1)

Back to Tutorial Series Homepage

Scroll to Top