Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

TileMap

Inherits: Node2D < CanvasItem < Node < Object

Node for 2D tile-based maps.

描述

Node for 2D tile-based maps. Tilemaps use a TileSet which contain a list of tiles which are used to create grid-based maps. A TileMap may have several layers, layouting tiles on top of each other.

For performance reasons, all TileMap updates are batched at the end of a frame. Notably, this means that scene tiles from a TileSetScenesCollectionSource may be initialized after their parent.

To force an update earlier on, call update_internals.

教程

属性

bool

collision_animatable

false

VisibilityMode

collision_visibility_mode

0

VisibilityMode

navigation_visibility_mode

0

int

rendering_quadrant_size

16

TileSet

tile_set

Methods

void

_tile_data_runtime_update ( int layer, Vector2i coords, TileData tile_data ) virtual

bool

_use_tile_data_runtime_update ( int layer, Vector2i coords ) virtual

void

add_layer ( int to_position )

void

clear ( )

void

clear_layer ( int layer )

void

erase_cell ( int layer, Vector2i coords )

void

fix_invalid_tiles ( )

void

force_update ( int layer=-1 )

int

get_cell_alternative_tile ( int layer, Vector2i coords, bool use_proxies=false ) const

Vector2i

get_cell_atlas_coords ( int layer, Vector2i coords, bool use_proxies=false ) const

int

get_cell_source_id ( int layer, Vector2i coords, bool use_proxies=false ) const

TileData

get_cell_tile_data ( int layer, Vector2i coords, bool use_proxies=false ) const

Vector2i

get_coords_for_body_rid ( RID body )

int

get_layer_for_body_rid ( RID body )

Color

get_layer_modulate ( int layer ) const

String

get_layer_name ( int layer ) const

RID

get_layer_navigation_map ( int layer ) const

int

get_layer_y_sort_origin ( int layer ) const

int

get_layer_z_index ( int layer ) const

int

get_layers_count ( ) const

RID

get_navigation_map ( int layer ) const

Vector2i

get_neighbor_cell ( Vector2i coords, CellNeighbor neighbor ) const

TileMapPattern

get_pattern ( int layer, Vector2i[] coords_array )

Vector2i[]

get_surrounding_cells ( Vector2i coords )

Vector2i[]

get_used_cells ( int layer ) const

Vector2i[]

get_used_cells_by_id ( int layer, int source_id=-1, Vector2i atlas_coords=Vector2i(-1, -1), int alternative_tile=-1 ) const

Rect2i

get_used_rect ( ) const

bool

is_layer_enabled ( int layer ) const

bool

is_layer_y_sort_enabled ( int layer ) const

Vector2i

local_to_map ( Vector2 local_position ) const

Vector2i

map_pattern ( Vector2i position_in_tilemap, Vector2i coords_in_pattern, TileMapPattern pattern )

Vector2

map_to_local ( Vector2i map_position ) const

void

move_layer ( int layer, int to_position )

void

notify_runtime_tile_data_update ( int layer=-1 )

void

remove_layer ( int layer )

void

set_cell ( int layer, Vector2i coords, int source_id=-1, Vector2i atlas_coords=Vector2i(-1, -1), int alternative_tile=0 )

void

set_cells_terrain_connect ( int layer, Vector2i[] cells, int terrain_set, int terrain, bool ignore_empty_terrains=true )

void

set_cells_terrain_path ( int layer, Vector2i[] path, int terrain_set, int terrain, bool ignore_empty_terrains=true )

void

set_layer_enabled ( int layer, bool enabled )

void

set_layer_modulate ( int layer, Color modulate )

void

set_layer_name ( int layer, String name )

void

set_layer_navigation_map ( int layer, RID map )

void

set_layer_y_sort_enabled ( int layer, bool y_sort_enabled )

void

set_layer_y_sort_origin ( int layer, int y_sort_origin )

void

set_layer_z_index ( int layer, int z_index )

void

set_navigation_map ( int layer, RID map )

void

set_pattern ( int layer, Vector2i position, TileMapPattern pattern )

void

update_internals ( )


信号

changed ( )

Emitted when the TileSet of this TileMap changes.


Enumerations

enum VisibilityMode:

VisibilityMode VISIBILITY_MODE_DEFAULT = 0

Use the debug settings to determine visibility.

VisibilityMode VISIBILITY_MODE_FORCE_HIDE = 2

Always hide.

VisibilityMode VISIBILITY_MODE_FORCE_SHOW = 1

Always show.


Property Descriptions

bool collision_animatable = false

  • void set_collision_animatable ( bool value )

  • bool is_collision_animatable ( )

