+2 votes

I want to cover a TileMap with a TextureRect. I'm having trouble getting the size and position of the TileMap in my scene.

I see that I can get the get_total_rect() of the TileMap, but I haven't successfully translated this value into my scene.

in Engine by (173 points)

1 Answer

+3 votes
Best answer

get_used_rect() will return the full rectangle in tile space. For example, if you have a map that is 10 tiles wide and 10 tiles tall, you'll get (0, 0, 10, 10). You then need to convert these into pixel space using map_to_world():

var tile_rect = $TileMap.get_used_rect()
var topleft = $TileMap.map_to_world(tile_rect.position)
var size = $TileMap.map_to_world(tile_rect.size)

If the TileMap is moved, you also need to add its position to place your TextureRect:

$TextureRect.rect_position = topleft + $TileMap.position
$TextureRect.rect_size = size

This works well for square tiles. It gets a bit more complicated for hex or isometric tiles.

by (21,969 points)
selected by

Ah, that must be why I'm having difficulty. I'm using Isometric tiles. Can you explain how I would go about calculating the proper size for the TextureRect from an isometric grid?

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 Frequently asked questions and 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 [email protected] with your username.