Skip to content

Simple implementation of a Dependency Injector Plugin for Unreal Engine. Uses a Singleton instance to hold the registered objects.

License

Notifications You must be signed in to change notification settings

JDSherbert/Unreal-Engine-Dependency-Injection-Plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 

Repository files navigation

image

Unreal Engine Dependency Injection Plugin

Stars Badge Forks Badge Watchers Badge Issues Badge


Unreal Engine Plugin Available on Itch.io License




Overview

Dependency Injection (DI) is a design pattern widely used in software development to achieve a more flexible and maintainable codebase. In essence, dependency injection involves providing a component with its dependencies rather than allowing it to create them. This pattern is particularly prevalent in object-oriented programming and is beneficial for several reasons:

Decoupling and Separation of Concerns:

Dependency Injection helps to decouple components, reducing the interdependence between different parts of the system. It allows for a clear separation of concerns, making individual components focus on their specific responsibilities without having to worry about the creation and management of their dependencies.

Testability:

One of the significant advantages of DI is improved testability. By injecting dependencies, it becomes easier to substitute real implementations with mock or fake objects during unit testing. Testability is enhanced because you can isolate and test individual components without dealing with their concrete dependencies.

Flexibility and Reusability:

DI promotes flexibility by making it easier to change or extend the behavior of a system. Components become more modular and can be replaced or upgraded without affecting the entire system. Reusability is increased as components become less tightly coupled, making it possible to reuse them in different contexts or projects.

Maintainability and Readability:

Code maintainability improves as dependencies are explicitly defined and managed. This makes it easier for developers to understand the codebase and make changes without introducing unexpected side effects. The code becomes more readable because the dependencies required by a component are explicitly declared, providing a clear contract for how the component should be used.

Easier Integration and Collaboration:

DI facilitates the integration of third-party components or libraries. Components can be easily integrated by injecting their dependencies, reducing the effort required for collaboration between different parts of a system. It also aids in collaborating on large codebases as teams can work more independently on isolated components.

Centralized Configuration and Control:

DI allows for centralized control over the configuration and creation of components. Dependency injection containers or frameworks are often used to manage the creation and resolution of dependencies. Centralized control simplifies the configuration of the application and makes it easier to manage the lifecycle of components. In practice, dependency injection is implemented in various ways, including constructor injection, method injection, and property injection


Usage

To use the plugin:

  1. Copy the DependencyInjectorPlugin folder into your Unreal Engine project's Plugins directory.
  2. Enable the plugin in your project:
    • Open your Unreal Engine project.
    • Go to Edit > Plugins.
    • Find the DependencyInjectorPlugin in the list and enable it.
    • Restart Unreal Engine if prompted.
  3. Open your IDE.
  4. Start writing some code!

You can use the injector in C++ as follows:

//.h
#pragma once

#include "CoreMinimal.h"

/**
 * Sample class to be registered with the Dependency Injector
 */
class YOURPROJECT_API MyClass
{
public:
    MyClass();
    ~MyClass();

    void MyFunction();
};
//.cpp
// MyClass.cpp

#include "MyClass.h"

MyClass::MyClass()
{
    // Constructor logic

    // Registering this instance with the Dependency Injector
    UDependencyInjector::RegisterObject(this);
}

MyClass::~MyClass()
{
    // Unregistering this instance from the Dependency Injector
  // (note that this is important to do if not using UObjects, due to Garbage Collection antics)
    UDependencyInjector::UnregisterObject(this);
}

void MyClass::MyFunction()
{
    // Function logic
}