C++ Asset Texture2D -> Blueprint

In Blueprint I know I can do something like this to load an image, for example:


and it works without any problem.

The problem is that I’m not interested in it for “n” reasons that are beyond the scope of this post.

In my Code I have something like this:

   const FString initialBackgroundPath = FPaths::ProjectDir() + TEXT("Content/...") +Obj->GetIconName();

   if (FPaths::FileExists(initialBackgroundPath)) {
     TSoftObjectPtr<UTexture2D> Icon = TSoftObjectPtr<UTexture2D>(FSoftObjectPath(initialBackgroundPath));
     Obj->SetIcon(Icon);
   } else {
     UE_LOG(LogTemp, Warning, TEXT("File does not exist: %s"), *initialBackgroundPath);
     Obj->SetIcon(NULL);
   }

The debugger tells me “nullptr”, so that seems to be the problem here

There may be a better way to do it in C++ or something and I don’t know about it.
Yes I have thought about Using UImage. But first I prefer to solve it by Texture2D.

Does the code I provided correctly pick up the image or does it really have to be done another way?

Note: The format is .png

If I understand the question correctly, you are looking to recreate in cpp what the “Import File as Texture 2D” is doing in bp? If that’s the case have you looked at what that bp node is doing in cpp? That is where I would start.

From a pure coding perspective, something seems off about TSoftObjectPtr<UTexture2D> Icon = TSoftObjectPtr<UTexture2D> FSoftObjectPath(initialBackgroundPath);

You’re constructing an FSoftObjectPath and attempting to cast it to a TSoftObjectPtr of type UTexture2D? I believe you’re missing an intermediate step which actually loads the object.

All of that being said, check this post out. This might be what you’re looking for Declaring a texture variable - Programming & Scripting / C++ - Epic Developer Community Forums (unrealengine.com)

you are looking to recreate in cpp what the “Import File as Texture 2D” is doing in bp?

It’s similar, but I “think” it’s not the same. Although Both load the texture.

I have tried what you told me:

   UTexture2D* icon;
   FString IconPath = FString::Printf(TEXT("Texture2D'/Game/.../%s'"), *Obj->GetIconName());
   ConstructorHelpers::FObjectFinder<UTexture2D> Icon(*IconPath);

   if (Icon.Object != NULL) {
     icon = Icon.Object;
   }

Exception: Exception 0x80000003 encountered at address 0x7ffc10abe022

My function must be called from a method, from nowhere else.Internally, I don’t know what calls he’s calling. I also don’t know if it needs to be placed somewhere specific so that it doesn’t give an error.

On the other hand,

You’re constructing an FSoftObjectPath and attempting to cast it to a TSoftObjectPtr of type UTexture2D? I believe you’re missing an intermediate step which actually loads the object.

Correct me, but according to the documentation:

TSoftObjectPtr | Unreal Engine 5.2 Documentation

It has a constructor to pass FSoftObjectPath
TSoftObjectPtr(FSoftObjectPath ObjectPath)
It’s just the one I’m following.








Finally I decided to do it differently than I was doing it. I decided to look at the “Import file as 2D texture” node and use it.
I have used it:

const FString initialBackgroundPath =   FPaths::ProjectDir() + TEXT("Content/.../") +Obj->GetIconName();

Obj->SetIcon(UKismetRenderingLibrary::ImportFileAsTexture2D(ObjectContext,initialBackgroundPath));

And it has worked.
The problem with doing it like this (with the signature of a higher level and not with the implementation itself) is that if this node is never modified for the UE7.45 version I will have to modify it by hand.

Therefore, at this point where the documentation no longer helps at all, I have decided to directly use the native implementation of this “Import File as Texture 2D” for my case.
I don’t know if I’m doing it right. But it works.

I hear ya, however, you could take that approach with all of the other helper functions available within the engine and you’d be down to writing everything from scratch. This seems like an innocuous enough of a function that, even if they adjust the internals a bit, the ins and outs will likely remain the same for backwards compatibility. You also have the added benefit that they are on tap to maintain it.

That being said, I understand the desire to see something through. Especially when it should be “simple”. Have you looked at the actual code for ImportFileAsTexture2D? I would be curious as to how they are doing what you want to do. Should be able to search it up within VS.

Nice work, moving on!

1 Like