+1 vote

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.

in Engine by (559 points)

2 Answers

+5 votes
Best answer

In order to center your GridContainer relative to the viewport:

  1. Select your GridContainer node.
  2. Press Layout button (resides on the toolbar above scene's viewport)
  3. From the popup menu, select Center

If you want your TextureButtons to be centered relative to GridContainer as well:

  1. Select your TextureButton node.
  2. In the inspector, go to Control -> Size flags.
  3. Enable Horizontal Expand and Horizontal Shrink Center.

I recommend going through this tutorial, it helped me a lot:

Design interfaces with the Control nodes

by (1,348 points)
selected by

Yeah... I went way too far by using script.

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!

0 votes

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


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.

by (378 points)
edited by
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.