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++?
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, 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?
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
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 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.
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.
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:
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:
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.