morphogen
(scottnla)
March 27, 2023, 12:49am
1
I’m trying to create a Texture2D
from a file path in my C++ file, and I’m receiving errors that it cannot do so.
The code:
FString initialBackgroundPath =
FPaths::ProjectContentDir() + "Core/screens/" + FString::FromInt(screenIndex) + "/main/background_0.png";
UE_LOG(LogTemp, Log, TEXT("File path: %s"), *initialBackgroundPath);
UTexture2D* initialBackgroundTexture = LoadObject<UTexture2D>(nullptr, *initialBackgroundPath);
This gives me the error:
LogUObjectGlobals: Warning: Failed to find object 'Texture2D None.D:/MyProject/Content/Core/screens/1/main/background_0.png'
I went ahead and checked in my File Explorer that this file exists, and the path is 100% correct – given that the path is correct, I’m actually kind of lost as to what else could be wrong here! It’s a pretty simple function call but the error is surprisingly mysteryious.
Any suggestions for what else could be wrong or how else to troubleshoot?
Hi scottnla1,
The LoadObject and FindObject are for loading “uasset” files, the native UE files.
Check out this thread about loading pngs:
As the question says, can i dynamically load a PNG image, for example, and create a Texture2D from it?
I’m trying to create an game launcher with covers and i need this for loading the cover images.
See ya!
morphogen
(scottnla)
March 27, 2023, 1:06am
3
How should I be using LoadObject if I have the correct uasset?
I actually tried creating a .uasset:
And changing the C++ code to reference the .uasset file instead:
FString initialBackgroundPath =
FPaths::ProjectContentDir() + "Core/screens/" + FString::FromInt(screenIndex) + "/main/T_background_0.uasset";
UE_LOG(LogTemp, Log, TEXT("File path: %s"), *initialBackgroundPath);
UTexture2D* initialBackgroundTexture = LoadObject<UTexture2D>(nullptr, *initialBackgroundPath);
But this gives me the same error:
LogUObjectGlobals: Warning: Failed to find object 'Texture2D None.D:/MyProject/Content/Core/screens/1/main/T_background_0.uasset'
You’re very close, just change the filename part to “T_background_0.T_background_0” - confusing I know, but the first one is for the “Package” (the uasset file) and the second one is for the asset contained in the package…
1 Like
morphogen
(scottnla)
March 27, 2023, 1:58am
5
I tried that and still get errors:
LogUObjectGlobals: Warning: LoadPackage can't find package Game/Core/screens/1/main/T_background_0.
LogUObjectGlobals: Warning: LoadPackage can't find package Game/Core/screens/1/main/T_background_0.
LogUObjectGlobals: Warning: Failed to find object 'Texture2D Game/Core/screens/1/main/T_background_0.T_background_0'
I created the assets by dragging and dropping them into the Content Drawer – could that be why maybe they aren’t structured properly?
Dragging them in should be ok as long as there’s no import errors.
You won’t need the “ProjectContentDir()” - replace that with “/Game/” and it should work.
1 Like
morphogen
(scottnla)
March 27, 2023, 2:25am
7
That was it! Thank you so much!
1 Like