How to access an asset in the content browser in C++?

I’m trying to use SetMaterial() to… well, set an actor’s material. But I want to set it to a file I can only access through the content browser. How can I do this?

Thanks in advance!

In my personal project, I use the LoadObject template function to load a UMaterialInstanceConstant object from the content browser; providing the directory + file name in question into that function. I am still learning C++ so I cannot fully explain how the function works, but I am also linking to documentation for this function and to another thread that might be helpful.

Make sure to always check if the returned object is valid before trying to use it in order to avoid any crashes. Good luck :slight_smile:

UMaterialInstanceConstant* MI_SkySphere = LoadObject<UMaterialInstanceConstant>(nullptr, TEXT("/Game/Hero/WeatherSystem/Materials/Sky/MI_SkySphere.MI_SkySphere"));

if (MI_SkySphere)
{
	SphereMaterial = SkySphere->CreateDynamicMaterialInstance(0, MI_SkySphere);
}

https://answers.unrealengine.com/questions/476579/loadobject-vs-staticloadobject.html

4 Likes

great, thanks!