Viewport Size returning 0

I’m trying to get the x,y of the viewport on my phone screen so that I can tell which section of the screen is being touched by the player. I would like to split the screen in half so that I can do something different if I touch the left half of the screen vs the right half.

I have tried using Get Viewport Size for both Viewport and HUD (Using blueprints). Both of these work if I’m using the mouse on my computer, but if I launch (or build and install) the game on my android phone “Get Viewport Size” always returns 0 for both x and y. I can capture the touch input and tell the location on my phone, but I want to be able to have it be half the screen regardless of which phone is being used. Am I missing something super simple?

Thanks!
Snackmix

For viewport to return correct values you need umg hud (even empty one) assigned to active player controller.
Also make sure you connect “get player controller” to get viewport so it has info from active player controller.

Ps. that may be player pawn instead of controller.

Thanks! I will try this as soon as I get home from work!

Sorry I never got back sooner some real life stuff came up. I have tried what you told me to do and have been toying around with it and I’m still unable to successfully get it to return anything but zero.

This is my method on my Player Character(Pawn) for determining the viewport size.

I’m finding half of the x just so I can split the screen in half.

This is my Player Controller where I create the UMG Widget. Add the Widget to the Viewport. Then Case to my character to get the screen size from the viewport.

Everything works correctly on my computer and will return me the correct sizes I expect. I still get 0s when trying to launch this on my phone and find the center of the screen. Did I misunderstand what you mean, or am I still missing something? Thanks a lot for your help.

That get viewport size i think you are using wrong one.
Correct one has blue input for player controller reference.

Now some more tips:
Calculate screen location in kind of percents. I take shorter screen dimension as -100% to +100%. Then calculate longer dimension as for eg. -170% to +170%.
Yes that is why i wrote “kind of percents”. But calculating it all in “percents” makes my later calculations for touch input independent from resolution.

Also you can make “Get Middle Screen X” pure function, i love those pure functions. :wink:

during construction time not every blueprint is activated. IT may happen that construction script of bp you are trying to get viewport size is executed before hud construction script. So no wonder that it is zero. Use OnBegin play.

Using GetViewportSize with Target Player Controller still returns 0 on android, I think this is a bug.

Hello guys, I faced the same issue. I have a HUD class and how you can see I trying get size on begin play event. In editor it works as expected, but on mobile device it returns 0. Dear Epic Games, when you will confirm this bug!?

Hm… Solution from this questionhelps, but… it’s still looks like a bug)

That is because “Event beginPlay” suffers from same problem as constructor scripts.
When Begin Play is executed it does not guarantee that everything else is loaded into level (just as constructor script). That is why 0.2 seconds delay fixes it.
Best would be if Epic fixed that begin play event, but i think it is not that simple.
Instead of 0.2 seconds delay it is better to set timer for 0.2 if viewport size is 0, wait for timed event, and keep doing it until is not zero.

1 Like

We can bind a function to the ViewportResizedEvent. GEngine->GameViewport->Viewport->ViewportResizedEvent.AddUObject(this, &ThisClass::OnViewportResized); In OnViewportResized we can get the viewport size and unbind from ViewportResizedEvent.

1 Like