Getting a TAssetPtr<UWorld> level reference from the content browser?

Hello,
My MapLoader class takes in TAssetPtr<UWorld> references and streams in the maps when needed.
This works fine with UPROPERTY() TAssetPtr<UWorld> variables.

However, I would also like to load levels through paths. ("/Game/Maps/Test_Map")

Rather than load a pathed level directly, how would I get the TAssetPtr<UWorld> from a path so I can send it to my MapLoader?

Thanks!

Note: Everything I’ve seen online loads the levels immediately, while I only want to get the TAssetPtr>UWorld> of them.

You can make an asset pointer through a pathname.
Try for example “Get Asset by Object Path”: https://docs.unrealengine.com/en-US/…age/index.html

TSoftObjectPtr<UWorld> you can convert to a path using builtin function ToSoftObjectPath() and you can also convert a SoftObjectPath back to a TSoftObjectPtr<>

Yes, that’s what I’m looking for!

But how can I make that conversion? Can’t find it.
Also how to convert from TSoftObjectPtr to TAssetPtr? Are they the same? Should I be using TSoftObjectPtr instead?



FSoftObjectPath path = FSoftObjectPath("Game/Maps/Test_Map");

TAssetPtr<UWorld> newMap = **(??? path ???)**;


Thanks!

TSoftObjectPtr<> has a constructor from FSoftObjectPath.

You can do



FSoftObjectPath path = FSoftObjectPath("Game/Maps/Test_Map.Test_Map");

TSoftObjectPtr<UWorld> newMap( path );

somePath = newMap->ToSoftObjectPath();


And can also be used as editable UProperty, so I would simply quit using TAssetPtr and use that instead.

Works great, thanks!
But 1 more question:

Any idea how to see if the map is valid or not? “newMap” seems to return null no matter what?

Valid path - Everything returns null

https://i.gyazo.com/665c835ab06faa1177a50d9935eac1e7.png

Fake path - Same results

https://i.gyazo.com/8a9ef0b210df548e70f91f5dacd87dac.png

You can make it an editable property and assign the value instead of manually typing a string, to see/log the actual valid path to the object.

Yes but if that property were left empty, newMap->ToSoftObjectPath(); would cause a crash. :slight_smile:
This is especially possible when dealing with hundreds of maps through an exposed array/tmap.

Would just be nice to know if the map is valid or not, though I guess it’s not possible without loading the map?

I mean you can do it in reverse to see the actual syntax, how the path string is correctly formatted, the example string above can be invalid.

Thanks for the thread guys.
The issue for me was that I didn’t include the “.Level_Name” post-fix after the path name.

e.g.
“/Game/Maps/Level_Name” → That’s invalid
“/Game/Maps/Level_Name.Level_Name” → That’s valid!

Thanks!