I want to create a simple level selector, that allows me to select a map to play. Right now I’ve hardcoded the list of maps, but it’s a clunky solution at best. Is there a way to list all maps included in project, or in specific directory? Preferably with blueprints, but C++ is OK too.
So I’ve found a solution. The relevant classes are described here: https://docs.unrealengine.com/latest/INT/Programming/Assets/AsyncLoading/index.html
Here’s my code
TArray<FString> UMyGameInstance::GetAllMapNames(){
auto ObjectLibrary = UObjectLibrary::CreateLibrary(UWorld::StaticClass(), false, true);
ObjectLibrary->LoadAssetDataFromPath(TEXT("/Game/Maps"));
TArray<FAssetData> AssetDatas;
ObjectLibrary->GetAssetDataList(AssetDatas);
UE_LOG(LogTemp, Warning, TEXT("Found maps: %d"), AssetDatas.Num());
TArray<FString> Names = TArray<FString>();
for (int32 i = 0; i < AssetDatas.Num(); ++i)
{
FAssetData& AssetData = AssetDatas[i];
auto name = AssetData.AssetName.ToString();
Names.Add(name);
}
return Names;
}
This function is blueprint-accessible and used to populate map list in UI. I’m not completely sure it’s the correct way, but it does work.
I’ve looked at ShooterGame first, and they just hardcode names:
static const FName PackageNames[] = { TEXT("test"), TEXT("Sanctuary.umap"), TEXT("Highrise.umap") };
You can also have a look at the ShooterGame Project. I know that the 2 Maps are displayed in the UI to let the Player choose them, but they could be displayed by Slate (UMG Code).
So maybe it’s not that easy to find, but this Project helps a lot
I did a search through the code, weird thing is that I didn’t find this ‘PackageNames’ is being referenced anywhere. Does anyone know how it affects things?
Can you please make a blueprint function plugin from this, and publish it? Thank you!
I used the answer above, but it wasn’t able to find maps in pak files from mods.
IFileManager::Get().FindFilesRecursive(MapFiles, FPaths::GameContentDir(), TEXT(".umap"), true, false);
Gets me a list of all umap files including ones in mods. You have to do a bit more string manipulation to get them in the format you want, but it’s working thus far for me.
I’m sorry, what connection between your code and “answer above”??? i don’t even mention about how workable your code…
this works great! I just had to make one change for it to work: the Filename string has to contain an asterisk “*.umap”
My final utility function that I use to get all map file names in my project:
UFUNCTION(BlueprintCallable, Category = "Utility")
static FORCEINLINE TArray<FString> GetAllMapNames()
{
TArray<FString> MapFiles;
IFileManager::Get().FindFilesRecursive(MapFiles, *FPaths::GameContentDir(), TEXT("*.umap"), true, false, false);
for (int32 i = 0; i < MapFiles.Num(); i++)
{
// replace the whole directory string with only the name of the map
int32 lastSlashIndex = -1;
if (MapFiles[i].FindLastChar('/', lastSlashIndex))
{
FString pureMapName;
// length - 5 because of the ".umap" suffix
for (int32 j = lastSlashIndex + 1; j < MapFiles[i].Len() - 5; j++)
{
pureMapName.AppendChar(MapFiles[i][j]);
}
MapFiles[i] = pureMapName;
}
}
return MapFiles;
}
Get All Map Names Plugin - Content Creation - Unreal Engine Forums I made a little plugin that gets all map names and gives back an array containing the names perfectly for level selection menus it contains the code mBeierling provided and some additional things.
Optimize Your code using ParseToArray:
TArray<FString> temp, Lista;
IFileManager::Get().FindFilesRecursive(Lista, *FPaths::GameContentDir(), TEXT("*.umap"), true, false, false);
for (int i = 0; i < Lista.Num(); i++) {
Lista[i].ParseIntoArray(temp, TEXT("Maps/"), true);
Lista[i] = temp[1];
Lista[i].ParseIntoArray(temp, TEXT("."), true);
Lista[i] = temp[0];
}
return Lista;
}
For anyone finding this through Google search. This is how it’s done using Blueprint. You can also apply filters when working with the asset registry