Download

LoadObject without hardcoding the file path

Hello, I’m struggling to find a way to pass a TCHAR with the path of the file to LoadObject. All the examples that I’ve seen are passing a fixed path like:


 LoadObject<UParticleSystem>(NULL, TEXT("A/PATH/TO/A/FILE"), NULL, LOAD_None, NULL);

what I’m trying to do is receiving the path from another object instead, something like:


 LoadObject<UParticleSystem>(NULL, AnoherObject->ThePath, NULL, LOAD_None, NULL);

I’ve tried 2 ways since now (and both failed):

**1) *create “ThePath” variable as a TCHAR public variable for AnotherObject




public:
    TCHAR* ThePath = TEXT("A/PATH/TO/A/FILE");


it crashes the engine…

**2) **Use an FString as value for the “ThePath” variable and use the * to get a TCHAR




public:
    Fstring ThePath = "A/PATH/TO/A/FILE";



 LoadObject<UParticleSystem>(NULL, *AnoherObject->ThePath, NULL, LOAD_None, NULL);

it crashes the engine…

Which is the right way to pass the path to LoadObject without hardcoding the path in the call?!

It seems that option 2 is actually working correctly. I keep this question open since I wasn’t able to find the right way to load assets without hardcoding the file path.

Please provide anyway feedback if you see any issue with this approach to assets loading.

You can use TSoftObjectPtr property in your blueprint to store a path to objects in your content folder. If you move these objects around in the editor the paths will automatically update plus you can load the objects only when you need them. The downside is that as your project grows this doesn’t scale very well.

A better way is to customize UAssetManager for your project and use the FPrimaryAssetId system. This lets you use string identifiers to stream in any asset in your game on demand. But it requires a bit of setup and planning to work correctly.

Thank you Waves!

Very interesting I will try this immediately!