skip to Main Content

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:

  1. Modify the .uproject file of your project to include the new module
  2. Create a Module Build file that includes other dependencies and contains a path for private and public include paths
  3. 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:

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:

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:

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:

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:

 

Avatar photo

This Post Has 13 Comments

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

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

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

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

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

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

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

  6. […] 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/ […]

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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top