Variable created in widget reads zero when called from HUD

In a widget WBP_UI, I create a Vector2D variable UI_ViewportXYSize to hold my viewport size:

Using the PrintString I know that the UI_ViewportXYSize correctly stores the viewport’s X and Y sizes.

However, when I create and add the WBP_UI widget to the viewport from my HUD, its debug PrintString shows me that the UI_ViewportXYSize is now (0,0) or in fact whatever the default value of UI_ViewportXYSize is set to in WBP_UI.

I’d be grateful if someone could point out what I’m doing wrong.

You added a delay to Construct, but you didn’t add it to BeginPlay, so you don’t have time to save the value to a variable before you try to get it.

You need to add a delay after AddToViewport or (better) make a Delegate in the widget that will notify when the value is valid.

1 Like

Thanks, that worked! I used the delay method. I’ll have to learn about how to use delegates but will give that a go as well.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.