Something quite common when you are creating a server or testing is to display a list of the maps that you have available. Simplified from the code Epic is using in their FGameProjectHelper class…
/**
* Gets the sorted list of available maps for current game, filter can be specified using IFileManagers filters syntax.
*
* @return A sorted & filtered list of available map names.
*/
TArray<FString> FSomeClass::GetAvailableMaps(FString Filter)
{
TArray<FString> Result;
TArray<FString> ProjectMapNames;
const FString WildCard = FString::Printf(TEXT("%s%s"),*Filter, *FPackageName::GetMapPackageExtension());
IFileManager::Get().FindFilesRecursive(ProjectMapNames, *FPaths::Combine(*FPaths::RootDir(), FApp::GetGameName(), TEXT("Content"), TEXT("Maps")), *WildCard, true, false);
for (int32 i = 0; i < ProjectMapNames.Num(); i++)
{
Result.Add(FPaths::GetBaseFilename(ProjectMapNames*));
}
Result.Sort();
return Result;
}
Usage is fairly easy:
// Gets all maps
TArray<FString> AvailableMaps = FSomeClass::GetAvailableMaps(TEXT("*"));
// // Gets maps starting with GM_
//TArray<FString> AvailableMaps = FSomeClass::GetAvailableMaps(TEXT("GM_*"));
for(int i=0; i< AvailableMaps.Num(); i++)
{
UE_LOG(MyLog, Log, TEXT("Map: %s"),*AvailableMaps*);
}
HTH,
D.
PD: This piece leaves out engine maps and it always sorts the result, because it’s what we need. If you need something different, look for FGameProjectHelper::GetAvailableMaps( in the engine source