Game Engine Crash!

Hej,

I tried to load a list of staticmesh from the path, but the unreal editor crashes without any error message.
I couldn’t understand what’s the problem. Please help me on this issues.

In main class

teststaticmesh = LoadMeshFromPath("StaticMesh'/Game/'");
StaticMeshComponent->SetStaticMesh(teststaticmesh);
RootComponent = StaticMeshComponent;

Is the LoadMeshFromPath declaration right??? How to set RootComponent to static mesh dynamically ???

Try to load a given asset:

TSubobject<UStaticMeshComponent> mesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("Mesh0"));
const ConstructorHelpers::FObjectFinder<UStaticMesh> MeshObj(TEXT("/Game/MyMesh"));
 mesh->SetStaticMesh(MeshObj.Object);

There are some nice tutorials on how to load object dynamically such as this one from Rama or the documentation about Asynchronous Asset Loading.

To get a list of files what you need is to repeat the above process for each one. You can use the FileManager to find files within a directory. More about the file manager from its documentation page and another tutorial. To find all files in a directoy just use the FPlatformFileManager::Get().GetPlatformFile().FindFiles() function with a wildcard

Yet another way would be loading a set of assets from a given path. It might fail if you try it from a packaged game and you are not adding the paths to be cooked. Add all paths into the Additional Asset Directories to Cook section of your project.

TArray<UObject*> MeshAssets;
EngineUtils::FindOrLoadAssetsByPath(TEXT("/Game/Assets/Meshes/"), MeshAssets, EngineUtils::ATL_Regular);

Take into account that the first approach using the FObjectFinder will work on a packaged game while EngineUtils::FindOrLoadAssetsByPath requires the editor to be present. The following code will load an asset from a given path using an instance of FStreamableManager. I usually just have one in my Game Singleton Class (more info on how to set one up can be found in this wiki post).

FStreamableManager& StreamableManager = [Get the singleton instance from where you declared it];
UObject* myObject = StreamableManager.SynchronousLoad(AssetPath);

In a packaged game you would normally use a PAK file with the content you would like to load and request the list of files that is contained in it. To add stuff to a pak file just the UnrealPak.exe for it. Here is a shorthand example, have a look at the FAssetStreamer::StreamPackage() function which does the whole process (the function is not complete but outlines the required steps).

Just in case you would like to play with pak files this is how you add assets to them:

UnrealPak.exe OutPak.pak myasset.uasset anotherasset.uasset

As you can see there are lots of stuff that you can use. If you just want to be able to load all assets from a given path in runtime you should investigate further EngineUtils::FindOrLoadAssetsByPath or you ensamble the list in the editor and then you use the FStreamableManager or the FObjectFinder to load them. In all cases you must ensure that your assets are taken into account when you cook your game.

thanks, The above code is working fine for one mesh. But I am looking for multiple mesh to load. For example; If I want do to this for one more Staticmesh, I have to repeat this line of code again. Is there method I can use instead ?

I have already tried this, but the MeshAssets returns Null. Can you please share the samples.

Just added a bit on how to find files ^^

Did the samples work for you?

No. It doesn’t work, But I’ve solved the issue the another way around. By using below link How to get file list in a directory? - Programming & Scripting - Epic Developer Community Forums