Download

Finding available maps for your game

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 :wink:

Thanks for this, was just lookin for this after finding out UObjectLibraries don’t let you load uworlds (I wonder why, ha!)

Anyway, a slight bug, I’m using a source built editor and when I use RootDir() I get the git directory, not my project directory. I changed that and the game name bits to *FPaths::GameDir() and it worked as expected.