How to add text to a RichTextLabel?

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

Greets!

Meaning, i got a text within a RichTextLabel, and i want to add one from another node, without replacing it.

:bust_in_silhouette: Reply From: njamster

If you aren’t using BBCodes

Attach this script to your RichTextLabel:

extends RichTextLabel

func add_text(text_to_add):
    text += text_to_add   # same as: text = text + text_to_add

If you are using BBCodes

Attach this script to your RichTextLabel:

extends RichTextLabel

func add_text(text_to_add):
    append_bbcode(text_to_add)

You then can call that function on any instance of that RichTextLabel.
If you don’t know what BBCodes are, take a look here.

Thxs but look my texts in area2D_entered:

func _on_Area2D_body_entered(_body):
	var roll = roll_a_dice(1, 100)
	print(roll)
	if roll > 1:
		birds.visible = true
		get_node("/root/Node2D/Stage/Player/Text").text = "A couple of Aelys playing in the air. A rare and good omen."
		GlobalP.morale += 5
	
	wildplain.visible = true
	PlainS.playing = true
	get_node("/root/Node2D/Stage/Player/Text").text = "A flowery plain with sweet fragrance."

Only the last one is displayed, and your script is attached to the RichTextLabel.

Sry, how do i call that function from inside that area2D_entered?

Syl | 2020-02-18 20:15

As I already wrote: just attaching the script won’t do it, you’d still needto call the function add_text. Like you call any other function in gdscript as well:

func _on_Area2D_body_entered(_body):
    var roll = roll_a_dice(1, 100)
    print(roll)
    if roll > 1:
        birds.visible = true
        get_node("/root/Node2D/Stage/Player/Text").text = "A couple of Aelys playing in the air. A rare and good omen."
        GlobalP.morale += 5

    wildplain.visible = true
    PlainS.playing = true
    get_node("/root/Node2D/Stage/Player/Text").text = "A flowery plain with sweet fragrance."

    get_node("/root/Node2D/Stage/Player/Text").add_text("This string will be appended to the existing text.")

njamster | 2020-02-18 22:07

That work great! Cheeers! :slight_smile:

Syl | 2020-02-18 23:02

Note that += will reassign the entire text, causing all the BBCode to be parsed again. This can be slow for long texts. Use append_bbcode() if you only need the newly-added text to be parsed (which is the case most of the time).

Calinou | 2020-02-19 08:57

Thxs for adding information. What you mean by ‘parsed’? ‘resetted’? And what is the BBCode?

Syl | 2020-02-19 13:30

Unlike a normal Label, a RichTextLabel allows for the use of so-called BBCodes. Click here for a tutorial. These codes will not be visible to the user, but instead will change the appearance of the text, e.g. underlining it. In order for that to work, the text has to be “parsed” first: the computer runs over the string, looks for such codes, removes them and renders the remaining text accordingly. While doing all of this again and again whenever you’re appending something to the text is possible, it’s not advised.

However, given that you haven’t heard of BBCode yet, this likely doesn’t affect you at all. If you’re using the text-property (like it did in my example) all of this does not apply. Should you find yourself in need of BBCodes, you will need bbcode_text instead and then - indeed - you would be better off using append_bbcode().

I’ll edit my answer to reflect this.

njamster | 2020-02-20 17:40

That’s really interesting, and it opens new possibilities for my texts with thoses bbcodes. Cheers!

Syl | 2020-02-20 18:53

Sry, but writing the bbcode script, i got this error: the method “append_bbcode” isn’t declared on base “string”.

Syl | 2020-02-20 19:05

Sorry, my bad! It isn’t bbcode_text.append_bbcode(), just append_bbcode(). I adjusted my example above.

njamster | 2020-02-20 19:30

Man, that works great! Colorful texts there will be from now on! :slight_smile:

Syl | 2020-02-20 19:44