Buttons going off position in containers when changing screen size

Godot Version

Godot Version 4.4.1

Question

Hi,

I’m making an inventory panel. My base resolution is low at 320x180. When I scale the resolution up to 1920x1080, the buttons bunch up and have some funky positioning.

Here’s my scene tree and how the anchors are positioned.

This is what I get at the base resolution when I fill it with items:

That’s what I want to achieve but when I increase the window size this happens:

One solution is to set the button to expand vertically. Then, to address the awkward space,

making a bunch of dummy nodes until it reaches the right amount of buttons to display things properly. After that, adjusting them to flat and disabling them. It achieves what I’m looking for, though it makes a scroll bar that’s not needed at the base resolution.

All good.

Is this the best solution to this problem?


Seem like your buttons just collapse on each other, try adjusting their custom_minimum_size property.

This is my quick setup I did, it seems to work fine with any resolution and any amount of buttons. I didn’t even need to adjust the custom_minimum_size property.


The Buttons expand only horizontally, not vertically.

I think it’s got something to do with the labels on my button. It has two labels on either side of it.

I did what you showed me and it does work fine.

When I use the button that I made then I run in to the problem. This is what my button node looks like.

I’m going to mess with the anchors and see if changes anything.

Did you try adjusting the custom_minimum_size? (especially y (height) component is important).
Without any text, the Button’s size will collapse. It doesn’t matter that the Button has children, it’s not a Container, so it ignores children. That’s why you need the custom_minimum_size.

I recreated your setup, below are the screenshots:

Button

HBoxContainer

Label1

Label2

Hey I see it is indeed collapsing. At a higher resolution I had to set it to about 45 pixels in the y component to display normally (I’ll show the pictures of before and after).

Before

After

When scaling the resolution back down though, the button takes up an awkward amount of space.

Do you think the best fix here is to scale the minimum size down with some code?

You can make your own custom Container for the Item so that it adjusts its own size according to the children’s size.

I think I’m close to getting it.

Here’s my code for the custom container:

@tool

extends Container

func _notification(what):
	if what == NOTIFICATION_SORT_CHILDREN:
		var button = get_child(0)
		var label = button.get_child(0)
		size.y = label.size.y
		fit_child_in_rect(button, Rect2(Vector2.ZERO, size))

The container’s set to expand horizontally in the editor. Then I’m setting the height of the container to the height of the label. I fit the button to the container’s size. It works fine when growing the window, but I see through print statements when I shrink it back down the y-size of the label doesn’t change so the button has an inappropriate height. I’m not too sure why.

Large window size

Small window size

When I get rid of size.y = label.size.y, the label’s y-size grows and shrinks like normal. I can’t think why that’s getting in the way of it shrinking.

Making item buttons like this, though, works fine. It doesn’t feel as clean but it gets what I’m going for. I put everything underneath a label.

Do you have any suggestions I can try for the custom container?

I honestly don’t understand what the problem is now, so if you still need some support there, you’d need to try rephrasing it, because you lost me, sorry :slight_smile:

Just one think comes to mind that you can try to see if the NOTIFICATION_SORT_CHILDREN fires with a print() statement when you shrink your container.

That’s ok. There were other things I wasn’t sure how to do with the container that were looking weird, like items clipping in to each other vertically. I set a label as the parent node instead of a button and it sizes things correctly!

1 Like