I have a question about DesiredSize.
I’m going to dynamically generate and put something like TextBlock and Image into a specific container like HorizontalBox in Game RunTime. Using C++.
The flow of code is as follows:
- UserWidget is present.
- AddToViewport.
- SetVisibility is not Hidden.
- UserWidget’s CanvasPanel has a HorizontalBox.
- Create a TextBlock and addToChild to the Box.
- After adding, we will use the GetDesiredSize function in the Box to obtain the size and use this value to move it to a specific location.
The first problem is that immediately after adding, the GetDesiredSize function has a value of 0.
Looking at the engine, GetDesiredSize found that the value was cached after the tick. In addition to this method, it says that I can call the ForceLayoutPrepass function.
I added the ForceLayoutPrepass function to the box and then called it, and after that, I tried to get a size value as the DesiredSize function, but it was still zero.
And I got the DesiredSize of Box from UserWidget’s Tick function, but it was still zero, and only after a few tags.(Of course, the time for this tick is less than 0.1 seconds, which is quite small.)
So I was able to create a Timer and get a DesiredSize in 0.1 seconds, and then I was able to solve the problem of using Size to position Translation.
There’s something I don’t understand.
And there’s something I want to do.
-
I don’t understand why DesiredSize has a value of 0 even though I called the ForceLayoutPrepass function after AddToChild to Box.
-
DesiredSize isn’t available, so I don’t think it’s a good logic to hold this position on the Tick. Is there any way to get the size of the box right after AddToChild?