In this post we’re going to see how to create a game module for Unreal Engine 4. Imagine that modules are containers for a collection of related classes. After you’re done with this tutorial you can read more about the pros and cons of multiple module creation in Unreal Engine 4 in the official documentation.

This post was written in 4.15 version of the engine. Depending on the time you’re reading this, this information may be outdated.

In order to create a module we have to do the following things:

Modify the .uproject file of your project to include the new module Create a Module Build file that includes other dependencies and contains a path for private and public include paths Create at least one class inside our Module

Update for 4.20 version: As a lot of people mentioned in the comments this tutorial was outdated for versions 4.18+. I have updated the code snippets and introduced changes where necessary.

Before you move any further, make sure to close the project you’re currently editing. So, let’s get started!

Modifying the .uproject file

To modify the .uproject file right click on it and open it up using a text editor of your choice.

By default, your file will contain at least one module that has the same name as your UE4 project. After the ending bracket on the modules add a comma and then add your module. In my case, I added a runtime module named OrfeasModule. Here is the complete .uproject file:

UProject file { "FileVersion": 3, "EngineAssociation": "4.15", "Category": "", "Description": "", "Modules": [ { "Name": "GDBlogPost", "Type": "Runtime", "LoadingPhase": "Default" }, { "Name": "OrfeasModule", "Type": "Runtime", "LoadingPhase": "Default" } ] } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 { "FileVersion" : 3 , "EngineAssociation" : "4.15" , "Category" : "" , "Description" : "" , "Modules" : [ { "Name": "GDBlogPost", "Type": "Runtime", "LoadingPhase": "Default" }, { "Name": "OrfeasModule", "Type": "Runtime", "LoadingPhase": "Default" } ] }

Save your file and let’s create the Module Build File.

Creating a Module Build File

Navigate inside your project’s Source folder and create a new folder with the same name as your plugin (in my case, this will be OrfeasModule). Then, inside that folder add:

A folder named Public (this will contain the header files of the classes you’re going to add)

A folder named Private (this will contain the source files of the classes you’re going to add)

A new .cs file that has the following name [YourModuleName].Build.cs (in my case, this will be OrfeasModule.Build.cs)

Here is a screenshot of my folder structure so far:

Open up the .cs file and add the following code:

OrfeasModule.Build.cs file //------------------ 4.20 VERSION using UnrealBuildTool; public class OrfeasModule : ModuleRules { public OrfeasModule(ReadOnlyTargetRules Target) : base(Target) { PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; //Public module names that this module uses. //In case you would like to add various classes that you're going to use in your game //you should add the core,coreuobject and engine dependencies. PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine","GDBlogPost"}); //The path for the header files PublicIncludePaths.AddRange(new string[] {"OrfeasModule/Public"}); //The path for the source files PrivateIncludePaths.AddRange(new string[] {"OrfeasModule/Private"}); } } //------------------ PRE 4.18 VERSION using UnrealBuildTool; public class OrfeasModule : ModuleRules { public OrfeasModule(TargetInfo Target) { //Public module names that this module uses. //In case you would like to add various classes that you're going to use in your game //you should add the core,coreuobject and engine dependencies. //Don't forget to add your project's name as a dependency (GDBlogPost in my case) PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "GDBlogPost" }); //The path for the header files PublicIncludePaths.AddRange(new string[] {"OrfeasModule/Public"}); //The path for the source files PrivateIncludePaths.AddRange(new string[] {"OrfeasModule/Private"}); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 //------------------ 4.20 VERSION using UnrealBuildTool ; public class OrfeasModule : ModuleRules { public OrfeasModule ( ReadOnlyTargetRules Target ) : base ( Target ) { PCHUsage = PCHUsageMode . UseExplicitOrSharedPCHs ; //Public module names that this module uses. //In case you would like to add various classes that you're going to use in your game //you should add the core,coreuobject and engine dependencies. PublicDependencyModuleNames . AddRange ( new string [ ] { "Core" , "CoreUObject" , "Engine" , "GDBlogPost" } ) ; //The path for the header files PublicIncludePaths . AddRange ( new string [ ] { "OrfeasModule/Public" } ) ; //The path for the source files PrivateIncludePaths . AddRange ( new string [ ] { "OrfeasModule/Private" } ) ; } } //------------------ PRE 4.18 VERSION using UnrealBuildTool ; public class OrfeasModule : ModuleRules { public OrfeasModule ( TargetInfo Target ) { //Public module names that this module uses. //In case you would like to add various classes that you're going to use in your game //you should add the core,coreuobject and engine dependencies. //Don't forget to add your project's name as a dependency (GDBlogPost in my case) PublicDependencyModuleNames . AddRange ( new string [ ] { "Core" , "CoreUObject" , "Engine" , "GDBlogPost" } ) ; //The path for the header files PublicIncludePaths . AddRange ( new string [ ] { "OrfeasModule/Public" } ) ; //The path for the source files PrivateIncludePaths . AddRange ( new string [ ] { "OrfeasModule/Private" } ) ; } }

Depending on your needs, you may need to add more dependencies to your module later on. Moreover in the 4.20 version, you may need to delete your Binaries and Intermediate folders and re-generate your VS files from the .uproject file.

Creating a module class

The last thing we need to do is to add a new class that will handle the startup and shutdown of our module. Open up a text editor of your choice (again) and add the following code:

OrfeasModule.h #pragma once #include "ModuleManager.h" DECLARE_LOG_CATEGORY_EXTERN(OrfeasModule, All, All); class FOrfeasModule : public IModuleInterface { public: /* This will get called when the editor loads the module */ virtual void StartupModule() override; /* This will get called when the editor unloads the module */ virtual void ShutdownModule() override; }; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #pragma once #include "ModuleManager.h" DECLARE_LOG_CATEGORY_EXTERN ( OrfeasModule , All , All ) ; class FOrfeasModule : public IModuleInterface { public : /* This will get called when the editor loads the module */ virtual void StartupModule ( ) override ; /* This will get called when the editor unloads the module */ virtual void ShutdownModule ( ) override ; } ;

This is the header file of our class. Save it inside the Public folder we’ve added before with the same name as your module (in my case this is the OrfeasModule.h).

Then, create the following file using your text editor and save it inside the Private folder we’ve added before:

OrfeasModule.cpp #include "OrfeasModule.h" DEFINE_LOG_CATEGORY(OrfeasModule); #define LOCTEXT_NAMESPACE "FOrfeasModule" void FOrfeasModule::StartupModule() { UE_LOG(OrfeasModule, Warning, TEXT("Orfeas module has started!")); } void FOrfeasModule::ShutdownModule() { UE_LOG(OrfeasModule, Warning, TEXT("Orfeas module has shut down")); } #undef LOCTEXT_NAMESPACE IMPLEMENT_MODULE(FOrfeasModule,OrfeasModule) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include "OrfeasModule.h" DEFINE_LOG_CATEGORY ( OrfeasModule ) ; #define LOCTEXT_NAMESPACE "FOrfeasModule" void FOrfeasModule :: StartupModule ( ) { UE_LOG ( OrfeasModule , Warning , TEXT ( "Orfeas module has started!" ) ) ; } void FOrfeasModule :: ShutdownModule ( ) { UE_LOG ( OrfeasModule , Warning , TEXT ( "Orfeas module has shut down" ) ) ; } #undef LOCTEXT_NAMESPACE IMPLEMENT_MODULE ( FOrfeasModule , OrfeasModule )

Then, go to your .uproject file again and select the generate project files. Once you open up your editor, your module will be loaded. If you attempt to add a new c++ class using the class wizard, you can choose to store your class in your newly created module: