TileMap

Inherits:Node2D<CanvasItem<Node<Object

Node for 2D tile-based maps.

Description

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

When doing physics queries against the tilemap, the cell coordinates are encoded asmetadata for each detected collision shape returned by methods such asPhysics2DDirectSpaceState.intersect_shape,Physics2DDirectBodyState.get_contact_collider_shape_metadata, etc.

Tutorials

Properties

bool

cell_clip_uv

false

Transform2D

cell_custom_transform

Transform2D(64,0,0,64,0,0)

HalfOffset

cell_half_offset

2

int

cell_quadrant_size

16

Vector2

cell_size

Vector2(64,64)

TileOrigin

cell_tile_origin

0

bool

cell_y_sort

false

bool

centered_textures

false

float

collision_bounce

0.0

float

collision_friction

1.0

int

collision_layer

1

int

collision_mask

1

bool

collision_use_kinematic

false

bool

collision_use_parent

false

bool

compatibility_mode

false

Mode

mode

0

int

occluder_light_mask

1

bool

show_collision

false

TileSet

tile_set

Methods

void

clear()

void

fix_invalid_tiles()

int

get_cell(int x,int y)const

Vector2

get_cell_autotile_coord(int x,int y)const

int

get_cellv(Vector2 position)const

bool

get_collision_layer_bit(int bit)const

bool

get_collision_mask_bit(int bit)const

Array

get_used_cells()const

Array

get_used_cells_by_id(int id)const

Rect2

get_used_rect()

bool

is_cell_transposed(int x,int y)const

bool

is_cell_x_flipped(int x,int y)const

bool

is_cell_y_flipped(int x,int y)const

Vector2

map_to_world(Vector2 map_position,bool ignore_half_ofs=false)const

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 ))

void

set_cellv(Vector2 position,int tile,bool flip_x=false,bool flip_y=false,bool transpose=false,Vector2 autotile_coord=Vector2( 0, 0 ))

void

set_collision_layer_bit(int bit,bool value)

void

set_collision_mask_bit(int bit,bool value)

void

update_bitmask_area(Vector2 position)

void

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

void

update_dirty_quadrants()

Vector2

world_to_map(Vector2 world_position)const

Signals

  • settings_changed()

Emitted when a tilemap setting has changed.

Enumerations

enumMode:

  • MODE_SQUARE =0 --- Orthogonal orientation mode.

  • MODE_ISOMETRIC =1 --- Isometric orientation mode.

  • MODE_CUSTOM =2 --- Custom orientation mode.


enumHalfOffset:

  • 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).


enumTileOrigin:

  • 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

Default

false

Setter

set_clip_uv(value)

Getter

get_clip_uv()

Iftrue, the cell's UVs will be clipped.


Default

Transform2D(64,0,0,64,0,0)

Setter

set_custom_transform(value)

Getter

get_custom_transform()

The customTransform2D to be applied to the TileMap's cells.


Default

2

Setter

set_half_offset(value)

Getter

get_half_offset()

Amount to offset alternating tiles. SeeHalfOffset for possible values.


  • intcell_quadrant_size

Default

16

Setter

set_quadrant_size(value)

Getter

get_quadrant_size()

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


Default

Vector2(64,64)

Setter

set_cell_size(value)

Getter

get_cell_size()

The TileMap's cell size.


Default

0

Setter

set_tile_origin(value)

Getter

get_tile_origin()

Position for tile origin. SeeTileOrigin for possible values.


Default

false

Setter

set_y_sort_mode(value)

Getter

is_y_sort_mode_enabled()

Iftrue, the TileMap's direct children will be drawn in order of their Y coordinate.


  • boolcentered_textures

Default

false

Setter

set_centered_textures(value)

Getter

is_centered_textures_enabled()

Iftrue, 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.

Iffalse, the texture position start in the top-left corner unlesscompatibility_mode is enabled.


Default

0.0

Setter

set_collision_bounce(value)

Getter

get_collision_bounce()

Bounce value for static body collisions (seecollision_use_kinematic).


Default

1.0

Setter

set_collision_friction(value)

Getter

get_collision_friction()

Friction value for static body collisions (seecollision_use_kinematic).


  • intcollision_layer

Default

1

Setter

set_collision_layer(value)

Getter

get_collision_layer()

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


  • intcollision_mask

Default

1

Setter

set_collision_mask(value)

Getter

get_collision_mask()

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


  • boolcollision_use_kinematic

Default

false

Setter

set_collision_use_kinematic(value)

Getter

get_collision_use_kinematic()

Iftrue, TileMap collisions will be handled as a kinematic body. Iffalse, collisions will be handled as static body.


  • boolcollision_use_parent

Default

false

Setter

set_collision_use_parent(value)

Getter

get_collision_use_parent()

Iftrue, this tilemap's collision shape will be added to the collision shape of the parent. The parent has to be aCollisionObject2D.


  • boolcompatibility_mode

Default

false

Setter

set_compatibility_mode(value)

Getter

is_compatibility_mode_enabled()

Iftrue, 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 doingflip_h,flip_v andtranspose 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.

Iffalse, the textures do not move when doingflip_h,flip_v operations if no offset is used, nor when changing the tile origin.

The compatibility mode doesn't work with thecentered_textures option, because displacing textures with thecell_tile_origin option or in irregular tiles is not relevant when centering those textures.


Default

0

Setter

set_mode(value)

Getter

get_mode()

The TileMap orientation mode. SeeMode for possible values.


  • intoccluder_light_mask

Default

1

Setter

set_occluder_light_mask(value)

Getter

get_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).


Default

false

Setter

set_show_collision(value)

Getter

is_show_collision_enabled()

Iftrue, collision shapes are visible in the editor. Doesn't affect collision shapes visibility at runtime. To show collision shapes at runtime, enableVisible Collision Shapes in theDebug menu instead.


Setter

set_tileset(value)

Getter

get_tileset()

The assignedTileSet.

Method Descriptions

  • voidclear()

Clears all cells.


  • voidfix_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, returnsINVALID_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, returnsINVALID_CELL.


Returnstrue if the given collision layer bit is set.


Returnstrue if the given collision mask bit is set.


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


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


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


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


Returnstrue if the given cell is flipped in the X axis.


Returnstrue if the given cell is flipped in the Y axis.


Returns the local position of the top left corner of the cell corresponding to the given tilemap (grid-based) coordinates.

To get the global position, useNode2D.to_global:

varlocal_position=my_tilemap.map_to_world(map_position)varglobal_position=my_tilemap.to_global(local_position)

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


Sets the tile index for the given cell.

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 callupdate_dirty_quadrants.

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

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

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 callupdate_dirty_quadrants.


  • voidset_collision_layer_bit(int bit,bool value)

Sets the given collision layer bit.


  • voidset_collision_mask_bit(int bit,bool value)

Sets the given collision mask bit.


  • voidupdate_bitmask_area(Vector2 position)

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


  • voidupdate_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.


  • voidupdate_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.

To use this with a global position, first determine the local position withNode2D.to_local:

varlocal_position=my_tilemap.to_local(global_position)varmap_position=my_tilemap.world_to_map(local_position)