How can I properly size these buttons so the aspect ratio of it is what I want?

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

I have a ScrollContainer with an HBoxContainer inside. On ready, I have a script which iterated through a folder and creates a button corresponding to each file in the folder. For each file (FileName) in the folder it runs the following code:

	var button =
    button.connect("pressed", self, "_which_button_pressed", [button])
    button.size_flags_vertical = 3
    button.rect_size.x = button.rect_size.y * 2

But the buttons are not coming out the way I want. They end up tall and skinny and ignore the

     button.rect_size.x = button.rect_size.y * 2

as if it is overriden by something. I can’t figure out how to do what I want, which is to have the button be exactly the height of the HBoxContainer it is inside of but stretch itself so that its aspect ratio is always 2:1. I must be missing something simple here. Thanks.

:bust_in_silhouette: Reply From: MitchReidNZ

Try setting the rect_min_size instead of rect_size

Pretty sure parent containers rewrite the rect_size however they think it should be, but will honour the rect_min_size

If the available size is greater than the rect_min_size I’m not sure how it’ll upscale things.

You could also try setting the rect_scale directly?

I solved it by simply setting the x value of the button’s rect_min_size to twice the value for the container’s rec_size.y. Thanks for telling me about the rect_min_size property as I had completely forgotten about it in this scenario.

fader | 2020-05-18 05:17