If enabled, the TileMap will see its collisions synced to the physics tick and change its collision type from static to kinematic. This is required to create TileMap-based moving platform.

Note: Enabling collision_animatable may have a small performance impact, only do it if the TileMap is moving and has colliding tiles.


VisibilityMode collision_visibility_mode = 0

Show or hide the TileMap's collision shapes. If set to VISIBILITY_MODE_DEFAULT, this depends on the show collision debug settings.


VisibilityMode navigation_visibility_mode = 0

Show or hide the TileMap's navigation meshes. If set to VISIBILITY_MODE_DEFAULT, this depends on the show navigation debug settings.


int rendering_quadrant_size = 16

  • void set_rendering_quadrant_size ( int value )

  • int get_rendering_quadrant_size ( )

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


TileSet tile_set

The assigned TileSet.


Method Descriptions

void _tile_data_runtime_update ( int layer, Vector2i coords, TileData tile_data ) virtual

Called with a TileData object about to be used internally by the TileMap, allowing its modification at runtime.

This method is only called if _use_tile_data_runtime_update is implemented and returns true for the given tile coords and layer.

Warning: The tile_data object's sub-resources are the same as the one in the TileSet. Modifying them might impact the whole TileSet. Instead, make sure to duplicate those resources.

Note: If the properties of tile_data object should change over time, use notify_runtime_tile_data_update to notify the TileMap it needs an update.


bool _use_tile_data_runtime_update ( int layer, Vector2i coords ) virtual

Should return true if the tile at coordinates coords on layer layer requires a runtime update.

Warning: Make sure this function only return true when needed. Any tile processed at runtime without a need for it will imply a significant performance penalty.

Note: If the result of this function should changed, use notify_runtime_tile_data_update to notify the TileMap it needs an update.


void add_layer ( int to_position )

Adds a layer at the given position to_position in the array. If to_position is negative, the position is counted from the end, with -1 adding the layer at the end of the array.


void clear ( )

Clears all cells.


void clear_layer ( int layer )

Clears all cells on the given layer.

If layer is negative, the layers are accessed from the last one.


void erase_cell ( int layer, Vector2i coords )

Erases the cell on layer layer at coordinates coords.

If layer is negative, the layers are accessed from the last one.


void fix_invalid_tiles ( )

Clears cells that do not exist in the tileset.


void force_update ( int layer=-1 )

Deprecated. See notify_runtime_tile_data_update and update_internals.


int get_cell_alternative_tile ( int layer, Vector2i coords, bool use_proxies=false ) const

Returns the tile alternative ID of the cell on layer layer at coords. If use_proxies is false, ignores the TileSet's tile proxies, returning the raw alternative identifier. See TileSet.map_tile_proxy.

If layer is negative, the layers are accessed from the last one.


Vector2i get_cell_atlas_coords ( int layer, Vector2i coords, bool use_proxies=false ) const

Returns the tile atlas coordinates ID of the cell on layer layer at coordinates coords. If use_proxies is false, ignores the TileSet's tile proxies, returning the raw alternative identifier. See TileSet.map_tile_proxy.

If layer is negative, the layers are accessed from the last one.


int get_cell_source_id ( int layer, Vector2i coords, bool use_proxies=false ) const

Returns the tile source ID of the cell on layer layer at coordinates coords. Returns -1 if the cell does not exist.

If use_proxies is false, ignores the TileSet's tile proxies, returning the raw alternative identifier. See TileSet.map_tile_proxy.

If layer is negative, the layers are accessed from the last one.


TileData get_cell_tile_data ( int layer, Vector2i coords, bool use_proxies=false ) const

Returns the TileData object associated with the given cell, or null if the cell does not exist or is not a TileSetAtlasSource.

If layer is negative, the layers are accessed from the last one.

If use_proxies is false, ignores the TileSet's tile proxies, returning the raw alternative identifier. See TileSet.map_tile_proxy.

func get_clicked_tile_power():
    var clicked_cell = tile_map.local_to_map(tile_map.get_local_mouse_position())
    var data = tile_map.get_cell_tile_data(0, clicked_cell)
    if data:
        return data.get_custom_data("power")
    else:
        return 0

Vector2i get_coords_for_body_rid ( RID body )

Returns the coordinates of the tile for given physics body RID. Such RID can be retrieved from KinematicCollision2D.get_collider_rid, when colliding with a tile.


int get_layer_for_body_rid ( RID body )

Returns the tilemap layer of the tile for given physics body RID. Such RID can be retrieved from KinematicCollision2D.get_collider_rid, when colliding with a tile.


Color get_layer_modulate ( int layer ) const

Returns a TileMap layer's modulate.

If layer is negative, the layers are accessed from the last one.


