Event Receive Draw Hud in hud BP gives you the current screen resolution.
Iâm trying to get the screen size/resolution of the game but neither ()->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 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++?
Yeah but as i understand, the event is only visible on blueprints but i want the solution in C++
Itâs not just visible in blueprint because that node is representation of C++ function and you can see how it get size here:
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
you can get to that function from UGameViewportClient then from Viewport varable there and you can access local GameViewportClient from ()->GetGameViewport()
Thanks for replying so fast, Iâve tried with the Viewpor GetSizeXY but it returns 0 for both components:
if (())
{
UGameViewportClient* Viewport = ()->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
#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!
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?
Seems like a bug. I just tried to package project and everything worked fine.
What part of code are you calling this at?
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
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?
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.
I am experiencing this bug as well. In standalone it just returns 0 for X and Y.
Same problem over here, using UE 4.7.2. Did you find a solution for this?
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.
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.
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;
}
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.
You should not use this code, prefer the one below, because it handle pointers protection. This solution may result in crashes
Great, thanks
Actually âResolutionâ is your monitor resolution, and âViewportâ is the size of game window.