TileMap

Inherits: Node2D < CanvasItem < Node < Object

Node for 2D tile-based maps.

Description

Node for 2D tile-based maps. Tilemaps use a TileSet which contain a list of tiles (textures plus optional collision, navigation, and/or occluder shapes) which are used to create grid-based maps.

Tutorials

Properties

boolcell_clip_uvfalse
Transform2Dcell_custom_transformTransform2D( 64, 0, 0, 64, 0, 0 )
HalfOffsetcell_half_offset2
intcell_quadrant_size16
Vector2cell_sizeVector2( 64, 64 )
TileOrigincell_tile_origin0
boolcell_y_sortfalse
boolcentered_texturesfalse
floatcollision_bounce0.0
floatcollision_friction1.0
intcollision_layer1
intcollision_mask1
boolcollision_use_kinematicfalse
boolcollision_use_parentfalse
boolcompatibility_modefalse
Modemode0
intoccluder_light_mask1
TileSettile_set 

Methods

voidclear ( )
voidfix_invalid_tiles ( )
intget_cell ( int x, int y ) const
Vector2get_cell_autotile_coord ( int x, int y ) const
intget_cellv ( Vector2 position ) const
boolget_collision_layer_bit ( int bit ) const
boolget_collision_mask_bit ( int bit ) const
Arrayget_used_cells ( ) const
Arrayget_used_cells_by_id ( int id ) const
Rect2get_used_rect ( )
boolis_cell_transposed ( int x, int y ) const
boolis_cell_x_flipped ( int x, int y ) const
boolis_cell_y_flipped ( int x, int y ) const
Vector2map_to_world ( Vector2 map_position, bool ignore_half_ofs=false ) const
voidset_cell ( int x, int y, int tile, bool flip_x=false, bool flip_y=false, bool transpose=false, Vector2 autotile_coord=Vector2( 0, 0 ) )
voidset_cellv ( Vector2 position, int tile, bool flip_x=false, bool flip_y=false, bool transpose=false )
voidset_collision_layer_bit ( int bit, bool value )
voidset_collision_mask_bit ( int bit, bool value )
voidupdate_bitmask_area ( Vector2 position )
voidupdate_bitmask_region ( Vector2 start=Vector2( 0, 0 ), Vector2 end=Vector2( 0, 0 ) )
voidupdate_dirty_quadrants ( )
Vector2world_to_map ( Vector2 world_position ) const

Signals

  • settings_changed ( )

Emitted when a tilemap setting has changed.

Enumerations

enum Mode:

  • MODE_SQUARE = 0 —- Orthogonal orientation mode.
  • MODE_ISOMETRIC = 1 —- Isometric orientation mode.
  • MODE_CUSTOM = 2 —- Custom orientation mode.

enum HalfOffset:

  • HALF_OFFSET_X = 0 —- Half offset on the X coordinate.
  • HALF_OFFSET_Y = 1 —- Half offset on the Y coordinate.
  • HALF_OFFSET_DISABLED = 2 —- Half offset disabled.
  • HALF_OFFSET_NEGATIVE_X = 3 —- Half offset on the X coordinate (negative).
  • HALF_OFFSET_NEGATIVE_Y = 4 —- Half offset on the Y coordinate (negative).

enum TileOrigin:

  • TILE_ORIGIN_TOP_LEFT = 0 —- Tile origin at its top-left corner.
  • TILE_ORIGIN_CENTER = 1 —- Tile origin at its center.
  • TILE_ORIGIN_BOTTOM_LEFT = 2 —- Tile origin at its bottom-left corner.

Constants

  • INVALID_CELL = -1 —- Returned when a cell doesn’t exist.

Property Descriptions

Defaultfalse
Setterset_clip_uv(value)
Getterget_clip_uv()

If true, the cell’s UVs will be clipped.


DefaultTransform2D( 64, 0, 0, 64, 0, 0 )
Setterset_custom_transform(value)
Getterget_custom_transform()

The custom Transform2D to be applied to the TileMap’s cells.


Default2
Setterset_half_offset(value)
Getterget_half_offset()

Amount to offset alternating tiles. See HalfOffset for possible values.


  • int cell_quadrant_size
Default16
Setterset_quadrant_size(value)
Getterget_quadrant_size()

The TileMap’s quadrant size. Optimizes drawing by batching, using chunks of this size.


DefaultVector2( 64, 64 )
Setterset_cell_size(value)
Getterget_cell_size()

The TileMap’s cell size.


Default0
Setterset_tile_origin(value)
Getterget_tile_origin()

Position for tile origin. See TileOrigin for possible values.


Defaultfalse
Setterset_y_sort_mode(value)
Getteris_y_sort_mode_enabled()

If true, the TileMap’s direct children will be drawn in order of their Y coordinate.


  • bool centered_textures
Defaultfalse
Setterset_centered_textures(value)
Getteris_centered_textures_enabled()

If true, the textures will be centered in the middle of each tile. This is useful for certain isometric or top-down modes when textures are made larger or smaller than the tiles (e.g. to avoid flickering on tile edges). The offset is still applied, but from the center of the tile. If used, compatibility_mode is ignored.

If false, the texture position start in the top-left corner unless compatibility_mode is enabled.


Default0.0
Setterset_collision_bounce(value)
Getterget_collision_bounce()

Bounce value for static body collisions (see collision_use_kinematic).


  • float collision_friction
Default1.0
Setterset_collision_friction(value)
Getterget_collision_friction()

