How to scroll a dynamic list of images

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

I need to create a dynamic list of images and be able to scroll them.
This is my scene:
—> HBoxContainer
------> TextureRect1
------> TextureRect2
------> TextureRect3
------> TextureRect4

It works, but I need the list to be dynamic.

I tried to turn off the visibility of TextureRext4 with this GDScript code: $ScrollContainer/HBoxContainer/TextureRect4.visible = false.
The fourth image disappears but the size of the HBoxContainer still has room for 4 images and not 3.

I tried to resize de HBoxContainer ($ScrollContainer/HBoxContainer.rect_size.x = …) in according to the number of images but it doesn’t work… when I scroll the images I still have the empty space for the fourth image at the end.

How can I do in order to achieve the result I need?
Thank you for your help.

:bust_in_silhouette: Reply From: jgodfrey

I’m not entirely sure what you want to happen here. As a test, I replicated your scene tree and adjusted my ScrollContainer width to display 3 of my 4 TextureRects. With that setup, I have a horizontal scrollbar that will let me scroll to see the 4th TextureRect.

Additionally, I added a button to the scene that, when pressed, toggles the visibility of the 4th TextureRect.

The first press of the button hides the 4th item which, in turn, causes the scrollbar of the ScrollContainer to disappear as it’s not needed to display the 3 visible items.

Pressing the button again makes the 4th item visible, and the scrollbar returns as it’s now needed to see the 4th item.

It almost sounds like you want want the geometry (size) of the ScrollContainer to change to fit its contents, but then you’d never need to “scroll”.

Generally, you’d want the container to stay the size you’ve set it and for it to either have (or not have) a scrollbar to see its full contents. In my above experiment, that’s exactly how it works.

Thank you for your reply.

The problem is: if you enable the touch input option (the two Touch input options in Project Settings — Input Devices — Pointing), you will see that, even if you toggled the visibility of the 4th TextureRect and the scrollbar of the ScrollContainer disappeared, the Container still has room for the 4th TextureRect and you can scroll to it.

I just want the scroll to be limited to the visible TextureRect.

I don’t know if this is the right way to do that, I just need to scroll a list of items but the number of items is dynamic.

make | 2020-08-28 01:38