+2 votes

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

in Engine by (1,469 points)

Related with this question.

3 Answers

+4 votes
Best answer

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.

by (28,789 points)
selected by

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

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

+1 vote

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)
by (93 points)
+3 votes
by (1,863 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.