Creating an Editor Module in Unreal Engine 4



This guide will provide a brief instruction on how to create an editor module for Unreal Engine 4. We will create a basic implementation of an editor module and make it ouput a log to Unreal its output log at editor startup.

Creating your own custom editor module can be useful when you want to extend certain parts of Unreals editor interface. As it is an editor module, it is also useful for containing certain classes to only exist in the editor.

This guide is mainly meant as a preparation for Creating a Custom Asset Editor for Unreal Engine 4 and will only briefly go over this subject. If you are interested in a more detailed explanation on how to set up an editor module, I can highly recommend taking a look at Sandor Daemen's tutorial on Editor modules.

About you

This guide is meant for Unreal Engine C++ programmers who are interested in creating an editor module. At the end of this guide you will have a better understanding of how to set up your own custom editor modules and you will be able to start setting up many more on your own.

You are expected to have some experience with programming in Unreal Engine 4 and you know your way around Visual Studio.

Prerequisites

Additional Notes

This guide is mainly written as a set-up for a more comprehensive guide that explains how to create and implement a Custom Asset Editor for Unreal Engine 4. If you are interesting in achieving this, make sure to start at the beginning.

Setting up the project

This part will teach you how to set up your Unreal project. We will set up some additional modules to keep everything structured while also splitting runtime and editor code into separate modules.

Prepare the Unreal project file

Open the visual studio solution and use the solution explorer to navigate to the Unreal project file. This file is indicated as yourprojectname.uproject and contains information about your project like the engine version associated with the project.

Configuring the Editor module

{
	"FileVersion": 3,
	"EngineAssociation": "4.22",
	"Category": "",
	"Description": "",
	"Enterprise": true,
	"Modules": [
		{
			"Name": "AssetEditorTutorial",
			"Type": "Runtime",
			"LoadingPhase": "Default"
		}
	]
}
In the entry for Modules you can see your runtime game module, in this example it is called AssetEditorTutorial. We will add a new entry just below our primary game module.
{
	"FileVersion": 3,
	"EngineAssociation": "4.22",
	"Category": "",
	"Description": "",
	"Enterprise": true,
	"Modules": [
		{
			"Name": "AssetEditorTutorial",
			"Type": "Runtime",
			"LoadingPhase": "Default"
		},
		{
			"Name": "CustomAssetEditor",
    		"Type": "Editor",
    		"LoadingPhase": "PreDefault"
		}
	]
}

We add an additional module to our Unreal project file. This CustomAssetEditor module is marked as a Editor type because this module will only contain editor functionality. Because we want our custom editor to be ready at engine initialization we do have to change the default loading phase as well. For more information about available loading phases, I encourage you to take a look at the documentation.

Setting up the Editor Module


We will need access to some parts of the engine before we can start creating our custom asset editor. We can add these dependencies in our module build rules. Within your Source directory, we create a additional folder with the same name as the module we created in the previous step, this is necessary as we want to keep our editor code separated from our game code. Optionally, you can keep C++ headers and implementation files separated from each other by adding the Public and Private folders.
Source/CustomAssetEditor/
						 Private/
						 Public/

In this example, I have created the folder structure above.

Configure the module build rules

using UnrealBuildTool;

public class CustomAssetEditor : ModuleRules
{
    public CustomAssetEditor(ReadOnlyTargetRules Target) : base(Target)
    {
        PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
        
        PublicDependencyModuleNames.AddRange(
            new string[] 
            { 
                "Core", 
                "CoreUObject", 
                "Engine", 
                "InputCore"

                // ... add public dependencies that you statically link with here ...   
            }
        );

        PrivateDependencyModuleNames.AddRange(
            new string[] 
            {
                "Tutorial"

                // ... add private dependencies that you statically link with here ...    
            }
        );

        PublicIncludePaths.AddRange(
            new string[]
            {
                "CustomAssetEditor/Public"
            }
        );

        PrivateIncludePaths.AddRange(
            new string[]
            {
                "CustomAssetEditor/Private"
            }
        );
    }
}

Your default CustomAssetEditor.Build.cs should look something like the above and should be placed in your Source/CustomAssetEditor/ folder . It contains some static links to private and public modules . If you have set up your folder structure to use the Private and Public folders, we also add these to the include paths.

Configure the editor target file

ExtraModuleNames.AddRange( 
    new string[] 
    { 
    	"Tutorial",
        "CustomAssetEditor" 
    } 
);

We also need to add our editor module to the yourprojectnameEditor.Target.cs. This will ensure that we compile binaries for our module. This file can be found in project_path/Source/.

Configuring the Editor module source files

We will now need to configure our Editor Module source files. Go ahead and create the CustomAssetEditorModule class. Do not forget to place the files in the correct public or private folders if you are following the same folder structure as this guide.

#pragma once

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

class FCustomAssetEditorModule : public IModuleInterface
{
public:
	// Begin IModuleInterface implementation
	virtual void StartupModule() override;
	virtual void ShutdownModule() override;
	// End IModuleInterface implementation
};
We will create a custom module class that will extend from IModuleInterface.h and override the StartupModule() and ShutdownModule() functions provided by the interface. This class represents the implementation of the module which will be used to initialize it during startup.
#include "CustomAssetEditorModule.h"

IMPLEMENT_GAME_MODULE(FCustomAssetEditorModule, CustomAssetEditor);

#define LOCTEXT_NAMESPACE "FCustomAssetEditorModule"

void FCustomAssetEditorModule::StartupModule()
{
	UE_LOG(LogTemp, Warning, TEXT("StartupModule() called"));
}

void FCustomAssetEditorModule::ShutdownModule()
{
	UE_LOG(LogTemp, Warning, TEXT("ShutdownModule() called"));
}

#undef LOCTEXT_NAMESPACE

Finally, we implement the functions in our implementation file. Note the usage of the IMPLEMENT_GAME_MODULE, do not forget to add this otherwise the engine will not know what module we are implementing. The first argument is the name of the module implementation class which we defined in the header file. The second in the module name as declared in the Unreal project file.

Test the Editor module

You can now recompile the solution via Visual Studio (Build->Rebuild Solution). As soon as the solution has been rebuild, launch the project and check your Output Log for the log. Please note that you might have to scroll up the list of messages.

Conclusion

We have briefly gone through the process of setting up our own custom editor module. This means that we can now split our game and editor code into separate folders and modules.

If you have any questions or comments on this guide, feel free to email me at cairan.steverink@gmail.com and I will be happy to answer your questions.

Further Reading and References


Share this Post: