CenterContainer in Scene; add_child(TILEMAP_NODE) > it's not centered

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By eod

Maybe CenterContainer doesn’t work this way, but here’s what I’m trying to do:


I have a scene with a CenterContainer set with a min size (300px by 100px).

When the game runs, via gdscript I create a Tilemap node, which is sized 200px by 100px. I then add_child this TileMap node to the CenterContainer. Instead of the tilemap being centered in the container, it’s left justified.

When I create a Label via gdscript and add_child that to the same CenterContainer it does get centered.

Any thoughts how to get that TileMap centered?


P.S. I also tried having a MarginContainer with a left margin of 50px and the TileMap still is flush to left.

:bust_in_silhouette: Reply From: Zylann

CenterContainer only centers nodes inheriting Control. TileMap is not a UI node, and it doesn’t even have an actual size (it is infinite, depends on tiles present inside).

If you want to center your tilemap somehow, you may have to do it manually, by carefully choosing where tiles are relative to the origin of the tilemap, and using get_used_rect() to obtain a local rectangle bounding all present tiles.

Awesome! Your get_used_rect() suggestion got me thinking along those lines. What I ended up doing was something like this:

tilemap_node.translate(Vector2(50,0))

This shifted it over 50px and now it’s centered. :slight_smile:

eod | 2020-03-30 03:06