Announcement

Collapse
No announcement yet.

Viewport Size returning 0

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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

    #2
    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.

    Comment


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

      Comment


        #4
        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.
        Click image for larger version

Name:	PlayerPawnFindCenterX.PNG
Views:	1
Size:	79.8 KB
ID:	1074692

        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.
        Click image for larger version

Name:	PlayerController.PNG
Views:	1
Size:	120.2 KB
ID:	1074693

        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.

        Comment


          #5
          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.

          Comment


            #6
            I am having this same issue. Also, there is no other get viewport size available. I am trying to use the get inside of a construction script on my PC in every kind of preview. Nothing is working.

            Comment


              #7
              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.

              Comment


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

                Comment


                  #9
                  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!?

                  Click image for larger version

Name:	bd34fd853447101e5a2d242dfc4073d1.png
Views:	1
Size:	87.3 KB
ID:	1097022

                  Comment


                    #10
                    Hm.. Solution from this question helps, but.. it's still looks like a bug)

                    Comment


                      #11
                      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.

                      Comment

                      Working...
                      X