I have a CameraComponent in ortho mode that I want to size to a specific height, regardless of aspect ratio. The only option is to set the OrthoWidth property, so I need to know the aspect ratio of the viewport. I have tried calling PlayerController->GetViewportSize(…) inside my GameMode’s StartPlay function, but that only works in editor mode. When run in standalone builds, I get back zero for both SizeX and SizeY.
I have looked at Tappy Chicken, which does something similar with an ortho camera, and I see in it’s Blueprint that it has a Delay node prior to it calling the GetViewportSize node. Is that delay in there to get it working in standalone builds?
Is there a proper way to get the viewport size? Is there an event/function I can override somewhere within my game code where I can be certain that the viewport size will be available? Or should I add in a latent delay within my code like Tappy Chicken (that seems very hacky to me)?
I can confirm that removing the hard-coded 0.2s delay in Tappy Chicken’s blueprint does mess up the calibration of the ortho camera. Can anyone explain why the viewport size is not available immediately in standalone mode, but is in PIE?
More importantly, is there a better function to override or event to respond to that will be executed after the viewport size is set, allowing you to avoid the hard-coded delay hack used in Tappy Chicken?
To answer your first question yes, the delay that you mentioned is there to allow time for the viewport to load before the call to get it’s size is made. If you are attempting to get the size on BeginPlay or StartPlay then this may cause issues as there is no way to force one object’s BeginPlay to occur before/after another. Because of this the delay will ensure that the viewport exists beforew attempting to access information about it.
is it planned to have another way to reliably get the viewport size in the future? I know there is a ViewportResizedEvent but this does not fire in PIE mode, so we anyway need to implement a delay to set the ortho width in 2D games. Do you have any other ideas or examples on how to achieve this (getting the correct viewport size) without using a delay?
Thank you very much. This bothers me since I started using the engine
It would still be great to have a better solution. Currently I have issues with popping visuals as the delay kicks the viewportResized calls after the delay. I can possibly work around it but it’s not elegant.
This gives me an accurate size in new editor window/ PIE, selected viewport mode etc and regardless of viewport setup in the editor. Also, it works for me in standalone game mode inside the editor at least. It also works as expected if you resize in windowed mode.