0 votes

Greets!

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

in Engine by (321 points)

1 Answer

+1 vote
Best answer

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.

by (10,221 points)
edited by

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?

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.")

That work great! Cheeers! :)

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).

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

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.

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

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

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

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

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 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 webmaster@godotengine.org with your username.