x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How to get current screen size/resolution?

I'm trying to get the screen size/resolution of the game but neither GetWorld()->GameViewport->GetViewportSize(...) or PlayerController->GetViewportSize(...) work when launching the game on standalone , I've rechecked that i have a HUD class on my GameMode.

Also the tutorial posted by rama isn't of very much help to me as it lists all supported resolutions but i just want to know the current resolution that's being used. How could i get the current resolution via C++?

Product Version: Not Selected
Tags:
more ▼

asked Sep 07 '14 at 11:11 PM in C++ Programming

avatar image

DylanVelez
198 38 36 43

avatar image Jacky Sep 07 '14 at 11:16 PM

Event Receive Draw Hud in hud BP gives you the current screen resolution.

avatar image DylanVelez Sep 07 '14 at 11:42 PM

Yeah but as i understand, the event is only visible on blueprints but i want the solution in C++

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

3 answers: sort voted first

Viewport Size is NOT the same as Resolution

They Are Two Separate Metrics

Viewport Size relates to how much screen space the game occupies. For example, you can play a game with a resolution of 1280x720 on a 2560x1440 monitor.

When playing fullscreen for this example, you will have:

ViewportSize=2560x1440
Resolution=1280x720

This is important because (at least one my machine) UMG, text, UI elements, etc are displayed using the Viewport Size and not the Resolution in order to remain clear and crisp when playing lower-resolution games on larger monitors.

Get Viewport Size / Get Resolution

 FVector2D GetGameViewportSize()
 {
     FVector2D Result = FVector2D( 1, 1 );
 
     if ( GEngine && GEngine->GameViewport )
     {
         GEngine->GameViewport->GetViewportSize( /*out*/Result );
     }
 
     return Result;
 }
 
 FVector2D GetGameResolution()
 {
     FVector2D Result = FVector2D( 1, 1 );
 
     Result.X = GSystemResolution.ResX;
     Result.Y = GSystemResolution.ResY;
 
     return Result;
 }
more ▼

answered Jan 23 '16 at 01:16 PM

avatar image

Yukova
136 1 58 11

avatar image TheGreatCabbage Apr 15 '17 at 04:19 PM

Great, thanks :)

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

How to Get Viewport Size and Center From Anywhere

Here's how you can get the viewport size and center from anywhere in your code base!

    //Viewport Size
     const FVector2D ViewportSize = FVector2D(GEngine->GameViewport->Viewport->GetSizeXY());
 
     //Viewport Center!            
     const FVector2D  ViewportCenter =  FVector2D(ViewportSize.X/2, ViewportSize.Y/2);


Engine.h not EngineMinimal

To be able to use the above code replace this in YourProject.h

 #include "EngineMinimal.h"

with

 #include "Engine.h"


Enjoy!

Rama

more ▼

answered Sep 08 '14 at 12:25 AM

avatar image

Rama
10.6k 446 346 1088

avatar image DylanVelez Sep 08 '14 at 12:38 AM

Thanks, it works (even the above solution works) but only when i play on the editor, if i launch the game or play in standalone mode the viewport size is 0 for X and Y, any idea as to why is that?

avatar image BarfedBean Mar 07 '17 at 05:59 PM

You should not use this code, prefer the one below, because it handle pointers protection. This solution may result in crashes

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

It's not just visible in blueprint because that node is representation of C++ function and you can see how it get size here:

https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AHUD/DrawHUD/index.html

 void AHUD::DrawHUD()
 {
     HitBoxMap.Empty();
     HitBoxHits.Empty();
     if ( bShowOverlays && (PlayerOwner != NULL) )
     {
         FVector ViewPoint;
         FRotator ViewRotation;
         PlayerOwner->GetPlayerViewPoint(ViewPoint, ViewRotation);
         DrawActorOverlays(ViewPoint, ViewRotation);
     }
  
     // Blueprint draw
     ReceiveDrawHUD(Canvas->SizeX, Canvas->SizeY);
 }

But this return size of HUD canvas which does not need to be screen size, in engine rendering field is called viewport, so logicly you should serch by this term

