• Godot Help
  • How to change the width of a button with code in Godot 4

Hi everyone

My game has something like a file explorer for saved files. It uses code to make a button for every saved file in user://

I'd like to use code to set the width of each button. So far, the width just varies depending on the length of the name of the saved filename.

I've tried

button.set("layout/transform/size.x", 340)
This does nothing, although it doesn't produce an error message.

I've also tried

button.rect_min_size = Vector2(340, 65)  and 
button.rect_size = Vector2(340, 65)

These produce this error message:
Invalid set index 'rect_min_size' (on base: 'Button') with value of type 'Vector2'.

Does anyone know the correct way to do it?

Thanks

    OofStatement This does nothing, although it doesn't produce an error message.

    If the property does not exist or the given value's type doesn't match, nothing happens.

    https://docs.godotengine.org/en/4.2/classes/class_object.html#class-object-method-set

    Where are you getting those property names from?

    You might try custom_minimum_size, which is a Vector2.
    https://docs.godotengine.org/en/4.2/classes/class_control.html#class-control-property-custom-minimum-size

      DaveTheCoder

      Thanks. It works now. I found one of the "rect" ones on Reddit and the other was a ChatGPT answer.

      The other one I guessed based on what worked when I changed the font size of a label.