Current DPI Scaling

Hi everyone!
I have LOTS of fun with UMG and I have implemented my own drag&drop. Only problem that I have is getting DPI Scaling current factor since I need proper one for my mouse position. Is there a way to get it or should I implement default DPI scaling curve ? cheerio!

I needed to set an actor on top of some of my UMG widgets so I was facing the same issues since apparently we can’t get widget world location. And so whenever the screen resolution changed, my actor was not at the relative location of my widget buttons like I wanted it. So I turned to the DPI curve, and like you, couldn’t find a way to actually retrieve it using blueprints. So instead I found a way to fake it and get the desired result, although it feels a bit hack-ish, it works well.

Ok, so below I show my DPI curve in the interface settings. Using that information, I built a similar curve in a timeline. The top value is the value that tries to match the viewport size. So I have the same keyframs then in my DPI curve using the screen resolution as values. Below I have another value which aims to find the DPI scale that we need to set our actors according to screen rez. Again I used the same values then in my DPI curve i.e. 0,44444 - 0,666666 - 1.0
What is important here is that both values match in time, so that the keyframe with value 480 happens at the same time as the keyframe with the value 0.44444. Originally I had tried to set the time line to be pretty short so as not to lose to much time calculating. But this made the results innacurate as it was too frame dependant. So I stretched my 3 first keyframes to 4 seconds. The last keyframe 8 I put because it was on my DPI curve but my game doesn’t use platforms that have that kind of resolution so I ignored much past 4 seconds.

So once I start the timeline, I update a compare float with my top value called DPI with the viewport size y. When the timeline finds a match (or close) it tells the timeline to stop, which leaves me with the bottom value called Scale which ends up being my DPI scale. In all trials the difference was always .1 and less of the actual value. So it isn’t 100% precise, but pretty close.

With all that said, hopefully an easier way will be available in future updates. And if anyone has a better way to do it, awesome!

Just in case someone is looking for a way, to get current dpi scaling in C++, here it is:

#include "Runtime/Engine/Classes/Engine/UserInterfaceSettings.h"
#include "Runtime/Engine/Classes/Engine/RendererSettings.h"

const FVector2D viewportSize = FVector2D(GEngine->GameViewport->Viewport->GetSizeXY());
const float viewportScale = GetDefault<UUserInterfaceSettings>(UUserInterfaceSettings::StaticClass())->GetDPIScaleBasedOnSize(FIntPoint(viewportSize.X, viewportSize.Y));
1 Like

I wanted to post an up-to-date answer for getting the UMG DPI Scale at runtime. This is current to UE 4.15:

// this function requires the UserInterfaceSettings header to be included
#include Runtime/Engine/Classes/Engine/UserInterfaceSettings.h

// this function can be marked as Blueprint Pure in its declaration, as it simply returns a float
float MyBPFL::GetUMG_DPI_Scale() {
  // need a variable here to pass to the GetViewportSize function
  FVector2D viewportSize;
  // as this function returns through the parameter, we just need to call it by passing in our FVector2D variable
  GEngine->GameViewport->GetViewportSize(viewportSize);

 // we need to floor the float values of the viewport size so we can pass those into the GetDPIScaleBasedOnSize function
  int32 X = FGenericPlatformMath::FloorToInt(viewportSize.X);
  int32 Y = FGenericPlatformMath::FloorToInt(viewportSize.Y);

  // the GetDPIScaleBasedOnSize function takes an FIntPoint, so we construct one out of the floored floats of the viewport
  // the fuction returns a float, so we can return the value out of our function here
  return GetDefault<UUserInterfaceSettings>(UUserInterfaceSettings::StaticClass())->GetDPIScaleBasedOnSize(FIntPoint(X,Y));
}

I also posted this as a gist on GitHub, which is linked on my studio’s blog here: [][1]

(Shout out to [Elringus][2], as they were on the right track; the API has just changed)

[1]:
[2]: Changing package directories - C++ - Epic Developer Community Forums

2 Likes

Thanks Elringus, it helped!

Thanks!!!

Thank you

Now we can use the method UWidgetLayoutLibrary::GetViewportScale to get the scale value.