https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/FViewport/GetSizeXY/index.html

you can get to that function from UGameViewportClient then from Viewport varable there and you can access local GameViewportClient from GetWorld()->GetGameViewport()

more ▼

answered Sep 08 '14 at 12:02 AM

avatar image

Shadowriver
37.2k 936 172 1116

avatar image DylanVelez Sep 08 '14 at 12:16 AM

Thanks for replying so fast, I've tried with the Viewpor GetSizeXY but it returns 0 for both components:

 if (GetWorld())
 {
     UGameViewportClient* Viewport = GetWorld()->GetGameViewport();
     FIntPoint ViewSize = Viewport->Viewport->GetSizeXY();
     if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Black, FString::Printf(TEXT("Size X = %i , Size Y = %i"), ViewSize.X, ViewSize.Y));
 }


The code just prints Size X = 0 and Size Y = 0

avatar image TimGS Sep 08 '14 at 03:43 AM

Seems like a bug. I just tried to package project and everything worked fine.

What part of code are you calling this at?

avatar image DylanVelez Sep 08 '14 at 03:56 AM

From GameMode OnBeginPlay() I'm spawning a Custom class which manages the logic for my game, after i spawn the class i call a method from which i'm trying the above code. It just works inside the editor, in standalone mode or when i launch it, size X and size Y are 0

avatar image TimGS Sep 08 '14 at 01:38 PM

I use this at the same place. Did you try this in blank project to see if this is something project-specific or engine-wide?

avatar image DylanVelez Sep 08 '14 at 08:16 PM

Well yeah i created another project and it still fails, i think this might have to be related to the latest version of the engine, I'll leave it as it is for now until another version comes out.

avatar image trutty Mar 10 '15 at 12:13 PM

Same problem over here, using UE 4.7.2. Did you find a solution for this?

avatar image simmania Feb 04 '15 at 08:39 AM

I am experiencing this bug as well. In standalone it just returns 0 for X and Y.

avatar image Ghar May 15 '15 at 02:41 PM

I don't know if this will work for you in a code project, but with blueprints when I call the node GetViewportSize in an OnBeginPlay event it return zero, unless I add a delay 0.2s node before it. I was having the same problem, then checked the Tappy Chicken where I know they use this node.

avatar image trutty Jun 04 '15 at 02:38 PM

Yeah, I did the same thing after I saw it in the Blueprint of the official the TappyChicken project. Isn't very beautiful but well it works.

avatar image florin.covit Mar 14 '16 at 03:17 PM

A bit late to the "party" but hopefully this info will help someone out there. It's not necessarily a bug when trying to get the viewport size function and it sometimes returns 0 in a standalone mode. It also happens in UE 4.10.4. Well, I don't think the way it behaves is because of a good code design decision either, but after hitting my head on it a few times I figured that the correct way to handle it is to wait for the "ViewportResizedEvent" event found in the "FViewport" class to be raised before reading the viewport size. In editor mode it doesn't happen probably because the viewport is already created and its size is already known initially.

So if you're working from code, you will have to bind yourself to this event and only after it's raised you must attempt to get the viewport size. You can bind yourself for example like this:

 GEngine->GameViewport->Viewport->ViewportResizedEvent.AddUObject(yourCallbackObj, yourCallbackFunction);

where "yourCallbackFunction" is a function from the "yourCallbackObj" which is one of your own UObject type. Of course this is just one way to bind to that event. Here you can read about other ways that may suite your needs: Multi-Cast Delegates

One other useful way to bind to the "FViewport::ViewportResizedEvent" is to use the: "AddUFunction" that will allow you to bind a Blueprint function to the event:

 GEngine->GameViewport->Viewport->ViewportResizedEvent.AddUFunction(callbackBlueprintObj, bpFunctionName);

where "bpFunctionName" is a string with the name of the blueprint function from the "callbackBlueprintObj".

Making this a static Blueprint Library function can allow you to handle this event in Blueprints by any of your custom Blueprint classes where you specify the name of the target function by string.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question