How do I remove the last line from a Rich Text Label?

Godot Version

4.2

Question

I am trying to remove the last two appended lines of my Rich Text Label on a button push. Some forums suggested remove_line(), but that doesn’t seem to be a valid function. I tried remove_paragraph(), but I can’t get it to remove the correct number of lines.

Could you explain a little more about the problem scope? What are the lines? What does the button do? Are you trying to remove Nodes or String text?

If you are handling strings, you could search for second case of the new line character “\n”, then substring up to that point.

var first_n: int = $RichTextLabel.text.find("\n")
assert(first_n != -1, "Couldn't find first line")
var second_n: int = $RichTextLabel.text.find("\n", first_n+1)
assert(second_n != -1, "Couldn't find second line")

$RichTextLabel.text = $RichTextLabel.text.substr(second_n+1)
1 Like

Sure. The lines are fairly complicated, but it’s all just string text formatted with bbcode. I am running a bunch of if statements and appending text if certain conditions are met.

An example:
if base_node.random_numbers[6] > 0.5:
if base_node.lhs_op.visible == true:
scroll_text.append_text(“base_node.const.text”)

One line consists of maybe 10-15 of these statements. All I want the button to do is to act as an undo button of sorts. I just need it to remove the last two lines that were added this way.

Thanks so much for your reply! I will try your suggestion in the morning and let you know if I can get it to work. I am still kind of new with Godot, so I will need to make sense of some of these functions.