[Widget] Canvas slot desired size incorrect

Having an interesting problem with widget re-positioning.

I have a widget that needs to dynamically grow in size and be positioned at construction time after it’s size increase. Having a container on a canvas allows you to “Size to content” which allows a widget to disregard it’s size instead getting the desired size, but this doesn’t seem to take effect right away. Which is a problem.

Here’s an example widget that I created to demonstrate this:

Here I have a simple CanvasPanel that contains a VerticalBox called container which is set to “Size to Content”

I created a separate widget “WBP_SimpleImage” which contains nothing but a blank image core widget, this was just so I could have something quick to toss inside of the example widget’s VerticalBox. I’m adding 10 of them as children, after which, I print the VerticalBox’s Size as well as Desired Size. Without that delay (arbitrary number) the results are incorrect.

131510-p3.png

You’ll notice the VerticalBox size is correct in both cases (100, 30) but the DesiredSize is incorrect until it hits that delay.

For my purposes, when I re-position the widget it doesn’t work due to the DesiredSize not being available on the loop’s completion. With the delay there, the widget appears at 0,0 for a fraction of a second before properly being positioned which is just wrong.

What are my options to fix this glitch?

So, since DesiredSize requires at least one pre-pass to have completed for any valid results to appear, I decided to tie a one time call into Tick that checks if the VerticalBox has any children, when it does fire the re-positioning call.

It’s not as ideal as I had hoped, but until I get a better solution, this will have to do. More insight into this is welcome.

Using UE5 Early Access this issue is still apparently in the engine. This can easily be seen by making a new empty user widget which has a canvas panel in it automatically. Then just add a second canvas panel under it … the second canvas panel gets this mysterious (100x30) offset that you can’t make go away and the runtime sizing is also wrong because of it.

You can force a pre-pass on that widget if you want to work on it’s size without waiting as well.
UWidget::ForceLayoutPrepass | Unreal Engine Documentation