How to change sprite / text using a button

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

I want if a certain button is pressed that the text changes on label / richtextlabel / button, or sprite on sprite / texturerect

:bust_in_silhouette: Reply From: njamster

Connect the pressed-signal of your button to a function looking like this:

func _on_Button_pressed():
    $Label.text = "NewText"
    $RichTextLabel.text = "NewText"
    $RichTextLabel.bbcode_text = "NewText"
    $Button.text = "NewText"

    $Sprite.texture = load("res://icon.png")
    $TextureRect.texture = load("res://icon.png")

The node-paths might look different depending on the location of (a) your Button and (b) the Label/RichtTextLabel/Button or Sprite/TextureRect you want to change. In the example I made the (rather unrealistic) assumption that all nodes a click on the Button changes are direct children of the Button-node and their default names are kept.

Thanks you!!

start123 | 2020-05-11 14:28

Is it possible to write text in two strings in this way?
Is it possible to write a var value?

start123 | 2020-05-12 12:27

Is it possible to write text in two strings in this way?

$Label.text = "New" + "Text"

Is it possible to write a var value?

var test = "NewText"
$Label.text = test

If the variable’s value is not a string, you have to cast the value using str():

var test = 1
$Label.text = str(test)

njamster | 2020-05-12 12:50

Is it possible write text in two paragraphs in this way?

start123 | 2020-05-12 14:56

$Label.text = "Line1\nLine2D"

The \n is a control character indicate a linebreak. It will be replaced during parsing.

njamster | 2020-05-12 16:16

I tried your advice and wrote the command:

$richtextlabel.text = "10/n5/n2"

As a result, I get: 10 / n5 / n2

start123 | 2020-05-13 12:50

It’s \n, not /n - a backslash.

njamster | 2020-05-13 12:52

Ah, got it…

start123 | 2020-05-13 12:54