String get_layer_name ( int layer ) const

Returns a TileMap layer's name.

If layer is negative, the layers are accessed from the last one.


RID get_layer_navigation_map ( int layer ) const

Returns the NavigationServer2D navigation map RID currently assigned to the specified TileMap layer.

By default the TileMap uses the default World2D navigation map for the first TileMap layer. For each additional TileMap layer a new navigation map is created for the additional layer.

In order to make NavigationAgent2D switch between TileMap layer navigation maps use NavigationAgent2D.set_navigation_map with the navigation map received from get_layer_navigation_map.

If layer is negative, the layers are accessed from the last one.


int get_layer_y_sort_origin ( int layer ) const

Returns a TileMap layer's Y sort origin.

If layer is negative, the layers are accessed from the last one.


int get_layer_z_index ( int layer ) const

Returns a TileMap layer's Z-index value.

If layer is negative, the layers are accessed from the last one.


int get_layers_count ( ) const

Returns the number of layers in the TileMap.


RID get_navigation_map ( int layer ) const

See get_layer_navigation_map.


Vector2i get_neighbor_cell ( Vector2i coords, CellNeighbor neighbor ) const

Returns the neighboring cell to the one at coordinates coords, identified by the neighbor direction. This method takes into account the different layouts a TileMap can take.


TileMapPattern get_pattern ( int layer, Vector2i[] coords_array )

Creates a new TileMapPattern from the given layer and set of cells.

If layer is negative, the layers are accessed from the last one.


Vector2i[] get_surrounding_cells ( Vector2i coords )

Returns the list of all neighbourings cells to the one at coords.


Vector2i[] get_used_cells ( int layer ) const

Returns a Vector2i array with the positions of all cells containing a tile in the given layer. A cell is considered empty if its source identifier equals -1, its atlas coordinates identifiers is Vector2(-1, -1) and its alternative identifier is -1.

If layer is negative, the layers are accessed from the last one.


Vector2i[] get_used_cells_by_id ( int layer, int source_id=-1, Vector2i atlas_coords=Vector2i(-1, -1), int alternative_tile=-1 ) const

Returns a Vector2i array with the positions of all cells containing a tile in the given layer. Tiles may be filtered according to their source (source_id), their atlas coordinates (atlas_coords) or alternative id (alternative_tile).

If a parameter has its value set to the default one, this parameter is not used to filter a cell. Thus, if all parameters have their respective default value, this method returns the same result as get_used_cells.

A cell is considered empty if its source identifier equals -1, its atlas coordinates identifiers is Vector2(-1, -1) and its alternative identifier is -1.

If layer is negative, the layers are accessed from the last one.


Rect2i get_used_rect ( ) const

Returns a rectangle enclosing the used (non-empty) tiles of the map, including all layers.


bool is_layer_enabled ( int layer ) const

Returns if a layer is enabled.

If layer is negative, the layers are accessed from the last one.


bool is_layer_y_sort_enabled ( int layer ) const

Returns if a layer Y-sorts its tiles.

If layer is negative, the layers are accessed from the last one.


Vector2i local_to_map ( Vector2 local_position ) const

Returns the map coordinates of the cell containing the given local_position. If local_position is in global coordinates, consider using Node2D.to_local before passing it to this method. See also map_to_local.


Vector2i map_pattern ( Vector2i position_in_tilemap, Vector2i coords_in_pattern, TileMapPattern pattern )

Returns for the given coordinate coords_in_pattern in a TileMapPattern the corresponding cell coordinates if the pattern was pasted at the position_in_tilemap coordinates (see set_pattern). This mapping is required as in half-offset tile shapes, the mapping might not work by calculating position_in_tile_map + coords_in_pattern.


Vector2 map_to_local ( Vector2i map_position ) const

Returns the centered position of a cell in the TileMap's local coordinate space. To convert the returned value into global coordinates, use Node2D.to_global. See also local_to_map.

Note: This may not correspond to the visual position of the tile, i.e. it ignores the TileData.texture_origin property of individual tiles.


void move_layer ( int layer, int to_position )

Moves the layer at index layer to the given position to_position in the array.


void notify_runtime_tile_data_update ( int layer=-1 )

Notifies the TileMap node that calls to _use_tile_data_runtime_update or _tile_data_runtime_update will lead to different results. This will thus trigger a TileMap update.

If layer is provided, only notifies changes for the given layer. Providing the layer argument (when applicable) is usually preferred for performance reasons.

Warning: Updating the TileMap is computationally expensive and may impact performance. Try to limit the number of calls to this function to avoid unnecessary update.

Note: This does not trigger a direct update of the TileMap, the update will be done at the end of the frame as usual (unless you call update_internals).


