How can I replace a specific line in a RichTextLabel while preserving its BBCode?

Godot Version

Godot 4.8.dev4

Question

I have a RichTextLabel with a text that’s built using append_text() with BBCode.

I want to replace a specific line of the existing content, for example:

Line 1
Line 2
Line 3

I want to replace Line 2 with something else, while keeping all the BBCode formatting of the rest of the RichTextLabel intact.

The problem is that get_parsed_text() only gives me the plain text, and text doesn’t contain the content added through append_text().

Is there a proper way to replace a specific line in a RichTextLabel without having to maintain a separate copy of the entire BBCode text?

I’m currently trying to make something like:

replace_line(2, "New line")

while preserving the existing formatting/content of every other line.

Afaik, there’s no way to fetch the internal representation in a form of complete bbcode string. You’ll have to maintain your own string.

Cant you do it by using a string variable instead of text?

For example if you have an array or other collection of possible strings and a var for each line, then in your richtextlabel, just use variables like line_1, line_2 instead of plain strings. When needed, just change the value of line_2. That should keep the bbcode intact while the string gets replaced. Then just update the label to get the new value displayed.