How to use multiple singleton classes in one project?

Hi,

I’d like to have multiple Singleton classes in my Project and don’t see a way of adding it. The reason for doing such a stupid thing like having multiple global objects is as follows. I created a plugin to handle all my Inventory stuff using a datatable reference in the singleton for data driven item declaration. Whenever I need my Inventory Components available I could just load that Plugin and add the Inventory Component and the Inventory UI Component to my Game Character and HUD.

The Problem is there is a C++ written Crafting Component as well as other Components that use Singleton Datatable References.

In order make this work I need the Singletons available somehow.
At the moment I use GEngine->GameSingleton to get the Singleton and then cast it to whatever it may be. So something with inheritance would work, but I don’t get inheritance cross Plugin/Game to work. I think I might be impossible in C++ ? In Blueprints it obviously works, I don’t see a reason why it won’t work.

Another solution would be, to create a Plugin that could append the GameSingleton functionality with custom Class Names, like InventorySingleton, CraftingSingleton and so on grabable with GEngine->InventorySingleton. Is this even possible?

I hope someone finds this interesting or can help me with this issue.

Thanks for reading,

Mario

You could build a singleton manager as a static blueprint function library. Include a static variable that holds your singleton object, and create a static function that retrieves that object when needed and instantiates it if it doesn’t yet exist.

//MyCustomSingletonManager.h
UCLASS()
class MYPLUGIN_API UMyCustomSingletonManager : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

private:

	  static UMyCustomSingleton* MyCustomSingletonObj;

public:

    /** Retrieves the custom singleton object; spawns new singleton if doesn't already exist */
    UFUNCTION(BlueprintCallable, Category="Singleton")
    static UMyCustomSingleton* GetMyCustomSingleton();

};


//MyCustomSingletonManager.cpp

UMyCustomSingleton* UMyCustomSingletonManager::MyCustomSingletonObj(NULL);

UMyCustomSingleton* UMyCustomSingletonManager::GetMyCustomSingleton() {
    if (!MyCustomSingletonObj) {
        MyCustomSingletonObj = NewObject<UMyCustomSingleton>();

    }
    return MyCustomSingletonObj;

}

You could have a separate manager for each of your singletons, or one manager that holds multiple, different singletons of different classes. These aren’t true singletons, the compiler will still allow multiple instances to exist; but you’ll get the functionality you need.

You could also just build your singleton as a collection of static variables and functions, depending on what you are doing with it. You could make your datatable reference a static variable if you will always want it to exist in memory.

Your best bet might be to build your unique objects into a custom game instance. Your game code would then be dependent on the plugin code, but it sounds like it’s going to be anyway. The game instance is available globaly via UGameplayStatics, and then you can just reference each of your “singleton” objects via pointers held in it.

//MyCustomGameInstance.h
UCLASS()
class MYGAME_API UMyGameInstance : public UGameInstance
{
	GENERATED_BODY()

  /** Globably available utility */
  UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Singleton Utilities")
  UMyCustomSingleton* MyCustomSingletonObj;
  
  UMyGameInstance();

};


//MyCustomGameInstance.cpp

UMyGameInstance::UMyGameInstance() {
  MyCustomSingletonObj = CreateDefaultSubObject<UMyCustomSingleton>();

}