Unreal Engine Module Creation

Unreal Engine modules

Sometimes you might want to create editor or utility code that exists outside of our game code module. It can be either custom math module, debugging tools or editor extending code. On top of that, we might want the module to be available only in editor, never in non editor version of the game. We can do so by creating new module that will get included into our game module only at certain build settings.

Please note that if you are about to create some generic code that can be shared across multiple projects consider creating plugin instead module. In my opinion creating module makes sense if code you are about to create there is tightly coupled with your game code.

Also, note that this post has more of a my personal note rather than tutorial.

File structure

In your Source directory you should already have YourProjectName catalog with source code of your game code. Create YourModuleName catalog. Create YourModuleName.Build.cs inside newly created catalog. Now, inside same catalog create header and source files named YourModuleNameModule.h/cpp. If you keep your source files under Private/Public directories put them in respective catalogs.

Setup

In all following snippets replace all occuranes of: * YourProjectName with your project name * YourModuleName with your module name

YourModuleName.Build.cs:

using UnrealBuildTool;

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

        PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "NavigationSystem", "AIModule", "YourProjectName" });
    }
}

YourModuleNameModule.h:

#pragma once

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

class FYourModuleNameModule : public IModuleInterface
{
	virtual void StartupModule() override;
	virtual void ShutdownModule() override;
};

YourModuleNameModule.cpp:

#include "YourModuleNameModule.h"

IMPLEMENT_MODULE(FYourModuleNameModule, YourModuleName);

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

void FYourModuleNameModule::ShutdownModule()
{
	// Put your module termination code here
}

YourProjectName.uproject:

...
	"Modules": [
...
		{
			"Name": "YourModuleName",
			"Type": "Runtime", // Set your desired runtime type
			"LoadingPhase": "PostEngineInit" // Set  your desired loading phase
		}
	]
...

YourProjectNameEditor.Target.cs:

...
	ExtraModuleNames.Add("YourModuleName");
...

If you wish the module to be visible also in non editor targets add same line of code inside YourProjectName.Target.cs file.

Regenerate Visual Studio solution.