Donekulda
(Donekulda)
January 3, 2020, 2:58pm
1
Hi guys,
im trying to load Material of my Actor using FindObject and LoadObject . Reason for this is, that i want to have different material used based on its preferences that are initilised after its spawn. But the code that i created always causes the engine to crash.
Here is my:
.cpp file
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Find or load specified Material
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
StarMaterial = nullptr;
//TCHAR materialLoc = TEXT("/Game/Materials/Galaxy/Class/%2s/%1d", Class, MaterialClass);
if (Class != NULL && MaterialClass != NULL) {
FString materialLoc = FString::Printf(TEXT("'/Game/Materials/Galaxy/Class/%s/%d"), Class, MaterialClass);
StarMaterial = FindObject<UMaterialInterface>(NULL, TEXT("MaterialInstanceConstant'/Game/Materials/Galaxy/Class/M/3'"));
if (StarMaterial) {
SuperMesh->SetMaterial(0, StarMaterial);
}
else {
StarMaterial = LoadObject<UMaterialInterface>(NULL, TEXT("MaterialInstanceConstant'/Game/Materials/Galaxy/Class/M/3'"), NULL, LOAD_None, NULL);
if (StarMaterial) {
SuperMesh->SetMaterial(0, StarMaterial);
}
}
}
.h file
UStaticMeshComponent* SuperMesh;
UMaterialInterface* StarMaterial;
The FString materialLoc is there prepared for ability to change the path based on its Inputs, but its not now in use because i want to test the ability to load a material with hard set location, but it always crashes the engine once it loads. So im asking if someone doesnt sees there some error because i dont see it there.
PS: Sorry about my English syntax.
adludum
(adludum)
January 5, 2020, 1:52am
2
Perhaps you can try something like this
Have an in-line function for materials
template <typename ObjClass>
static FORCEINLINE ObjClass* LoadObjFromPath(const FName& Path)
{
if (Path == NAME_None) return nullptr;
return Cast<ObjClass>(StaticLoadObject(ObjClass::StaticClass(), nullptr, *Path.ToString()));
}
static FORCEINLINE UMaterial* LoadMaterialFromPath(const FName& Path)
{
if (Path == NAME_None) return nullptr;
return LoadObjFromPath<UMaterial>(Path);
}
And call it like this at runtime
FString sPath = "/Game/Materials/YourMat";
UMaterial* mat = LoadMaterialFromPath(FName(*sPath));
Donekulda
(Donekulda)
January 5, 2020, 10:13am
3
Thanks a lot, this is big help.
VortexFX
(VortexFX)
November 10, 2020, 2:12pm
4
adludum:
Perhaps you can try something like this
Have an in-line function for materials
template <typename ObjClass>
static FORCEINLINE ObjClass* LoadObjFromPath(const FName& Path)
{
if (Path == NAME_None) return nullptr;
return Cast<ObjClass>(StaticLoadObject(ObjClass::StaticClass(), nullptr, *Path.ToString()));
}
static FORCEINLINE UMaterial* LoadMaterialFromPath(const FName& Path)
{
if (Path == NAME_None) return nullptr;
return LoadObjFromPath<UMaterial>(Path);
}
And call it like this at runtime
FString sPath = "/Game/Materials/YourMat";
UMaterial* mat = LoadMaterialFromPath(FName(*sPath));
I’m trying to use this however I’m getting
Warning: Failed to find object 'Material None./Game/Materials/LavaPlanet'
It’s a material instance, not a material.
When I get the master material it works correctly does this only work with the material, not material instance?
Thanks
Fieol
(Fieol)
November 29, 2020, 7:14am
5
VortexFX:
I’m trying to use this however I’m getting
Warning: Failed to find object 'Material None./Game/Materials/LavaPlanet'
It’s a material instance, not a material.
When I get the master material it works correctly does this only work with the material, not material instance?
Thanks
Try writing UMaterialInstance their instead of UMaterial type