void remove_layer ( int layer )

Removes the layer at index layer.


void set_cell ( int layer, Vector2i coords, int source_id=-1, Vector2i atlas_coords=Vector2i(-1, -1), int alternative_tile=0 )

Sets the tile identifiers for the cell on layer layer at coordinates coords. Each tile of the TileSet is identified using three parts:

If source_id is set to -1, atlas_coords to Vector2i(-1, -1) or alternative_tile to -1, the cell will be erased. An erased cell gets all its identifiers automatically set to their respective invalid values, namely -1, Vector2i(-1, -1) and -1.

If layer is negative, the layers are accessed from the last one.


void set_cells_terrain_connect ( int layer, Vector2i[] cells, int terrain_set, int terrain, bool ignore_empty_terrains=true )

Update all the cells in the cells coordinates array so that they use the given terrain for the given terrain_set. If an updated cell has the same terrain as one of its neighboring cells, this function tries to join the two. This function might update neighboring tiles if needed to create correct terrain transitions.

If ignore_empty_terrains is true, empty terrains will be ignored when trying to find the best fitting tile for the given terrain constraints.

If layer is negative, the layers are accessed from the last one.

Note: To work correctly, this method requires the TileMap's TileSet to have terrains set up with all required terrain combinations. Otherwise, it may produce unexpected results.


void set_cells_terrain_path ( int layer, Vector2i[] path, int terrain_set, int terrain, bool ignore_empty_terrains=true )

Update all the cells in the path coordinates array so that they use the given terrain for the given terrain_set. The function will also connect two successive cell in the path with the same terrain. This function might update neighboring tiles if needed to create correct terrain transitions.

If ignore_empty_terrains is true, empty terrains will be ignored when trying to find the best fitting tile for the given terrain constraints.

If layer is negative, the layers are accessed from the last one.

Note: To work correctly, this method requires the TileMap's TileSet to have terrains set up with all required terrain combinations. Otherwise, it may produce unexpected results.


void set_layer_enabled ( int layer, bool enabled )

Enables or disables the layer layer. A disabled layer is not processed at all (no rendering, no physics, etc...).

If layer is negative, the layers are accessed from the last one.


void set_layer_modulate ( int layer, Color modulate )

Sets a layer's color. It will be multiplied by tile's color and TileMap's modulate.

If layer is negative, the layers are accessed from the last one.


void set_layer_name ( int layer, String name )

Sets a layer's name. This is mostly useful in the editor.

If layer is negative, the layers are accessed from the last one.


void set_layer_navigation_map ( int layer, RID map )

Assigns a NavigationServer2D navigation map RID to the specified TileMap layer.

By default the TileMap uses the default World2D navigation map for the first TileMap layer. For each additional TileMap layer a new navigation map is created for the additional layer.

In order to make NavigationAgent2D switch between TileMap layer navigation maps use NavigationAgent2D.set_navigation_map with the navigation map received from get_layer_navigation_map.

If layer is negative, the layers are accessed from the last one.


void set_layer_y_sort_enabled ( int layer, bool y_sort_enabled )

Enables or disables a layer's Y-sorting. If a layer is Y-sorted, the layer will behave as a CanvasItem node where each of its tile gets Y-sorted.

Y-sorted layers should usually be on different Z-index values than not Y-sorted layers, otherwise, each of those layer will be Y-sorted as whole with the Y-sorted one. This is usually an undesired behavior.

If layer is negative, the layers are accessed from the last one.


void set_layer_y_sort_origin ( int layer, int y_sort_origin )

Sets a layer's Y-sort origin value. This Y-sort origin value is added to each tile's Y-sort origin value.

This allows, for example, to fake a different height level on each layer. This can be useful for top-down view games.

If layer is negative, the layers are accessed from the last one.


void set_layer_z_index ( int layer, int z_index )

Sets a layers Z-index value. This Z-index is added to each tile's Z-index value.

If layer is negative, the layers are accessed from the last one.


void set_navigation_map ( int layer, RID map )

See set_layer_navigation_map.


void set_pattern ( int layer, Vector2i position, TileMapPattern pattern )

Paste the given TileMapPattern at the given position and layer in the tile map.

If layer is negative, the layers are accessed from the last one.


void update_internals ( )

Triggers a direct update of the TileMap. Usually, calling this function is not needed, as TileMap node updates automatically when one of its properties or cells is modified.

However, for performance reasons, those updates are batched and delayed to the end of the frame. Calling this function will force the TileMap to update right away instead.

Warning: Updating the TileMap is computationally expensive and may impact performance. Try to limit the number of updates and how many tiles they impact.