[0.17.76] Scrolling functions don't work when elements are yet to be displayed/updated
Posted: Mon Nov 11, 2019 3:47 pm
The scrolling functions (scroll_to_top, scroll_to_bottom ...) do nothing when the element (a scroll-pane or a textbox) isn't shown/updated yet, that is to say when the function is called in the same tick as when its contents/text is set.
In this exemple, we create a string with a lot of lines and display it in a textbox with a fixed height, causing a scrollbar to appear. If we want the scrollbar to be at the top immediately, we cannot use the following command:
The position of the scrollbar is set to its default, which is at the bottom.
However doing it in two commands work:
This also affects scroll-panes, but their default position is at the top:
I am making a mod where I need to dynamically change the content of a textbox with a fixed height, and I cannot use those functions in the same tick as when I update the GUI. The only reliable workaround I have found is to schedule the scrolling action in the next tick, but without using events as they are executed in the same tick as I schedule them.
In this exemple, we create a string with a lot of lines and display it in a textbox with a fixed height, causing a scrollbar to appear. If we want the scrollbar to be at the top immediately, we cannot use the following command:
Code: Select all
/c
local bigText = ""
for i = 1, 100 do
bigText = bigText .. "\n"
end
bigText = bigText .. "A"
local textbox = game.player.gui.center.add{type="text-box", name="test", text=""}
textbox.style.maximal_height = 800
textbox.text = bigText
textbox.scroll_to_top()
However doing it in two commands work:
Code: Select all
/c
local bigText = ""
for i = 1, 100 do
bigText = bigText .. "\n"
end
bigText = bigText .. "A"
local textbox = game.player.gui.center.add{type="text-box", name="test", text=""}
textbox.style.maximal_height = 800
textbox.text = bigText
Code: Select all
/c game.player.gui.center["test"].scroll_to_top()
Code: Select all
/c
local bigText = ""
for i = 1, 100 do
bigText = bigText .. "\n"
end
bigText = bigText .. "A"
local scroll = game.player.gui.center.add{type="scroll-pane", name="scroll"}
local textbox = scroll.add{type = "text-box", name = "test", text = bigText}
scroll.scroll_to_bottom()