Friction value for static body collisions (see collision_use_kinematic).


  • int collision_layer
Default1
Setterset_collision_layer(value)
Getterget_collision_layer()

The collision layer(s) for all colliders in the TileMap. See Collision layers and masks in the documentation for more information.


  • int collision_mask
Default1
Setterset_collision_mask(value)
Getterget_collision_mask()

The collision mask(s) for all colliders in the TileMap. See Collision layers and masks in the documentation for more information.


  • bool collision_use_kinematic
Defaultfalse
Setterset_collision_use_kinematic(value)
Getterget_collision_use_kinematic()

If true, TileMap collisions will be handled as a kinematic body. If false, collisions will be handled as static body.


  • bool collision_use_parent
Defaultfalse
Setterset_collision_use_parent(value)
Getterget_collision_use_parent()

If true, this tilemap’s collision shape will be added to the collision shape of the parent. The parent has to be a CollisionObject2D.


  • bool compatibility_mode
Defaultfalse
Setterset_compatibility_mode(value)
Getteris_compatibility_mode_enabled()

If true, the compatibility with the tilemaps made in Godot 3.1 or earlier is maintained (textures move when the tile origin changes and rotate if the texture size is not homogeneous). This mode presents problems when doing flip_h, flip_v and transpose tile operations on non-homogeneous isometric tiles (e.g. 2:1), in which the texture could not coincide with the collision, thus it is not recommended for isometric or non-square tiles.

If false, the textures do not move when doing flip_h, flip_v operations if no offset is used, nor when changing the tile origin.

The compatibility mode doesn’t work with the centered_textures option, because displacing textures with the cell_tile_origin option or in irregular tiles is not relevant when centering those textures.


Default0
Setterset_mode(value)
Getterget_mode()

The TileMap orientation mode. See Mode for possible values.


  • int occluder_light_mask
Default1
Setterset_occluder_light_mask(value)
Getterget_occluder_light_mask()

The light mask assigned to all light occluders in the TileMap. The TileSet’s light occluders will cast shadows only from Light2D(s) that have the same light mask(s).


Setterset_tileset(value)
Getterget_tileset()

The assigned TileSet.

Method Descriptions

  • void clear ( )

Clears all cells.


  • void fix_invalid_tiles ( )

Clears cells that do not exist in the tileset.


Returns the tile index of the given cell. If no tile exists in the cell, returns INVALID_CELL.


Returns the coordinate (subtile column and row) of the autotile variation in the tileset. Returns a zero vector if the cell doesn’t have autotiling.


Returns the tile index of the cell given by a Vector2. If no tile exists in the cell, returns INVALID_CELL.


  • bool get_collision_layer_bit ( int bit ) const

Returns true if the given collision layer bit is set.


  • bool get_collision_mask_bit ( int bit ) const

Returns true if the given collision mask bit is set.


  • Array get_used_cells ( ) const

Returns a Vector2 array with the positions of all cells containing a tile from the tileset (i.e. a tile index different from -1).


  • Array get_used_cells_by_id ( int id ) const

Returns an array of all cells with the given tile index specified in id.


Returns a rectangle enclosing the used (non-empty) tiles of the map.


Returns true if the given cell is transposed, i.e. the X and Y axes are swapped.


Returns true if the given cell is flipped in the X axis.


Returns true if the given cell is flipped in the Y axis.


Returns the global position corresponding to the given tilemap (grid-based) coordinates.

Optionally, the tilemap’s half offset can be ignored.


  • void set_cell ( int x, int y, int tile, bool flip_x=false, bool flip_y=false, bool transpose=false, Vector2 autotile_coord=Vector2( 0, 0 ) )

Sets the tile index for the cell given by a Vector2.

An index of -1 clears the cell.

Optionally, the tile can also be flipped, transposed, or given autotile coordinates. The autotile coordinate refers to the column and row of the subtile.

Note: Data such as navigation polygons and collision shapes are not immediately updated for performance reasons.

If you need these to be immediately updated, you can call update_dirty_quadrants.

Overriding this method also overrides it internally, allowing custom logic to be implemented when tiles are placed/removed:

  1. func set_cell(x, y, tile, flip_x=false, flip_y=false, transpose=false, autotile_coord=Vector2())
  2. # Write your custom logic here.
  3. # To call the default method:
  4. .set_cell(x, y, tile, flip_x, flip_y, transpose, autotile_coord)

Sets the tile index for the given cell.

An index of -1 clears the cell.

Optionally, the tile can also be flipped or transposed.

Note: Data such as navigation polygons and collision shapes are not immediately updated for performance reasons.

If you need these to be immediately updated, you can call update_dirty_quadrants.


  • void set_collision_layer_bit ( int bit, bool value )

Sets the given collision layer bit.


  • void set_collision_mask_bit ( int bit, bool value )

Sets the given collision mask bit.


  • void update_bitmask_area ( Vector2 position )

Applies autotiling rules to the cell (and its adjacent cells) referenced by its grid-based X and Y coordinates.


  • void update_bitmask_region ( Vector2 start=Vector2( 0, 0 ), Vector2 end=Vector2( 0, 0 ) )

Applies autotiling rules to the cells in the given region (specified by grid-based X and Y coordinates).

Calling with invalid (or missing) parameters applies autotiling rules for the entire tilemap.


  • void update_dirty_quadrants ( )

Updates the tile map’s quadrants, allowing things such as navigation and collision shapes to be immediately used if modified.


Returns the tilemap (grid-based) coordinates corresponding to the given local position.