Why is GetViewportSize() returning zeroed vector when launching in "Standalone Game" mode?

Hi :slight_smile:

I have a very hard time getting GetViewportSize() function to work. I am creating a Slate widget in certain location that depends on the size of the Viewport.

Here is my code:

   FVector2D ViewportSize;

	if(GEngine) GEngine->GameViewport->GetViewportSize(ViewportSize);

	OwnerHUD = InArgs._OwnerHUD;

			+ SOverlay::Slot()
			.Padding(FMargin(ViewportSize.X * 0.1f, ViewportSize.Y * 0.6f, 0, 0))
				.ShadowOffset(FIntPoint(-1, 1))
				.Font(FSlateFontInfo("Veranda", 16)) //Probably doesn't work
				.Text(FText::FromString("Hello, Slate!"))

When I launch the game in “Standalone Game” mode then ViewportSize is zeroed and it looks like this:

But when i launch the game in any other mode(“Selected Viewport (Active)” or “New Editor Window”) it works perfectly fine, ViewportSize contains proper size and it looks like it supposed to be:

Why is that so? Why GetViewportSize() function is not working in “Standalone Game” mode?

Hi Ogniok,

This line .Padding(FMargin(ViewportSize.X * 0.1f, ViewportSize.Y * 0.6f, 0, 0)) is only going to be evaluated once; this happens when your widget is being constructed. Chances are, ViewportSize is already set to something in the editor, but is 0 in the standalone game at the time your widget is being constructed. One solution would be to make a dynamic binding. You can do that like this:

 .Padding( this, &MyClassName::GetWidgetPadding )
 // Later implement the function as
 FMargin GetWidgetPadding()
           return FMargin(ViewportSize.X * 0.1f, ViewportSize.Y * 0.6f, 0, 0);
           return FMargin(0); // Cannot determine Margin without engine.

Now the .Padding will be re-evaluated every time it is needed.

However, I find the whole approach somewhat problematic. Could you explain what you are trying to accomplish and what the motivation is? I can suggest the best way to execute using Slate.


My goal was to create that GUI:

But i didn’t find any other way to reposition the texts, so I used Padding.

My code: [http://pastebin.com/ZPCnrENq][2]

I think you could be attempting to achieve a couple things there. Let me see if I can address them:

  1. My guess is you are trying to align things to the bottom of the screen. A lot of layout panels support .VAlign(VAlign_Bottom) to express that notion. You could then offset your content by using .Padding().

  2. You are trying to account for both layout re-flow and scaling. Layout re-flow occurs when the shape of your viewport changes. Your UI should respond but re-arranging itself to make best use of the available space. Scaling occurs when you increase the size of your viewport proportionally, and everything you should bigger uniformly. Typically, both happen when you resize your game’s window. You can control scaling by wrapping all your UI in an SDPIScaler widget. You can control layout re-flow by using the set of provided layout panels.

For examples of Slate usage, try running the SlateViewer project and use the WidgetReflector window to inspect various UI elements. You will be able to see where they are declared in the code and use that code as a reference. Note that you can always get at the widget reflector during the game by hitting ~ to bring down the console and typing WidgetReflector.

Hope this helps.

I’ll look into this. Thanks for help. :smiley: