While our game may be running without any issues in the editor or even in…
Creating Custom Modules
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:
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:
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:
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:
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:
Thank you for that tutorial, I was wondering how to do that!
But I have a question regarding modules in UE4. Is it possible to pass data between the modules? I mean, I wanted to connect my game with a web api, so moving that functionality to another module, different from the game module. Would that be possible?
Hello Mihail,
So according to the official UE4 docs: “We do support creating modules that are cross-dependent (both export and import functions and data from each other — e.g., Engine and UnrealEd modules), but this is not ideal for compile-times and may sometimes cause problems with static initialization of variables. Gameplay modules that are not cross-dependent are harder to design and maintain, but the code may be cleaner for it.”
I think that you can connect your game with a web api by moving all the corresponding functionality to a module dedicated for this particular use. Then, you could make backend calls to your web API from your game’s module by using the created module as a middleman.
-Orfeas
Seems also need add our module to Target.cs file.
My UE version is 4.17
Thanks!!!! In 4.18 I added new module to [YourProjectName].Target.cs and [YourProjectName]Editor.Target.cs and works! 🙂
Seems like this:
ExtraModuleNames.AddRange( new string[] { “TwoModules”, “NewModule” } );
(where “TwoModules” are the name of my project and “NewModule” the new module :P)
Hi Orefeas,
I could not get this to work Some steps are missing as in BP project I had no default module called GDBlogPost. So I assumed this:
1. Created a new c++ project called it GDBlogPost -> This contained the default module GDBlogPost.
2. Created the OrfeasModule .h and .cpp files.
3. Modifed the .uproject file to include the ‘OrfeasModule’ as you show.
In my Source folder I have the following:
GDBlogPost
OrfeasModule
GDBlogPost.target.cs
GDBlogPostEditor.target.cs
When it finished generating the visual studio files. I launch the .uproject file and following errors :
OrefeasModule is missing would you like to rebuild -> I click Yes.
Then it says GDBlogPost could not be build
Could you help, please Im using 4.15.3.
A link to complete project would be appreciated, if its not too much trouble, as Im trying to add custom debugger category which you show in another tutorial, but you state that first I need to complete this
Thank you
I can’t seem to get this to work, even after the suggestions for 4.18 above….
Discovering modules, targets and source code for project…
While compiling C:\Users\szaharakis.D2\Documents\Unreal Projects\Classes\Intermediate\Build\BuildRules\ClassesModuleRules.dll:
ERROR: ..\Unreal Projects\Classes\Source\OrfeasModule\OrfeasModule.Build.cs(5,9) : error CS1729: ‘UnrealBuildTool.ModuleRules’ does not contain a constructor that takes 0 arguments
ERROR: UnrealBuildTool Exception: Unable to compile source files.
Post has been updated for 4.20 version.
-Orfeas
I wasn’t able to make this work until I edited both the Target.cs and TargetEditor.cs files. In both of these files, I changed
ExtraModuleNames.Add(“GDBlogPost”);
to
ExtraModuleNames.AddRange(new string[] { “GDBlogPost”, “OrfeasModule” });
Now it works!
My engine version is 4.21
Can confirm this works with engine 4.22.1 aswell.
I had to do one more step with 4.21
I had to open -projectname-.Target.cs and add my module name to the additional modules list.
[…] Add to Config/DefaultEngine.ini (you can remove it after compilation)[/Script/Engine.Engine]+ActiveGameNameRedirects=(OldGameName=”/Script/OldName”, NewGameName=”/Script/NewName”)Example : +ActiveGameNameRedirects=(OldGameName=”/Script/Arrow”, NewGameName=”/Script/DontFeedTheAnimals”)+ActiveGameNameRedirects=(OldGameName=”/Script/OculusPlatformSample.OSSLeaderboardWidget”, NewGameName=”/Script/DontFeedTheAnimals.OSSLeaderboardWidget”)More here https://www.orfeasel.com/creating-custom-modules/ […]
Great tutorial.
I’m using 4.24.2, however, and I replaced
#include “ModuleManager.h”
with
#include “CoreMinimal.h”
#include “Modules/ModuleInterface.h”
which seems more in sync with the current documentation.
Hi Orfeas could you please advise on compiling for UE4.27 ?