• Godot HelpGUI
  • Stretch TextureButton Width Based On Label Child (and all other buttons in container)?

I have a main menu. In this main menu, I have various containers, and my custom TextureButton scenes for, currently, Start Game and Quit Game buttons. This scene has a TextureButton as the root and a Label as it's child. However, when the Label gets longer than the TextureButton, the size of the button width does not increase. In contrast, using a standard Godot Button has this behaviour. Furthermore, a Godot Button will stretch the width of all other Controls - including my custom TextureButton scenes - to match it's width.

I want this exact behaviour. I have provided images to illustrate what currently happens and what I wish to happen. I'm sure there's a very simple solution but I've been at this for about 5 hours and haven't gotten anywhere. Setting the button's rect size manually from within GDScript does absolutely nothing, same with setting the minimum size.

Cheers.

Current Button Behaviour

Desired Button Behaviour

Well, the reason it probably is not working is because the Label is a child node of the TextureButton. Because of how Godot is setup, with all nodes being mostly independent of each other, the TextureButton node does not resize to fit the children nodes.

The simplest way to fix the issue is to scale the buttons in the editor so the button is the proper size. If possible, that is probably your best bet.

You could try something like this to work around the issue using GDScript (untested):

tool # makes the script run in the editor
extends TextureButton

var label_node = null

func _ready():
	# This may need changing so the name of the node is the name of the label
	label_node = get_node("Label")
	label_node.connect("resized", self, "on_label_resized")

func on_label_resized():
	if (label_node.rect_size.x > self.rect_size.x):
		self.rect_size.x = label_node.rect_size.x
	if (label_node.rect_size.y > self.rect_size.y):
		self.rect_size.y = label_node.rect_size.y

Which should hopefully resize the TextureButton correctly both in-game and in the editor when the script is attached to the TextureButton node. Keep in mind that this script does not account for the Label's position, just it's scale.

Hopefully this helps! (Side note: welcome to the forums)

Instead of using a TextureButton, you could use a standard Button with a custom StyleBoxTexture attached. One benefit of this is that you can use nine-patch textures, which avoids visible stretching on the button's edges as it gets wider/taller.

To create a theme that will affect the child nodes automatically, create a Theme resource in the root Control node. See GUI skinning in the documentation for details.

Thanks for the responses! I ended up just using a Button with a theme and setting the images for each state. That gave me my desired outcome!

3 years later