Viewport/Canvas returns wrong size at first frame.

I’m making my custom HUD class and I came across a bug.

At first frame Viewport (PlayerController->GetViewportSize(Width, Height)) and Canvas (Canvas->SizeX, Canvas->SizeY) return wrong size.

void AMyHUD::DrawHUD()
{
	Super::DrawHUD();
	if (!Canvas) return;
	if (bFirstTick)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::FromInt(Canvas->SizeX) + ", " + FString::FromInt(Canvas->SizeY));
		bFirstTick = false;
	}
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::FromInt(Canvas->SizeX) + ", " + FString::FromInt(Canvas->SizeY));
}

When playing in new window it returns 1294, 754 in first frame which is wrong.

In next frames it returns 1280, 720 which is good.

It looks like it returns window size with borders in the first frame.

Hi Harsay,

Could you provide some more information about this issue? I tried to reproduce it and was unable to see the results that you described.

  • What version of the Engine are you using?
  • Are you using the binary version installed by the Launcher, or did you build the Engine from source code?
  • Do you only see this in your own project, or are you able to reproduce it in a new project as well?
  • Are you seeing this happen in a packaged project?

I’d like to chip in on this one, I’m seeing similar oddities early on with viewport that clear up later.

For me it happens whenever I run mobile preview or standalone mode but not in editor mode. To repro, try some code like this:

if (APlayerController* playerCtrl = GEngine->GetFirstLocalPlayerController(GWorld))
	{
		if (ULocalPlayer* localPlayer = (ULocalPlayer*)playerCtrl->Player)
		{
			if (UGameViewportClient* viewPort = localPlayer->ViewportClient)
			{
				// Screen position
				FIntPoint vpSize = viewPort->Viewport->GetSizeXY();

Attach that code to the BeginPlay event of a custom GameState object (this is my case it may well happen in the level blueprint) you will find that when this is called through BeginPlay the values in vpSize will be zero, I would have thought the viewport should be certified as valid before we hit any gamey code like BeginPlay.

However! The plot thickens in that if I was to add a delay node between my BeginPlay node and the node that uses the viewport everything works fine, even if the delay is zero! Very odd! Sound like there’s a potential race condition with the setting of the viewport or the delay node just forcibly invalidates its state, either way perhaps a one worth checking out!

Perhaps the delay node hack is a OK enough work around for now if others are experiencing this?

I set up a custom GameState class containing the code you provided and set a custom GameMode to use it. When I tried to reproduce the issue you described, it did not appear that the BeginPlay function was being called. I set a breakpoint and it was never triggered. Could you provide some more information for how you are setting up your GameState class with its BeginPlay function?

Hi everyone,

We have not heard back from you for a few days. Do you still need help with this issue? I will be marking this issue as resolved for tracking purposes, but please feel free to re-open this issue at any time if you need any more help.

Hi,

I just ran across this issue in 4.15. I’ve attached a test project that prints out the viewport size in the begin play event for the level blueprint and also whenever the space button is pressed. If you open up TestMap and play in a new editor window, you should see that the viewport size is different when called from the begin play event compared to the space bar event. For me, I get 1290x754 from the begin play event and 1280x720 from the space bar event. It seems that when playing in a new editor window, the viewport size for the first frame includes the borders of the window. Subsequent frames do not.

The viewport size for the first frame varies based on how the game is launched.

  1. Play in selected viewport - correct size (1280x720)
  2. Mobile preview - 0 size (0x0)
  3. New editor window - includes borders (1290x754)
  4. Standalone game - 0 size (0x0)
  5. Launch to mobile device - correct size

I’m currently getting around this issue by adding a small delay to functions I call in begin play. It works, but it’s clunky.

Hi ,

Thank you for the additional information that you provided. This is an issue that was reported in UE-10555. This issue has a somewhat lower priority to be fixed, mainly because it is relatively simple to work around the issue.