Cast UTextureRenderTarget2D to UTexture2D Failed

Hi,

I am trying to cast a UTextureRenderTarget2D object to a UTexture2D using UTexture2D obj = Cast(UTextureRenderTarget2DObj); in UE 4.10.0. The obj here is always a NULL. But if I want to cast a UTextureRenderTarget2D obj to a UTexture obj it works because UTexture is the parent class of UTextureREnderTarget2D. Does anybody here has this problem before?

PS: An example:
In A.h file:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "RealSenseDelegator")
	UTextureRenderTarget2D* RenderTarget;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "RealSenseDelegator")
	UTexture2D* Test;

In A.cpp file:

Test = Cast<UTexture2D>(RenderTarget);

And Test is a NULL;

Thanks,
ZH

I’ll answer because I had same problem just a hour ago - actually we can first cast UTextureRenderTarget2D pointer to UTexture, and then UTexture to UTexture2D. Here is some code I used:

UTexture *auxTex = static_cast<UTexture*>(PortalView);
UTexture2D *auxTex2 = static_cast<UTexture2D*>(auxTex);

After this I am using auxTex2 in UMaterialInstanceDynamic::SetTextureParameterValue and it works!

Interesting. Why do we need to convert it to UTexture Pointer first? I think I saw one example that convert a rendertarget pointer to a utexture2d pointer, but that is not the case to me. But this works for me. So, thank you so much.

Because UTextureRenderTarget2D inherits from UTexture, same as UTexture2D. First you cast UTextureRenderTarget2D pointer to his more general class object, and after this you cast general class to other inherited class. (Sorry, I don’t know proper nomenclature).

I believe you should be calling UTextureRenderTarget2D::ConstructTexture2D() to create a UTexture2D from a UTextureRenderTarget2D; you cannot cast it, as has been suggested, as there is no relationship between the two classes, despite them sharing a common ancestor.

I don’t see how that will work.

But I use it in my project. I have to refer to RenderTarget from material which gets only UTexture2D pointer as input.

I use this get a texture2D,but it’s black,how to get the right texture2D

As @trojanfoe mentioned in their answer, UTextureRenderTarget2D and UTexture2D are unrelated types, in spite of them sharing a common ancestor. Using static_cast to cast between unrelated types will lead to crashes. I have an example of this situation here.

I believe the reason your static_casts above worked is that you are actually casting with the following sequence: UTextureRenderTarget2D => UTexture => UTexture2D (invalid cast) => UTexture (implicit, due to the fact SetTextureParameterValue takes a UTexture). You could get rid of auxTex2 and just pass auxTex to SetTextureParameterValue. In fact, you can also get rid of auxTex and simply pass PortalView (assuming it is a UTextureRenderTarget2D) to SetTextureParameterValue.

If that does not work, it’s likely that you have not included the header file for UTextureRenderTarget2D, which means the compiler would have no idea it derived from UTexture. This can happen if you only have a forward declaration of UTextureRenderTarget2D, which would look like class UTextureRenderTarget2D;.

@trojanfoe is right. @mortmaire, see my reply to your question to see why your technique, though wrong, appeared to work for you.