In this lesson, you will learn how to use the IModuleInterface to execute custom code the moment your plugin or game module loads or unloads.
The Lifecycle of a Module
Every module in Unreal Engine acts like a mini-application that needs a clear starting and ending point. When the Engine starts, it must initialize your code; when it shuts down, it must clean up to prevent memory leaks or crashes.
To handle these events, every module inherits from IModuleInterface. This interface provides two primary functions that you override: StartupModule() and ShutdownModule().
Implementing the Interface
When you create a module, you will typically see a class declaration in your header file that looks like this:
class FMyPluginModule : public IModuleInterface
{
public:
virtual void StartupModule() override;
virtual void ShutdownModule() override;
};
1. StartupModule(): This is where you perform your "registration." If your plugin adds a new global service, registers a custom settings menu in the Editor, or hooks into an engine delegate, you do it here. Think of this as the "setup" phase where you ensure your dependencies are ready for use.
2. ShutdownModule(): This is the mirror image of your startup. If you registered a service or allocated global memory during startup, you must unregister or free it here. If you fail to clean up, the Engine may keep "ghost" references to your module, leading to crashes when you try to close the Editor or launch your game.
Why This Matters
By managing the lifecycle, you ensure your module is a "good citizen" in the Engine. If your plugin creates a temporary tool window in the Editor, you register that window during StartupModule and destroy the pointer during ShutdownModule. This keeps your project stable and prevents dangling references that could persist after your module is supposedly gone.
Key Takeaway: Always use StartupModule to initialize global services and ShutdownModule to cleanly unregister them, ensuring your modules leave no trace behind when the Engine shuts down.