Blueprint input node not making it to c++

So I have this blueprint, with a custom node named Output. I’m trying to get the viewport size into it. None of the code examples have worked so far, so I thought I’d try inputting it from the editor.

The two print strings there output the right results. I also have a log call in the blueprint code itself that immediately outputs the FVector2D that it gets passed. But by that stage it’s always zero.

Is this a known issue? What’s up?



    UFUNCTION(
        BlueprintPure,
        meta = (
            DisplayName = "Get texture result",
            CompactNodeTitle = "Output",
            Keywords = "output"
        ),
        Category = Game
    )
    static UTexture2D* Output(APlayerCameraManager* Camera, FVector2D ViewportSize);




UTexture2D* UOutput::Output(APlayerCameraManager* Camera, FVector2D ViewportSize)
{
    UE_LOG(LogTemp, Warning, TEXT("Viewport is %d x %d"), ViewportSize.X, ViewportSize.Y);
    return Texture;
}


It’s not my %d doing something funny and giving me zero, is it? I definitely get a crash when I try to instantiate the texture with those sizes after I round them up to a power of two.

FVector2D is two floats, so yes it is exactly your %d giving you 0, great intuition on your part :slight_smile:

Try %f instead!


**Const Reference**

Btw you might want to make your input const reference for efficiency. :)



```


UTexture2D* UOutput::Output(APlayerCameraManager* Camera, const FVector2D& ViewportSize)
{


```



Pure C++ Way To Get Viewport Center and Size

Here’s two of my static C++ function library nodes for you!



static FORCEINLINE FVector2D **GetScreenCenter**()
{
	if(!GEngine->GameViewport)
	{
		return FVector2D::ZeroVector;
	}
	//Viewport Size
	const FVector2D ViewportSize = FVector2D(GEngine->GameViewport->Viewport->GetSizeXY());
	return FVector2D(ViewportSize.X/2, ViewportSize.Y/2);
}
static FORCEINLINE FVector2D **GetViewportSize**()
{
	if(!GEngine->GameViewport)
	{
		return FVector2D::ZeroVector;
	}
	//Viewport Size
	return GEngine->GameViewport->Viewport->GetSizeXY();
}


Keep in mind they dont have to be static if you just want to stick them in one of your classes somewheres, but I prefer to make a static C++ function library to use common functions throughout my UE4 C++ code bases.


**Wiki On C++ Static Function Libraries**

https://wiki.unrealengine.com/Static_Function_Libraries,_Your_Own_Version_of_UE4_C%2B%2B,_No_Engine_Compile_Times

Enjoy!

:slight_smile:

Rama

Man, why do we care about strict types when we’re just trying to parse something into a log string? It’d be super cool if it just handled it and had one injection symbol.

Thanks Rama! :slight_smile:

What’s the deal with const? I expect the value to change occasionally, will const prevent that?

The const is not declaring that the viewport can never change in size, it is simply saying that for the duration of that function (very very small amount of time), that you are not going to change the inputted value.

It’s basically the same as copying the FVector2D as you did in original version, but more efficient.

If you need to change the value of the viewportsize within your function that’s a different story but it didn’t look like you were doing that

:slight_smile:

Rama

Gotcha! I’ll switch to doing that, thanks :slight_smile:

Hee hee!

Have fun today!

:slight_smile:

Rama