Dynamic Load Object

From Epic Wiki
Jump to: navigation, search

Overview

Dear Community,

here are my static library functions for saving a objects path and then loading an object dynamically from a path

I have a templated version of my dynamic load object function for your entertainment!

To test this you could remove the word static and put in any .h file of your choosing like player controller

Get Object's Path

//Get Path
static FORCEINLINE FName GetObjPath(const UObject* Obj)
{
	if(!Obj) 
        {
          return NAME_None;
        }
	//~
	
	FStringAssetReference ThePath = FStringAssetReference(Obj);
	
	if(!ThePath.IsValid()) return NAME_None;
	
	//The Class FString Name For This Object
	FString Str = Obj->GetClass()->GetDescription();
	
	Str += "'";
	Str += ThePath.ToString();
	Str += "'";
	
	return FName(*Str);
}

Why FName?

Because in my In-Game editor I only save the asset references to binary file, and use Dynamic Load Object during the loading of a level, and

 FName is half the size of FString, 8 compared to 16 bytes.

So I do it for Save File size-reduction reasons

My Templated Dynamic Load Object Function

//TEMPLATE Load Obj From Path
template <typename ObjClass>
static FORCEINLINE ObjClass* LoadObjFromPath(const FName& Path)
{
	if(Path == NAME_None) return NULL;
	//~
	
      return Cast<ObjClass>(StaticLoadObject( ObjClass::StaticClass(), NULL,*Path.ToString()));
}

Example Uses of The Template

// Load PS From Path 
static FORCEINLINE UParticleSystem* LoadPSFromPath(const FName& Path)
{
	if(Path == NAME_None) return NULL;
	//~
	
	return LoadObjFromPath<UParticleSystem>(Path);
}


// Load Material From Path 
static FORCEINLINE UMaterialInterface* LoadMatFromPath(const FName& Path)
{
	if(Path == NAME_None) return NULL;
	//~
	
	return LoadObjFromPath<UMaterialInterface>(Path);
}

// Load Static Mesh From Path 
static FORCEINLINE UStaticMesh* LoadMeshFromPath(const FName& Path)
{
	if(Path == NAME_None) return NULL;
	//~
	
	return LoadObjFromPath<UStaticMesh>(Path);
}


Rama (talk)