Loading textures at runtime

Not sure what the best approach for this would be. I currently load a bunch of textures in a constructor:

ConstructorHelpers::FObjectFinder<UTexture2D> MyObj(*(pathName));
TextureArr[i] = MyObj.Object;

and even though this method works, I have found that it is getting bloated and it would also be loading a bunch of other textures I wouldn’t actually be using at that point in the game. Now what I need to do is have the same approach to loading really, but in another function, such as a LoadTextures( int TextureSetToLoad ) and then when that is called, it would load up and set some textures.

So is there any way to do this, or is there any way to create another constructor somehow, which I could pass my TextureSetToLoad into, as ConstructorHelpers doesn’t seem to work outside of the per-generated constructors.

I would recommend using a TAssetPtr, and then loading them on demand at runtime. Here’s a doc explaining the basics of how you would do this:

https://docs.unrealengine.com/latest/INT/Programming/Assets/AsyncLoading/index.html

Is this the correct approach I need to use then to just load in textures and nothing else? I made an attempt with that documentation but found I kept running into errors and such, esepctially when I needed to declare what class the objects where and such, as they are only textures (Assuming UTexture2D would be the class?).

After having a fresh mind and another look around this morning, I found this, which I had a play around with and got working in a rather simple fashion. I am now able to load textures from path locations at run time by concatenating some strings with variables to create my paths. Only problem is, my textures have to be in a certain folder structure but it’s not a huge issue. Basic code is:

UTexture2D* MyTextureLoader::LoadTextureFromPath(const FString& Path)
{
	if (Path.IsEmpty()) return NULL;

	return Cast<UTexture2D>(StaticLoadObject(UTexture2D::StaticClass(), NULL, *(Path)));
}

FString PathToLoad = "/Game/Textures/YourStructureHere";
UTexture2D* tmpTexture = LoadTextureFromPath(PathToLoad);

Yeah, it’s a UTexture2D.

I also tried using TAssetPtr to try and get from a path to an asset to an instance of it as UTexture2D* but failed miserably. Ben, can you post some example code?

For now I’m just going with what Dune has.

Important caveat to anyone looking to use this approach:

Loading an asset from code in this fashion will not create a reference to it when dependencies are gathered at cook time. Meaning that once you cook your project, assets that are exclusively loaded in this fashion will not be included in your cooked build and will fail to load.

As described in the question, using ConstructorHelpers::FObjectFinder to assign to a hard pointer property will create a reference and the asset will be gathered. However, as mentioned, it has the downside that the asset will always be loaded along the object.

As per Ben’s answer, TAssetPtr is really what you want to use. It will create an assignable property and will be properly referenced, but is not loaded automatically.

For future reference, also have a great Blueprintable function for this in the forum : The solution is in this post: (39) 's Extra Blueprint Nodes for You as a Plugin, No C++ Required! - Blueprint - Epic Developer Community Forums

I’m trying to set material onto object at run-time from texture, normal map, displacement map etc. located in the hard disk. To achieve this i need to create dynamic material instance and set the parameter to the material by loading the image file at run-time.

I’m creating material from texture & displacement map image file. These image files are loading from folder using Victory Plugin’s “Victory Load Texture 2D from File” Node.

I’m creating the material instance dynamic of the “test material”. Now I’m setting the texture and displacement parameter as shown in figure 4.
After creating material instance dynamic, I’m setting this material to the object.

figure[1] test material:

figure[2] texture:

figure[3] displacement map:

figure[4] Method:

Now here is the problem.

When I directly create material in material editor from texture and displacement map and set it onto object. It doesn’t generate** moiré pattern.**

But when I run-time set the material onto object using figure [4]. It will generate moiré pattern.

Here are the sample screen shots of both.

figure[5]: Material Editor result:

figure[6]: Runtime result: Imgur: The magic of the Internet

I think the “Load Texture 2D from File” nodes is not calculating the Mip Map correctly.

I tried with following nodes but the result is same.
1> “Victory Load Texture 2D from File” (Created by )
2> “Load Texture 2D from File by Extension” (Created by you)
3> “Victory Load Texture 2D from File Pixels” (Created by )

I hope you will take this request into consideration. I would like to thank you in advance for your time and attention in this matter.
Should you have any further questions or concerns, please do not hesitate to contact me.

Regards,

I recommend you to reference your asset with

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Item) TAssetPtr<UTexture> Thumbnail;

in your class.
And after return it as

UTexture* UCommonLibrary::ReturnThumbnailFunction()
{
		auto thumbnail = Thumbnail.LoadSynchronous();
                 return thumbnail ;
}

Hi, did you get it to work? Sitting with these as we speak