Creating C++ module

From UE4: Community Wiki
Jump to: navigation, search

Unreal Engine modules[edit]

Sometimes you might want to create editor or utility code that exists outside of our game code module. It can be either a 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 a 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 a plugin instead of a module. In my opinion creating a module makes sense if code you are about to create is tightly coupled with your game code.

File structure[edit]

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[edit]

In all following snippets replace all occurrences of YourProjectName with your actual project name and all occurrences of 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"
            }
        );

        PrivateDependencyModuleNames.AddRange(
            new string[]
            {
                "Slate",
                "SlateCore",
                "UMG"
            }
        );
    }
}

YourModuleNameModule.h

#pragma once

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

class FYourEditorModule : 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.