ScrollContainer.follow_focus behavior

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

I am using a scrollcontainer with one vbox child. The vbox child has multiple label nodes.
This is what the tree looks like,

  • ScrollContainer (follow_focus property is set)
    • VBoxContainer
      • Label1 (added at runtime)
      • Label2 (added at runtime)
      • Label3 (added at runtime)

Here is a link to a small scene illustrating the problem:

When I add Label3 to the tree, I expect the scrollcontainer to scroll to the bottom.
But the behavior is very random. Sometimes it scrolls to the bottom, sometimes it doesn’t.

This is the code I have,

func create_label_with_message(message: String):
    var lbl: Label = Label.new()
    lbl.text = message
    lbl.autowrap_mode = TextServer.AUTOWRAP_WORD
    lbl.mouse_filter = Control.MOUSE_FILTER_IGNORE
    lbl.focus_mode = Control.FOCUS_ALL
    add_child(lbl)
    await get_tree().process_frame
    scroll_container.ensure_control_visible(lbl) # (mentioned in the docs for Scrollcontainer)

After 3 hours of banging my head against the wall, the following magical incantation works,

func create_label_with_message(message: String):
    var lbl: Label = Label.new()
    lbl.text = message
    lbl.autowrap_mode = TextServer.AUTOWRAP_WORD
    lbl.mouse_filter = Control.MOUSE_FILTER_IGNORE
    lbl.focus_mode = Control.FOCUS_ALL
    add_child(lbl)
    await get_tree().process_frame
    await get_tree().process_frame # (this is the only extra line added)
    scroll_container.ensure_control_visible(lbl) # (mentioned in the docs for Scrollcontainer)

The only thing I changed was I added await get_tree().process_frame twice.
Can someone please tell me what might be going on?
I’m trying to understand why adding the line await get_tree().process_frame twice makes it work.

I am trying to convince my boss at work to use godot for an internal project.
And this makes me wonder what other hidden “gotchas” there are.

Phew, it’s such a relief to say that after a considerable amount of time and effort, I have, at long last, come up with a viable solution.

jayjay | 2023-06-02 00:02

You should post the solution lol

ShatteredReality | 2023-06-06 17:12