[FIX] How to create subdirectories in Source folder for C++ files without errors in UE5

Hi everyone! I am kinda new to Unreal, I been really struggling with the creation of subfolders inside the “Source” folder of Unreal C++ classes. So the main problem is this: I want to create new C++ class inside a subfolder of the “Source” folder. What happens is that both the folder and the class can be seen from the IDE but not from the content browser. The “fix” is quite simple and seems almost stupid, but instead of typing the new folder you want to be created in the path input, you have to click in the folder icon and create it in the explorer window that opens. I do not really know why, but if you do it that way the folder is immediately visible in the content browser, other way is just not visible unless you regenerate the project files.

This simple problem was very frustating for me because I kept finding lots of entries in forums of people having the same kind of errors and the only solutions were “delete these folders and regenerate the project files”, which although it works, it doesn’t really seem like the right way to do something as simple as creating a C++ inside a new subfolder.

Anyway hope this helps anyone! :slight_smile:

1 Like

To add folders to the project add them to the .Build.cs file like this. The folder has to have at least one file in it for it to show up in the editor.

PublicIncludePaths.AddRange(
	        new string[]
	        {
                  "ProjectName",
                  "ProjectName/Public",
                  "ProjectName/Public/Subfolder"
});
2 Likes

I implemented a recursive search that finds all “Public” folders under my Source folder. Allows me to not worry about adding paths constantly to my Build.cs file.

Use these namespaces:

using System;
using System.IO;
using System.Linq;

And add this to your Build.cs:

// find all public folders in Source
try
{
	// path to your Source folder in your project
	string dirPath = "C:/foo/bar/Source";

	// LINQ query.
	var dirs = from dir in
			Directory.EnumerateDirectories(
				dirPath, 
				"Public", 
				new EnumerationOptions {
					RecurseSubdirectories = true
				}
			)
			select dir;

	// Show results (optional)
	foreach (var dir in dirs)
	{
		// Remove path information from string.
		Console.WriteLine("{0}",
			dir.Substring(dir.LastIndexOf("\\") + 1));
	}
	Console.WriteLine("{0} directories found.",
		dirs.Count<string>().ToString());


	// add directories to PublicIncludePaths
	PublicIncludePaths.AddRange(dirs);
}
catch (UnauthorizedAccessException UAEx)
{
	Console.WriteLine(UAEx.Message);
}
catch (PathTooLongException PathEx)
{
	Console.WriteLine(PathEx.Message);
}

Actually you just need to include your own module in the include path.
My game name is BattleHeight:


BattleHeight.build.cs

public BattleHeight(ReadOnlyTargetRules Target) : base(Target)
	{
		PrivateIncludePaths.AddRange(new string[]
		{
			"BattleHeight"
		});
}