tilemap
Functions and messages used to manipulate tile map components.
Functions
tilemap.get_bounds(url: string | Hash | Url): LuaMultiReturn<[number, number, number, number]>tilemap.get_tile(url: string | Hash | Url, layer: string | Hash, x: number, y: number): number | undefinedtilemap.get_tile_info(url: string | Hash | Url, layer: string | Hash, x: number, y: number): { index: number; h_flip: boolean; v_flip: boolean; rotate_90: boolean } | undefinedtilemap.get_tiles(url: string | Hash | Url, layer: string | Hash): LuaMap<number, LuaMap<number, number>> | undefinedtilemap.reset_constant(url: string | Hash | Url, constant: string | Hash): voidDtilemap.set_tile(url: string | Hash | Url, layer: string | Hash, x: number, y: number, tile: number, transform_bitmask?: number): booleantilemap.set_visible(url: string | Hash | Url, layer: string | Hash, visible: boolean): void
tilemap.get_bounds(url: string | Hash | Url): LuaMultiReturn<[number, number, number, number]>
Get the bounds for a tile map. This function returns multiple values: The lower left corner index x and y coordinates (1-indexed), the tile map width and the tile map height. The resulting values take all tile map layers into account, meaning that the bounds are calculated as if all layers were collapsed into one.
// get the level bounds.
const [x, y, w, h] = tilemap.get_bounds("/level#tilemap");-- get the level bounds.
local x, y, w, h = tilemap.get_bounds("/level#tilemap")Parameters
url:string | Hash | Url— the tile map
Returns
x:number— x coordinate of the bottom left corner. ⚠️ 1-based; passed to Defold unchanged.y:number— y coordinate of the bottom left corner. ⚠️ 1-based; passed to Defold unchanged.w:number— number of columns (width) in the tile maph:number— number of rows (height) in the tile map
tilemap.get_tile(url: string | Hash | Url, layer: string | Hash, x: number, y: number): number | undefined
Get the tile set at the specified position in the tilemap. The position is identified by the tile index starting at origin with index 1, 1. (see tilemap.set_tile()) Which tile map and layer to query is identified by the URL and the layer name parameters.
function tile_under_player(self: { player_x: number; player_y: number }) {
// get the tile under the player.
const tileno = tilemap.get_tile("/level#tilemap", "foreground", self.player_x, self.player_y);
}-- get the tile under the player.
local tileno = tilemap.get_tile("/level#tilemap", "foreground", self.player_x, self.player_y)Parameters
url:string | Hash | Url— the tile maplayer:string | Hash— name of the layer for the tilex:number— x-coordinate of the tile. ⚠️ 1-based; passed to Defold unchanged.y:number— y-coordinate of the tile. ⚠️ 1-based; passed to Defold unchanged.
Returns
tile:number | undefined— index of the tile
tilemap.get_tile_info(url: string | Hash | Url, layer: string | Hash, x: number, y: number): { index: number; h_flip: boolean; v_flip: boolean; rotate_90: boolean } | undefined
Get the tile information at the specified position in the tilemap. The position is identified by the tile index starting at origin with index 1, 1. (see tilemap.set_tile()) Which tile map and layer to query is identified by the URL and the layer name parameters.
function tile_info_under_player(self: { player_x: number; player_y: number }) {
// get the tile under the player.
const tile_info = tilemap.get_tile_info("/level#tilemap", "foreground", self.player_x, self.player_y);
pprint(tile_info);
// {
// index = 0,
// h_flip = false,
// v_flip = true,
// rotate_90 = false
// }
}-- get the tile under the player.
local tile_info = tilemap.get_tile_info("/level#tilemap", "foreground", self.player_x, self.player_y)
pprint(tile_info)
-- {
-- index = 0,
-- h_flip = false,
-- v_flip = true,
-- rotate_90 = false
-- }Parameters
url:string | Hash | Url— the tile maplayer:string | Hash— name of the layer for the tilex:number— x-coordinate of the tile. ⚠️ 1-based; passed to Defold unchanged.y:number— y-coordinate of the tile. ⚠️ 1-based; passed to Defold unchanged.
Returns
tile_info:{ index: number; h_flip: boolean; v_flip: boolean; rotate_90: boolean } | undefined— full tile information
tilemap.get_tiles(url: string | Hash | Url, layer: string | Hash): LuaMap<number, LuaMap<number, number>> | undefined
Retrieves all the tiles for the specified layer in the tilemap.
It returns a table of rows where the keys are the
tile positions (see tilemap.get_bounds()).
You can iterate it using tiles[row_index][column_index].
const [left, bottom, columns_count, rows_count] = tilemap.get_bounds("#tilemap");
const tiles = tilemap.get_tiles("#tilemap", "layer");
let tile = 0;
let count = 0;
for (let row_index = bottom; row_index <= bottom + rows_count - 1; row_index++) {
for (let column_index = left; column_index <= left + columns_count - 1; column_index++) {
tile = tiles?.get(row_index)?.get(column_index) ?? 0;
count = count + 1;
}
}local left, bottom, columns_count, rows_count = tilemap.get_bounds("#tilemap")
local tiles = tilemap.get_tiles("#tilemap", "layer")
local tile, count = 0, 0
for row_index = bottom, bottom + rows_count - 1 do
for column_index = left, left + columns_count - 1 do
tile = tiles[row_index][column_index]
count = count + 1
end
endParameters
url:string | Hash | Url— the tilemaplayer:string | Hash— the name of the layer for the tiles
Returns
tiles:LuaMap<number, LuaMap<number, number>> | undefined— a table of rows representing the layer
tilemap.reset_constant(url: string | Hash | Url, constant: string | Hash): voidD
- Deprecated since 1.13.0
Resets a shader constant for a tile map component. The constant must be defined in the material assigned to the tile map. Resetting a constant through this function implies that the value defined in the material will be used. Which tile map to reset a constant for is identified by the URL.
tilemap.reset_constant("#tilemap", "tint");function init(self)
tilemap.reset_constant("#tilemap", "tint")
endParameters
url:string | Hash | Url— the tile map that should have a constant resetconstant:string | Hash— name of the constant
tilemap.set_tile(url: string | Hash | Url, layer: string | Hash, x: number, y: number, tile: number, transform_bitmask?: number): boolean
Replace a tile in a tile map with a new tile. The coordinates of the tiles are indexed so that the "first" tile just above and to the right of origin has coordinates 1,1. Tiles to the left of and below origin are indexed 0, -1, -2 and so forth.
+-------+-------+------+------+
| 0,3 | 1,3 | 2,3 | 3,3 |
+-------+-------+------+------+
| 0,2 | 1,2 | 2,2 | 3,2 |
+-------+-------+------+------+
| 0,1 | 1,1 | 2,1 | 3,1 |
+-------O-------+------+------+
| 0,0 | 1,0 | 2,0 | 3,0 |
+-------+-------+------+------+The coordinates must be within the bounds of the tile map as it were created. That is, it is not possible to extend the size of a tile map by setting tiles outside the edges. To clear a tile, set the tile to number 0. Which tile map and layer to manipulate is identified by the URL and the layer name parameters.
function set_tiles(self: { player_x: number; player_y: number }, x: number, y: number) {
// Clear the tile under the player.
tilemap.set_tile("/level#tilemap", "foreground", self.player_x, self.player_y, 0);
// Set tile with different combination of flip and rotation
tilemap.set_tile("#tilemap", "layer1", x, y, 0, tilemap.H_FLIP + tilemap.V_FLIP + tilemap.ROTATE_90);
tilemap.set_tile("#tilemap", "layer1", x, y, 0, tilemap.H_FLIP + tilemap.ROTATE_270);
tilemap.set_tile("#tilemap", "layer1", x, y, 0, tilemap.V_FLIP + tilemap.H_FLIP);
tilemap.set_tile("#tilemap", "layer1", x, y, 0, tilemap.ROTATE_180);
}-- Clear the tile under the player.
tilemap.set_tile("/level#tilemap", "foreground", self.player_x, self.player_y, 0)
-- Set tile with different combination of flip and rotation
tilemap.set_tile("#tilemap", "layer1", x, y, 0, tilemap.H_FLIP + tilemap.V_FLIP + tilemap.ROTATE_90)
tilemap.set_tile("#tilemap", "layer1", x, y, 0, tilemap.H_FLIP + tilemap.ROTATE_270)
tilemap.set_tile("#tilemap", "layer1", x, y, 0, tilemap.V_FLIP + tilemap.H_FLIP)
tilemap.set_tile("#tilemap", "layer1", x, y, 0, tilemap.ROTATE_180)Parameters
url:string | Hash | Url— the tile maplayer:string | Hash— name of the layer for the tilex:number— x-coordinate of the tile. ⚠️ 1-based; passed to Defold unchanged.y:number— y-coordinate of the tile. ⚠️ 1-based; passed to Defold unchanged.tile:number— index of new tile to set. 0 resets the celltransform_bitmask?:number— optional flip and/or rotation should be applied to the tile
tilemap.set_visible(url: string | Hash | Url, layer: string | Hash, visible: boolean): void
Sets the visibility of the tilemap layer
// Disable rendering of the layer
tilemap.set_visible("/level#tilemap", "foreground", false);-- Disable rendering of the layer
tilemap.set_visible("/level#tilemap", "foreground", false)Parameters
url:string | Hash | Url— the tile maplayer:string | Hash— name of the layer for the tilevisible:boolean— should the layer be visible
Constants
tilemap.H_FLIP: number & { readonly __brand: "tilemap.H_FLIP" }
flip tile horizontally
tilemap.ROTATE_180: number & { readonly __brand: "tilemap.ROTATE_180" }
rotate tile 180 degrees clockwise
tilemap.ROTATE_270: number & { readonly __brand: "tilemap.ROTATE_270" }
rotate tile 270 degrees clockwise
tilemap.ROTATE_90: number & { readonly __brand: "tilemap.ROTATE_90" }
rotate tile 90 degrees clockwise
tilemap.V_FLIP: number & { readonly __brand: "tilemap.V_FLIP" }
flip tile vertically
Properties
material: Hash
The material used when rendering the tile map. The type of the property is hash.
tile_source: Hash
The tile source used when rendering the tile map. The type of the property is hash.