Scroll down Scroll Container AFTER filling child VBox container

Godot v4.2.1

extends CanvasLayer

signal finished_adding_texts

@onready var container = %TextContainer
@onready var scroll = %Scroll

const TEXT_CONTAINER = preload("res://_Scenes/Core/Balloon/history_text.tscn")

var history : Array = []

func _ready():
	process_mode = Node.PROCESS_MODE_ALWAYS
	get_tree().paused = true
	connect("finished_adding_texts", handle_scrollbar_changed)

func add_texts():
	if history.size() > 0:
		for i in history.size():
			print(str(i))
			var instance = TEXT_CONTAINER.instantiate()
			instance.text = history[i]
			container.add_child(instance)
		emit_signal("finished_adding_texts")
func set_texts(texts : Array):
	history = texts

func _process(_delta):
	if Input.is_action_pressed("esc"):
		get_tree().paused = false
		self.queue_free()

func _on_close_pressed():
	get_tree().paused = false
	self.queue_free()

var handle_scrollbar_changed = func():
	var scrollbar = scroll.get_v_scroll_bar()
	scrollbar.value = scrollbar.max_value

image

I’m making a visual novel, and every time a text passes it gets stored inside the history array. When I click a button a history panel opens, its a scene consisting of a scroll container, a vbox container and dynamically created labels inside the vbox container.
Everything works fine, but the thing is that when I open the history window the scrollbar stays at the start, and I want it to drop down to the last text added automatically. I want it down when the labels are all in place, and not everytime a label appears.
I tried with the code I attached and it just not works.

you mean the scroll container will scroll down to the bottom always whenever the history canvas is created?

if that’s the case, then just call the handle_scrollbar_changed whenever added text done?

I have placed the signal after adding each label and it still won’t work…


func add_texts():
	if history.size() > 0:
		for i in history.size():
			print(str(i))
			var instance = TEXT_CONTAINER.instantiate()
			instance.text = history[i]
			container.add_child(instance)
			emit_signal("finished_adding_texts")

this one, did it get accessed? try print() something there to confirm

change to
scroll.set_deferred("scroll_vertical", max_value)

image

Hmm, max value stays at 0 every time

so it could the whole texts is still inside the VBoxContainer size height

Try using ScrollContainer.ensure_control_visible() after adding the children to the VBoxContainer

For example:

func add_texts():
	if history.size() > 0:
		for i in history.size():
			print(str(i))
			var instance = TEXT_CONTAINER.instantiate()
			instance.text = history[i]
			container.add_child(instance)
		emit_signal("finished_adding_texts")
		await get_tree().process_frame
		scroll.ensure_control_visible(container.get_child(-1))

Didn’t work. It has to do with the max_value, it stays at 0 every time… let me check