Dynamic Load Object?

Dear Friends at Epic,

What is the best / memory efficient way to dynamically load objects from a supplied path?

I can use the object finder to create an object, but when I try to use the same object finder again it still contains the previously found object, ignoring the new supplied path:

//create a JoySMA class to cover these types of functions for maaaaaany future classes
void AJoySMA::OnRep_SetMaterial()
{
	if (MaterialPath == "") return;
	//~~~~~~~~~~~~~~~~~~~~~~~
	
	//testing only
	if (VictoryPC) VictoryPC->Optimize(MaterialPath);
		//the path is the correct new path here
	
	//Load Asset From Path
	static ConstructorHelpers::FObjectFinder OnRep_SetMaterialOb(*MaterialPath);
	
	//Asset not found?
	if (!OnRep_SetMaterialOb.Object) return;
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		
	//testing only
	if (VictoryPC) VictoryPC->Optimize("GOT PAST OBJECT FINDER");
	if (VictoryPC) VictoryPC->Optimize(VictoryPC->GetObjectPath(OnRep_SetMaterialOb.Object));
		//path is still whichever the first constructed object path was
	
	StaticMeshComponent->SetMaterial(0,OnRep_SetMaterialOb.Object);
}

UObjectGlobals.h

I see these functions, but I want to make sure I am not using these functions in a way that would be inefficient

COREUOBJECT_API UObject* StaticLoadObject( UClass* Class, UObject* InOuter, const TCHAR* Name, const TCHAR* Filename = NULL, uint32 LoadFlags = LOAD_None, UPackageMap* Sandbox = NULL, bool bAllowObjectReconciliation = true );

COREUOBJECT_API UClass* StaticLoadClass( UClass* BaseClass, UObject* InOuter, const TCHAR* Name, const TCHAR* Filename, uint32 LoadFlags, UPackageMap* Sandbox );

 COREUOBJECT_API UObject* StaticConstructObject( UClass* Class, UObject* InOuter=(UObject*)GetTransientPackage(), FName Name=NAME_None, EObjectFlags SetFlags=RF_NoFlags, UObject* Template=NULL, bool bCopyTransientsFromClassDefaults=false, struct FObjectInstancingGraph* InstanceGraph=NULL );

Thanks!

Rama

In a C++ function, when you include an initialization for a static object in the same line it is declared, the object is only ever initialized the first time the function is run.
Also you probably want to use StaticLoadObject to dynamically load objects from a given path.

So if you wanted to use a static object, your function could look something like:

// Static declare a Material (declared only the first time the material is run.
static UMaterial* MyMaterial;

MyMaterial = NULL; // Clear previous material

// Non-static initialize the material (runs every time function is called)
MyMaterial = CastStaticLoadObject( ...  ); 

As for the other two functions, you’ll most likely never need to call StaticLoadClass(), and instead of StaticConstructObject(), you should use the more convenient NewObject(), which is declared in the same header file.

Thanks so much for the detailed answer Joe!

:slight_smile:

Rama

Is it mean you can load a .uasset file from hard disk which the path is not included in the game path?Or what you say is just load a game-build-in resource?