Creating C++ module

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...

Updated over 2 years ago Edit Page Revisions

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 is tightly coupled with your game code.

File structure

In your Source directory you should already have YourProjectName directory with source code of your game code.

Create YourModuleName directory and add YourModuleName.Build.cs file inside newly created directory.

Now, inside same directory create header and source files named YourModuleNameModule.h/cpp .

If you keep your source files under Private/Public directories put them in respective directory.

Setup

In all following snippets replace all occurrences of

  • YourProjectName with your actual project name

  • YourModuleName with your actual 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

Regenerate Visual Studio solution.