Get Viewport Size directly into FVector2D. C++

This is more of a general coding thing.

I’m trying to store the result of GetViewportSize() into an FVector2D like this.

FVector2d Local_ViewportSize;
GetViewportSize(Local_ViewportSize.X, Local_ViewportSize_Y);

This does not work since the function returns int32 and the FVector2D is double.
So I tried this

FVector2d Local_ViewportSize;
GetViewportSize((int32)Local_ViewportSize.X, (int32)Local_ViewportSize_Y);

But I get this error:

Non-const lvalue reference to type int32 cannot bind to rvalue of type int32

I tried a few things and I got it working like this.

INT32 Local_ViewportSize_X;
INT32 Local_ViewportSize_Y;
GetViewportSize(Local_ViewportSize_X, Local_ViewportSize_Y);
FVector2d Local_ViewportSize = FVector2d(Local_ViewportSize_X, Local_ViewportSize_Y);

But that sorta defeats the purpose. Do you know how to make this into a neat little block of code?

GetViewportSize expects an FVector2D itself, not it’s components.

this works for me

FVector2d Local_ViewportSize;
GetGameViewportClient()->GetViewportSize(Local_ViewportSize);

Also, shouldn’t it be

Local_ViewportSize.Y

instead of

Local_ViewportSize_Y
1 Like

Yes you are right about the Local_ViewportSize_Y, I just copy pasted it wrong when writing the message.
That way you are doing is using the GetGameViewportClient do you know how to do this while using the PlayerController->GetViewportSize(...)?
Honestly I’m just curious on how to get a int32 transformed to double.

Parameters are passed by reference there’s no way it could work with float/double as int32.

FVector is simply an instantiation of TVector with the explicit type double :

image

So you could kinda gain some lines in the following way :

UE::Math::TVector2<int32> Vec;
GetViewportSize(Vec.X, Vec.Y);

However be aware that this is NOT an FVector2D, it is a 2d vector of int32, they are not directly interchangeable/convertible, and most common vector operations will probably not work correctly with int32 operators. But if you just need the variables then it may work. It’s essentially equivalent to this :

int32 SizeX, SizeY;
GetViewportSize(SizeX, SizeY);
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.