multiple lines in button

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

Hi!
I just have a string

var name = "dangerous danger"

And I want it to be a name of button such that dangerous be the in the first line and danger in the second line

:bust_in_silhouette: Reply From: wyattb

Add a Label node to your Button and then in your button _ready() script set it like this:

func _ready() -> void:
	$Label.text="dangerous\ndanger"
	pass # Replace with function body.

Note the \n escape character in the text. Or simply edit the text of Label if you want to hard code it.

I don’t want to use any label node. Is there any option like autowrap for button same as we have in label

Help me please | 2021-06-15 10:54

I don’t think there is anything like that for Button. Any reason why you don’t want to add Label node? You could add the Label node at runtime so it wouldn’t even be part of the project tree.

E.g.

# Called when the Button node enters the scene tree for the first time.
func _ready() -> void:
	var label: Label=Label.new()
	label.name="Danger"
	add_child(label)
	$Danger.text="danger\ndanger"

wyattb | 2021-06-15 12:17

Its because actually I have four buttons within an HBoxContainer. So will adding label will work there?

And why have you written -> void do this have any specific reason.

Help me please | 2021-06-15 13:43

→ void is optional. with gdscript but the template adds it when you create a new script. So I leave it alone.

Yes will work. As long as you keep the script on the Buttons. You can also customize the label text for each button E.g.

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	var label: Label=Label.new()
	label.name="Danger"
	add_child(label)
	match name:
		"Button": 
			$Danger.text="%s\ndanger" % name
		"Button2": 
			$Danger.text="%s\ndanger2" % name
		"Button3": 
			$Danger.text="%s\ndanger3" % name
		"Button4": 
			$Danger.text="%s\ndanger4" % name

wyattb | 2021-06-15 15:02