TileMapLayer
Entity for 2D tile-based maps.
Entity for 2D tile-based maps.
Entity for 2D tile-based maps. A TileMapLayer uses a TileSet which contain a list of tiles which are used to create grid-based maps. Unlike the TileMap node, which is deprecated, TileMapLayer has only one layer of tiles. You can use several TileMapLayer to achieve the same result as a TileMap node.
For performance reasons, all TileMap updates are batched at the end of a frame. Notably, this means that scene tiles from a TileSetScenesCollectionSource are initialized after their parent. This is only queued when inside the scene tree.
To force an update earlier on, call TileMapLayer.UpdateInternals.
Note: For performance and compatibility reasons, the coordinates serialized by TileMapLayer are limited to 16-bit signed integers, i.e. the range for X and Y coordinates is from -32768 to 32767. When saving tile data, tiles outside this range are wrapped.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
TileMapLayerConstructor
public TileMapLayer();Properties13
CollisionEnabledProperty
public bool CollisionEnabled { get; set; }Enable or disable collisions.
CollisionVisibilityModeProperty
public TileMapLayer.DebugVisibilityMode CollisionVisibilityMode { get; set; }Show or hide the TileMapLayer's collision shapes. If set to TileMapLayer.DebugVisibilityMode.Default, this depends on the show collision debug settings.
EnabledProperty
public bool Enabled { get; set; }If false, disables this TileMapLayer completely (rendering, collision, navigation, scene tiles, etc.)
NavigationEnabledProperty
public bool NavigationEnabled { get; set; }If true, navigation regions are enabled.
NavigationVisibilityModeProperty
public TileMapLayer.DebugVisibilityMode NavigationVisibilityMode { get; set; }Show or hide the TileMapLayer's navigation meshes. If set to TileMapLayer.DebugVisibilityMode.Default, this depends on the show navigation debug settings.
OcclusionEnabledProperty
public bool OcclusionEnabled { get; set; }Enable or disable light occlusion.
PhysicsQuadrantSizeProperty
public int PhysicsQuadrantSize { get; set; }The TileMapLayer's physics quadrant size. Within a physics quadrant, cells with similar physics properties are grouped together and their collision shapes get merged. TileMapLayer.PhysicsQuadrantSize defines the length of a square's side, in the map's coordinate system, that forms the quadrant. Thus, the default quadrant size groups together 16 * 16 = 256 tiles.
Note: As quadrants are created according to the map's coordinate system, the quadrant's "square shape" might not look like square in the TileMapLayer's local coordinate system.
Note: This impacts the value returned by TileMapLayer.GetCoordsForBodyRid. Higher values will make that function less precise. To get the exact cell coordinates, you need to set TileMapLayer.PhysicsQuadrantSize to 1, which disables physics chunking.
RenderingQuadrantSizeProperty
public int RenderingQuadrantSize { get; set; }The TileMapLayer's rendering quadrant size. A quadrant is a group of tiles to be drawn together on a single canvas item, for optimization purposes. TileMapLayer.RenderingQuadrantSize defines the length of a square's side, in the map's coordinate system, that forms the quadrant. Thus, the default quadrant size groups together 16 * 16 = 256 tiles.
The quadrant size does not apply on a Y-sorted TileMapLayer, as tiles are grouped by Y position instead in that case.
Note: As quadrants are created according to the map's coordinate system, the quadrant's "square shape" might not look like square in the TileMapLayer's local coordinate system.
TileMapDataProperty
public byte[] TileMapData { get; set; }Binary cell data, excluding the TileSet and its textures. The verified format stores signed 16-bit coordinates; keep them in [-32768,32767]. Runtime cell (40000,0) becomes (-25536,0) after this roundtrip. This is not a universal long-term save format; retain the matching TileSet and your own schema version.
TileSetProperty
UseKinematicBodiesProperty
public bool UseKinematicBodies { get; set; }If true, this TileMapLayer collision shapes will be instantiated as kinematic bodies. This can be needed for moving TileMapLayer nodes (i.e. moving platforms).
XDrawOrderReversedProperty
public bool XDrawOrderReversed { get; set; }If CanvasItem.YSortEnabled is enabled, setting this to true will reverse the order the tiles are drawn on the X-axis.
YSortOriginProperty
public int YSortOrigin { get; set; }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. This can be useful for top-down view games.
Methods59
Clearvoid
public void Clear();Clears all cells.
EraseCellvoid
public void EraseCell(Vector2I coords);Erases the cell at coordinates coords.
| Name | Type | Default |
|---|---|---|
coords | Vector2I | — |
FixInvalidTilesvoid
public void FixInvalidTiles();Clears cells containing tiles that do not exist in the TileMapLayer.TileSet.
GetCellAlternativeTileint
public int GetCellAlternativeTile(Vector2I coords);Returns the tile alternative ID of the cell at coordinates coords.
| Name | Type | Default |
|---|---|---|
coords | Vector2I | — |
intGetCellAtlasCoordsVector2I
public Vector2I GetCellAtlasCoords(Vector2I coords);Returns the tile atlas coordinates ID of the cell at coordinates coords. Returns Vector2i(-1, -1) if the cell does not exist.
| Name | Type | Default |
|---|---|---|
coords | Vector2I | — |
Vector2IGetCellSourceIdint
public int GetCellSourceId(Vector2I coords);Returns the tile source ID of the cell at coordinates coords. Returns -1 if the cell does not exist.
| Name | Type | Default |
|---|---|---|
coords | Vector2I | — |
intGetCellTileDataTileData
public TileData GetCellTileData(Vector2I coords);Reads TileData for a valid atlas cell. This is tile data in the shared TileSet, not unique mutable state for the cell instance. Assign TileSet and check cell existence before using it.
| Name | Type | Default |
|---|---|---|
coords | Vector2I | — |
coordsLayer grid cell coordinate.
TileDataTileData from TileSetAtlasSource, or null for an empty cell or another source type. Check the result before accessing data.
GetCollisionVisibilityModeTileMapLayer.DebugVisibilityMode
public TileMapLayer.DebugVisibilityMode GetCollisionVisibilityMode();TileMapLayer.DebugVisibilityModeGetCoordsForBodyRidVector2I
public Vector2I GetCoordsForBodyRid(Rid body);Returns the coordinates of the physics quadrant (see TileMapLayer.PhysicsQuadrantSize) for given physics body Rid. Such an Rid can be retrieved from KinematicCollision2D.GetColliderRid, when colliding with a tile.
Note: Higher values of TileMapLayer.PhysicsQuadrantSize will make this function less precise. To get the exact cell coordinates, you need to set TileMapLayer.PhysicsQuadrantSize to 1, which disables physics chunking.
| Name | Type | Default |
|---|---|---|
body | Rid | — |
Vector2IGetNavigationMapRid
public Rid GetNavigationMap();Returns the Rid of the NavigationServer2D navigation used by this TileMapLayer.
By default this returns the default World2D navigation map, unless a custom map was provided using TileMapLayer.SetNavigationMap.
RidGetNavigationVisibilityModeTileMapLayer.DebugVisibilityMode
public TileMapLayer.DebugVisibilityMode GetNavigationVisibilityMode();TileMapLayer.DebugVisibilityModeGetNeighborCellVector2I
public Vector2I GetNeighborCell(Vector2I coords, TileSet.CellNeighbor neighbor);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.
| Name | Type | Default |
|---|---|---|
coords | Vector2I | — |
neighbor | TileSet.CellNeighbor | — |
Vector2IGetPatternTileMapPattern
public TileMapPattern GetPattern(Reckfell.Collections.Array<Vector2I> coordsArray);Creates and returns a new TileMapPattern from the given array of cells. See also TileMapLayer.SetPattern.
| Name | Type | Default |
|---|---|---|
coordsArray | Reckfell.Collections.Array<Vector2I> | — |
TileMapPatternGetPhysicsQuadrantSizeint
public int GetPhysicsQuadrantSize();intGetRenderingQuadrantSizeint
public int GetRenderingQuadrantSize();intGetSurroundingCellsReckfell.Collections.Array<Vector2I>
public Reckfell.Collections.Array<Vector2I> GetSurroundingCells(Vector2I coords);Returns the list of all neighboring cells to the one at coords. Any neighboring cell is one that is touching edges, so for a square cell 4 cells would be returned, for a hexagon 6 cells are returned.
| Name | Type | Default |
|---|---|---|
coords | Vector2I | — |
Reckfell.Collections.Array<Vector2I>GetTileMapDataAsArraybyte[]
public byte[] GetTileMapDataAsArray();byte[]GetTileSetTileSet
public TileSet GetTileSet();TileSetGetUsedCellsReckfell.Collections.Array<Vector2I>
public Reckfell.Collections.Array<Vector2I> GetUsedCells();Returns a Vector2I array with the positions of all cells containing a tile. A cell is considered empty if its source identifier equals -1, its atlas coordinate identifier is Vector2(-1, -1) and its alternative identifier is -1.
Reckfell.Collections.Array<Vector2I>GetUsedCellsByIdReckfell.Collections.Array<Vector2I>
public Reckfell.Collections.Array<Vector2I> GetUsedCellsById(int sourceId = -1, Vector2I? atlasCoords = default, int alternativeTile = -1);Returns a Vector2I array with the positions of all cells containing a tile. 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 values, this method returns the same result as TileMapLayer.GetUsedCells.
A cell is considered empty if its source identifier equals -1, its atlas coordinate identifier is Vector2(-1, -1) and its alternative identifier is -1.
| Name | Type | Default |
|---|---|---|
sourceId | int | -1 |
atlasCoords | Vector2I? | default |
alternativeTile | int | -1 |
atlasCoordsIf the parameter is null, then the default value is new Vector2I(-1, -1).
Reckfell.Collections.Array<Vector2I>GetUsedRectRect2I
public Rect2I GetUsedRect();Bounding rectangle of nonempty cells in grid coordinates. Not pixel dimensions, screen bounds or a collision region.
Rect2ICell-space Rect2I, empty when there are no cells. A 20×15 map spanning (0,0) through (19,14) returns position (0,0) and size (20,15).
GetYSortOriginint
public int GetYSortOrigin();intHasBodyRidbool
public bool HasBodyRid(Rid body);Returns whether the provided body Rid belongs to one of this TileMapLayer's cells.
| Name | Type | Default |
|---|---|---|
body | Rid | — |
boolIsCellFlippedHbool
public bool IsCellFlippedH(Vector2I coords);Returns true if the cell at coordinates coords is flipped horizontally. The result is valid only for atlas sources.
| Name | Type | Default |
|---|---|---|
coords | Vector2I | — |
boolIsCellFlippedVbool
public bool IsCellFlippedV(Vector2I coords);Returns true if the cell at coordinates coords is flipped vertically. The result is valid only for atlas sources.
| Name | Type | Default |
|---|---|---|
coords | Vector2I | — |
boolIsCellTransposedbool
public bool IsCellTransposed(Vector2I coords);Returns true if the cell at coordinates coords is transposed. The result is valid only for atlas sources.
| Name | Type | Default |
|---|---|---|
coords | Vector2I | — |
boolIsCollisionEnabledbool
public bool IsCollisionEnabled();boolIsEnabledbool
public bool IsEnabled();boolIsNavigationEnabledbool
public bool IsNavigationEnabled();boolIsOcclusionEnabledbool
public bool IsOcclusionEnabled();boolIsUsingKinematicBodiesbool
public bool IsUsingKinematicBodies();boolIsXDrawOrderReversedbool
public bool IsXDrawOrderReversed();boolLocalToMapVector2I
public Vector2I LocalToMap(Vector2 localPosition);Finds a cell from a layer-local position. Convert canvas-space input through ToLocal first. Integer-casting a division is not equivalent, especially for negative values or other layouts.
| Name | Type | Default |
|---|---|---|
localPosition | Vector2 | — |
localPositionPosition local to TileMapLayer, not an unconverted InputEvent.Position.
Vector2ICell coordinate. On a 16×16 square grid, local point (-1,-1) belongs to cell (-1,-1).
MapPatternVector2I
public Vector2I MapPattern(Vector2I positionInTilemap, Vector2I coordsInPattern, TileMapPattern pattern);Returns for the given coordinates coords_in_pattern in a TileMapPattern the corresponding cell coordinates if the pattern was pasted at the position_in_tilemap coordinates (see TileMapLayer.SetPattern). This mapping is required as in half-offset tile shapes, the mapping might not work by calculating position_in_tile_map + coords_in_pattern.
| Name | Type | Default |
|---|---|---|
positionInTilemap | Vector2I | — |
coordsInPattern | Vector2I | — |
pattern | TileMapPattern | — |
Vector2IMapToLocalVector2
public Vector2 MapToLocal(Vector2I mapPosition);Converts grid coordinates to a cell center local to TileMapLayer. Accounts for TileSet shape/layout, but not the entity canvas transform or TileData.TextureOrigin artwork offset.
| Name | Type | Default |
|---|---|---|
mapPosition | Vector2I | — |
mapPositionInteger cell coordinate.
Vector2Local cell center. On a 16×16 square grid, cell (2,3) gives (40,56). Apply ToGlobal for a canvas-space position.
NotifyRuntimeTileDataUpdatevoid
public void NotifyRuntimeTileDataUpdate();Notifies the TileMapLayer node that calls to TileMapLayer._UseTileDataRuntimeUpdate or TileMapLayer._TileDataRuntimeUpdate will lead to different results. This will thus trigger a TileMapLayer update.
Warning: Updating the TileMapLayer 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 TileMapLayer, the update will be done at the end of the frame as usual (unless you call TileMapLayer.UpdateInternals).
SetCellvoid
public void SetCell(Vector2I coords, int sourceId = -1, Vector2I? atlasCoords = default, int alternativeTile = 0);Sets a cell tile reference using sourceId, atlasCoords and alternativeTile. Supply the required identifiers explicitly. An invalid identifier makes the cell empty. Logical data changes immediately; rendering/physics batches update later.
| Name | Type | Default |
|---|---|---|
coords | Vector2I | — |
sourceId | int | -1 |
atlasCoords | Vector2I? | default |
alternativeTile | int | 0 |
coordsInteger layer grid coordinates, not pixels or atlas coordinates.
sourceIdSource ID in TileSet. The default -1 removes the tile. Source IDs and image coordinates are different values.
atlasCoordsTile coordinates in the atlas grid. The default means invalid (-1,-1), not Vector2I.Zero. SetCell(coords, 0) therefore clears the cell instead of selecting the first tile.
alternativeTileAlternative ID; 0 selects the base tile. An atlas source can also use its supported transform flags. A value of -1 makes the cell empty.
SetCellsTerrainConnectvoid
public void SetCellsTerrainConnect(Reckfell.Collections.Array<Vector2I> cells, int terrainSet, int terrain, bool ignoreEmptyTerrains = 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.
Note: To work correctly, this method requires the TileMapLayer's TileSet to have terrains set up with all required terrain combinations. Otherwise, it may produce unexpected results.
| Name | Type | Default |
|---|---|---|
cells | Reckfell.Collections.Array<Vector2I> | — |
terrainSet | int | — |
terrain | int | — |
ignoreEmptyTerrains | bool | true |
SetCellsTerrainPathvoid
public void SetCellsTerrainPath(Reckfell.Collections.Array<Vector2I> path, int terrainSet, int terrain, bool ignoreEmptyTerrains = 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.
Note: To work correctly, this method requires the TileMapLayer's TileSet to have terrains set up with all required terrain combinations. Otherwise, it may produce unexpected results.
| Name | Type | Default |
|---|---|---|
path | Reckfell.Collections.Array<Vector2I> | — |
terrainSet | int | — |
terrain | int | — |
ignoreEmptyTerrains | bool | true |
SetCollisionEnabledvoid
public void SetCollisionEnabled(bool enabled);| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetCollisionVisibilityModevoid
public void SetCollisionVisibilityMode(TileMapLayer.DebugVisibilityMode visibilityMode);| Name | Type | Default |
|---|---|---|
visibilityMode | TileMapLayer.DebugVisibilityMode | — |
SetEnabledvoid
public void SetEnabled(bool enabled);| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetNavigationEnabledvoid
public void SetNavigationEnabled(bool enabled);| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetNavigationMapvoid
public void SetNavigationMap(Rid map);Sets a custom map as a NavigationServer2D navigation map. If not set, uses the default World2D navigation map instead.
| Name | Type | Default |
|---|---|---|
map | Rid | — |
SetNavigationVisibilityModevoid
public void SetNavigationVisibilityMode(TileMapLayer.DebugVisibilityMode showNavigation);| Name | Type | Default |
|---|---|---|
showNavigation | TileMapLayer.DebugVisibilityMode | — |
SetOcclusionEnabledvoid
public void SetOcclusionEnabled(bool enabled);| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetPatternvoid
public void SetPattern(Vector2I position, TileMapPattern pattern);Pastes the TileMapPattern at the given position in the tile map. See also TileMapLayer.GetPattern.
| Name | Type | Default |
|---|---|---|
position | Vector2I | — |
pattern | TileMapPattern | — |
SetPhysicsQuadrantSizevoid
public void SetPhysicsQuadrantSize(int size);| Name | Type | Default |
|---|---|---|
size | int | — |
SetRenderingQuadrantSizevoid
public void SetRenderingQuadrantSize(int size);| Name | Type | Default |
|---|---|---|
size | int | — |
SetTileMapDataFromArray2 overloads
public void SetTileMapDataFromArray(byte[] tileMapLayerData);| Name | Type | Default |
|---|---|---|
tileMapLayerData | byte[] | — |
public void SetTileMapDataFromArray(ReadOnlySpan<byte> tileMapLayerData);| Name | Type | Default |
|---|---|---|
tileMapLayerData | ReadOnlySpan<byte> | — |
SetTileSetvoid
public void SetTileSet(TileSet tileSet);| Name | Type | Default |
|---|---|---|
tileSet | TileSet | — |
SetUseKinematicBodiesvoid
public void SetUseKinematicBodies(bool useKinematicBodies);| Name | Type | Default |
|---|---|---|
useKinematicBodies | bool | — |
SetXDrawOrderReversedvoid
public void SetXDrawOrderReversed(bool xDrawOrderReversed);| Name | Type | Default |
|---|---|---|
xDrawOrderReversed | bool | — |
SetYSortOriginvoid
public void SetYSortOrigin(int ySortOrigin);| Name | Type | Default |
|---|---|---|
ySortOrigin | int | — |
UpdateInternalsvoid
public void UpdateInternals();Forces processing of pending internal layer changes. They are normally batched; call once after a group of edits when needed before the deferred update. It does not advance physics time and should not be called after every cell in a large loop.
_TileDataRuntimeUpdatevoid
public virtual void _TileDataRuntimeUpdate(Vector2I coords, TileData tileData);Called with a TileData object about to be used internally by the TileMapLayer, allowing its modification at runtime.
This method is only called if TileMapLayer._UseTileDataRuntimeUpdate is implemented and returns true for the given tile coords.
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 TileMapLayer.NotifyRuntimeTileDataUpdate to notify the TileMapLayer it needs an update.
| Name | Type | Default |
|---|---|---|
coords | Vector2I | — |
tileData | TileData | — |
_UpdateCellsvoid
public virtual void _UpdateCells(Reckfell.Collections.Array<Vector2I> coords, bool forcedCleanup);Called when this TileMapLayer's cells need an internal update. This update may be caused from individual cells being modified or by a change in the TileMapLayer.TileSet (causing all cells to be queued for an update). The first call to this function is always for initializing all the TileMapLayer's cells. coords contains the coordinates of all modified cells, roughly in the order they were modified. forced_cleanup is true when the TileMapLayer's internals should be fully cleaned up. This is the case when:
- The layer is disabled;
- The layer is not visible;
TileMapLayer.TileSetis set tonull;- The node is removed from the tree;
- The node is freed.
Note that any internal update happening while one of these conditions is verified is considered to be a "cleanup". See also
TileMapLayer.UpdateInternals. Warning: Implementing this method may degrade theTileMapLayer's performance.
| Name | Type | Default |
|---|---|---|
coords | Reckfell.Collections.Array<Vector2I> | — |
forcedCleanup | bool | — |
_UseTileDataRuntimeUpdatebool
public virtual bool _UseTileDataRuntimeUpdate(Vector2I coords);Should return true if the tile at coordinates coords requires a runtime update.
Warning: Make sure this function only returns 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 change, use TileMapLayer.NotifyRuntimeTileDataUpdate to notify the TileMapLayer it needs an update.
| Name | Type | Default |
|---|---|---|
coords | Vector2I | — |
boolSignals and events1
ChangedEvent
public event Action Changed;Emitted when this TileMapLayer's properties changes, including changes to its assigned TileSet.
Note: This signal may be emitted very often when batch-modifying a TileMapLayer. Avoid executing complex processing in a connected function, and consider delaying it to the end of the frame instead (i.e. calling ReckfellObject.CallDeferred).
TileMapLayer.DebugVisibilityMode
| Value | Description |
|---|---|
Default = 0 | Hide the collisions or navigation debug shapes in the editor, and use the debug settings to determine their visibility in game (i.e. |
ForceHide = 2 | Always hide the collisions or navigation debug shapes. |
ForceShow = 1 | Always show the collisions or navigation debug shapes. |