Topic was automatically imported from the old Question2Answer platform.
Asked By
Socrates
I have a grid container that contains a number of TextureButtons. All I want to do is make it so that the it’s in the center of the screen, with an equal amount of space on the left and right sides. I’ve found similar questions asked here, but I just don’t understand.
I hate how Node2D is scaling child nodes…
Create Node2D inside root Node2D, to new created Node2D attach GridContainer and then insert script (to Node2D), should look like this:
Node2D (root)
- Node2D
- GridContainer
extends Node2D
func _ready():
var viewport_size = get_viewport_rect().size
var grid = get_node("GridContainer") # change name to your GridContainer
global_position = Vector2(
viewport_size.x / 2 - grid.rect_size.x / 4,
viewport_size.y / 2 - grid.rect_size.y / 4
)
pass
#Edit
Check Xrayez answer, that should work better for you. Using my way you could make it centered if you add stuff to GridContainer in real-time and its size will change.
Ok, this works if the root of my scene is a control node. I guess that was the problem (my root node was a Node2D and the GridContainer was being centered at the origin). Just adding this in case anyone else has the same problem. Thx!