GameViewport->GetViewportSize() result being unstable?

Hello, Unreal gurus,

I wrote some basic C++ callbacks to help my development.
But some weird thing I discover:

On Android platform, the value of these call:
GEngine->GameViewport->GetViewportSize( );
could be (0,0) when the game is just started.

This is triggered by applying a default hud, which call these function early when game mode is set.

Is there a better approach to obtain viewport size?

Thank you.

Yeah you can’t rely on this being true all the time.

The best way, if you’re using it to apply something to a HUD - is to get it from the Local Player.

Hi, TheJamsh

I really appreciate your input, but allow me to ask for more details.

I just modified the HUD blueprint to receive a updated size from the default pawn class blueprint,
where I believe it is what do you meant by “the local player.”

The HUD received the right input size in PIE, but not in physical Android device again.
( still (0,0) vector from “get view size” in pawn blueprint sent to HUD.)

Is this what you meant by “the local player”? Or it is somewhere else?
Please allow me to ask for you help again. Thank you.

Not quite, the ‘Local Player’ is a UObject that is a representation of the player. As a hierarchy, it looks like this:


You still won’t be able to set the HUD size until the Viewport is valid though, but it’s a safer place to access it from IMO.