Tilemap size

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jospic
:warning: Old Version Published before Godot 3 was released.

Hi,
is there anyway to get title map size, width and height in pixels, or number of tiles in x and y direction?
I need to know these properties from GDScript, but it seems they are not available…
Thanks.
-j

Related with this question.

aaschmitz | 2016-04-20 14:04

:bust_in_silhouette: Reply From: Zylann

Tilemaps are somehow infinite, their bounding rectange is not available, unless you compute it yourself:

func calculate_bounds():
	var used_cells = tilemap.get_used_cells()
	for pos in used_cells:
		if pos.x < min_x:
			min_x = int(pos.x)
		elif pos.x > max_x:
			max_x = int(pos.x)
		if pos.y < min_y:
			min_y = int(pos.y)
		elif pos.y > max_y:
			max_y = int(pos.y)

It would be a nice addition to have the bounds already in Tilemap.

The answer above enabled me to find this much more complete answer: https://forum.godotengine.org/7450/how-do-i-get-tilemaps-size-height-and-width-with-script

apb1963 | 2017-03-21 20:29

@apb1963 This post seems to be hidden, what was the answer?

Matheus Pesegoginski | 2020-04-13 18:10

:bust_in_silhouette: Reply From: CKO

You’ll need to take tilemap’s cell size and scale into consideration when calculating the real-world size of tilemap’s used_rect as follows:

func calculate_bounds(tilemap):
    var cell_bounds = tilemap.get_used_rect()
    # create transform
    var cell_to_pixel = Transform2D(Vector2(tilemap.cell_size.x * tilemap.scale.x, 0), Vector2(0, tilemap.cell_size.y * tilemap.scale.y), Vector2())
    # apply transform
    return Rect2(cell_to_pixel * cell_bounds.position, cell_to_pixel * cell_bounds.size)
:bust_in_silhouette: Reply From: rakkarage

get_used_rect