Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

Cast UTextureRenderTarget2D to UTexture2D Failed


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

Product Version: UE 4.10
more ▼

asked Feb 17 '16 at 08:49 PM in C++ Programming

avatar image

6 2 3 5

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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!

more ▼

answered Aug 25 '16 at 12:08 AM

avatar image

132 12 10 22

avatar image HolyKaisar Aug 25 '16 at 05:12 AM

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.

avatar image mortmaire Aug 30 '16 at 08:06 AM

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).

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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.

more ▼

answered Aug 30 '16 at 08:20 AM

avatar image

179 10 18 26

avatar image mortmaire Aug 30 '16 at 11:51 AM

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

avatar image trojanfoe Aug 30 '16 at 12:32 PM

I don't see how that will work.

avatar image joeysnow Oct 13 '16 at 11:08 AM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question