Creating module in Unreal Engine

Posted on Feb 16, 2020

Motivation

Creating a new module in Unreal Engine is fairly simple but unfortunately not very well described on official documentation pages. It is very easy to omit one required step which is then very hard to find and is often a source of frustration and irritation. This post is a step by step guideline on module creation to help anyone (including me) go through this process effortlessly.

Module creation

It is important to know that by default there is only one module - the one that is created by Unreal when generating new project files. Root of this module is $(project_root)/Source/$(project_name).

Here are the steps:

  1. Create a new directory inside the Source catalogue with your new module name e.g. EditorUtilities.
  2. Inside the EditorUtilities directory create a C# file with the following name: EditorUtilities.Build.cs.
  3. Create header and source file for the module with the following names: EditorUtilitiesModule.h and EditorUtilitiesModule.cpp. In case source file encapsulation is required it is okay to create Private and Public catalogues and put the files in the corresponding catalogues.
  4. Edit EditorUtilities.Build.cs to include the following code:
using UnrealBuildTool;

public class EditorUtilities : ModuleRules
{
    public EditorUtilities(ReadOnlyTargetRules Target) : base(Target)
    {
        PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

        PublicDependencyModuleNames.AddRange(new string[] { 
            "Core",
            "CoreUObject", 
            "Engine", 
            "InputCore", 
            "HeadMountedDisplay", 
            "NavigationSystem", 
            "AIModule" 
        });
    }
}
  1. Edit EditorUtilitiesModule.h to include the following code:
#pragma once

#include "CoreMinimal.h"
#include "Modules/ModuleInterface.h"

class FEditorUtilitiesModule : public IModuleInterface
{
	virtual void StartupModule() override;
	virtual void ShutdownModule() override;
};
  1. Edit EditorUtilitiesModule.cpp to include the following code:
#include "EditorUtilitiesModule.h"

IMPLEMENT_MODULE(FEditorUtilitiesModule, EditorUtilities);

void FEditorUtilitiesModule::StartupModule()
{
	// Put your module initialization code here
}

void FEditorUtilitiesModule::ShutdownModule()
{
	// Put your module termination code here
}
  1. Open .uproject file and add the following entry to the "Modules" array:
...
	"Modules": [
		{
			"Name": "YourModuleName",
			"Type": "Runtime", // Set your desired runtime type
			"LoadingPhase": "PostEngineInit" // Set your desired loading phase
		}
	]
...

More information on different types and loading phases can be found here and here.

  1. Finally edit either $(project_name).Target.cs or $(project_name)Editor.Target.cs(depending on the module purpose) and add new module name to extra module names array:
...
	ExtraModuleNames.Add("EditorUtils");
...

Extra

There is an open source plugin that adds a new button in the editor and generates a new module here.