Scroll Container not working with buttons instantiated while the game is playing

Godot Version

v4.3

Question

Hello! This is my first time posting here so I apologize if I don't explain it well enough.

I am working on making a visual novel in Godot and when the player is given choices to select, instances of a button scene I made will be created and then added as children to the container node.

Here is the parent scene:


Within the ScrollContainer Node, there is a grid container that is another scene called ChoiceSelector:


And finally, the ChoiceButton scene:

This is the script attached to ChoiceSelector where ChoiceButton is instantiated

class_name ChoiceSelector
extends GridContainer

signal choice_made(target_id)

const CHOICE_BUTTON := preload('res://ChoiceButton.tscn')

func display(_decision: Decision) -> void:
	var numberOfOptions = 0
	for option in _decision.options:
		if ScenePlayer.choice_dictionary.has(option):
			if !PlayerStats.selectedChoices.has(option):
				numberOfOptions += 1
				var choiceInformation = ScenePlayer.choice_dictionary[option]
				var button = CHOICE_BUTTON.instantiate()
				button.new_choice_button(choiceInformation)
				button.choice_button_pressed.connect(_on_Button_pressed)
				add_child(button)

func _clear() -> void:
	for child in get_children():
		child.queue_free()

func _on_Button_pressed(target_id: String) -> void:
	_clear()
	choice_made.emit(target_id)

When playing the game, it looks like this:


And there is no way to scroll to show the fifth option.

I have tried various combinations of clicking Expand in the Container Sizing but so far, I haven’t found a combination that works for what I need to accomplish. Another thing to note is that if I manually add ChoiceButton nodes into ChoiceSelector, I will be able to scroll with the scrollbar.

Any insight is very much appreciated :slight_smile:

Make the Choice button the scene root, delete the Control node. Containers work best if all children are also containers, up until the last child.

3 Likes

If you are making a heavy UI game and at the start of it, I suggest you to use Godot 4.7. It has a lot of Container & UI improvements.

Only -dev4- version is available and it’s soon to be fully released.

1 Like

Thank you! I need to clean it up now to fix the spacing and everything but it is scrolling now!

Thanks for the advice! I will definitely look into upgrading my version then.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.