How does RichTextLabel scrolling work?

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

I am working on a small roguelike and I needed a message box. So I made a blank project and experimented with the controls. I quickly found out that the default scrolling made it so there was always a blank line at the bottom of the textbox which I found ugly.

So I made my own code to try to scroll so that the RichTextLabel was always full of text (giving players the ability to see the most possible). I could hack it together if I knew the max number of lines the text box could hold.

I found it was really hard to figure out how many lines a RichTextLabel could hold. I even tried to write a function that would use get_visible_line_count to figure out when the box ran out of space, but get_visible_line_count() is not synced with get_line_count in any way I can figure out. But then I got it to work but I don’t understand why, scroll_to_line(line_num) is not working as expected!

Code:

extends RichTextLabel

func _ready():
pass # Replace with function body.

func _on_Button_add_message(message):
var line = (get_line_count() - 1)
print(“I have " + String(get_line_count()) + " lines.”)
print("Scrolling to line: " + String(line))
print(“I have : " + String(get_visible_line_count()) + " visible lines.”)
scroll_to_line(line)
add_text(message)
newline()

A button passed a message with a counter when I press it. Now this is the confusing part:
Image: TextBox.png - Google Drive
(I can’t figure out how this thing enters images… but it is important to see)
enter image description here
It works, but it really should not, and I wonder if I’m missing something obvious? I don’t have to actually know where I want to scroll to, since it isn’t scrolling to the line it says it is. The function description in the documentation reads: “Scrolls the window’s top line to match (variable) line”. I’m glad it isn’t doing that, but… what is it doing?
Sorry if this is obvious, I’m a novice.