Hi dears.

I wrote a scene with:

Panel
  > ScrollContainer
    > HBoxContainer
      > TextureRect1
      > TextureRect2
      > TextureRect3
      > TextureRect4

I want to set by GDScript the Scroll position, so I used this code:

$ScrollContainer.scroll_horizontal = 200

This code is accepting values from 0 to 100, if I try to use any higher value don't works, eg.:

$ScrollContainer.scroll_horizontal = 200
print($ScrollContainer.scroll_horizontal)

The scroll moves just a little bit and displays 100. :-(

Could you please kindly help me?

Welcome to the forums @marcio_pessoa!

What is the size of the HBoxContainer node? If the horizontal size of the HBoxContainer node is 100 or less, the scroll container will not allow you to scroll farther than the minimum size of its children nodes, so that could be causing the issue.

Hi @TwistedTwigleg . Thank you for the welcome and thanks for the help.

I'm learning Godot for just few days, I'm very happy, it's awesome!

The HBoxContainer width is 1080, so I belive this is not the problem.

I wrote an small version, could you please kindly check what is my code mistake?

I would like to keep the scroll position when I back to panel scene.

Thanks a lot!

I tested the project, and yeah, setting scroll_horizontal is not acting like I would expect. It seems that no matter what value you set it to in _ready, it doesn't apply. I think this is a bug, but I do not have enough experience with the ScrollContainer node to say for sure. At very least, it is not how I would expect the node to behave.

However, I found out how you can fix the issue! Just add yield(get_tree(), "idle_frame") to _ready before any of the other code, and it works as expected. Here's the code I used in _ready:

func _ready():
	yield(get_tree(), "idle_frame");
	
	print("Read value:", Global.scroll)
	print("Previous scroll:", scroll_horizontal)
	scroll_horizontal = Global.scroll
	print("After scroll:", scroll_horizontal)

With Global.scroll set to 500, it yields the expected result. Knowing this, I'm wondering if the issue with setting the horizontal_scroll in _ready is that, without a yield, the ScrollContainer is not fully initialized and therefore overrides any attempt to set the horizontal scroll property. Regardless, hopefully adding a yield statement should fix the issue! :smile:

Thank you very much for your effort!

I am very happy with this solution. =)

Now, I'm going to learn more about yield. It seems to be very powerful.

Thanks a lot!

2 years later