The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+2 votes

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

Godot version 3.3
in Engine by (942 points)
edited by

1 Answer

+1 vote
Best answer

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.

by (810 points)
selected by

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

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"

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.

-> 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
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.