๐Ÿงฉ Singleton

Short Description

Attach singleton helpers to UObject classes via the GORGEOUS_ATTACH_SINGLETON macro. Provides scoped singleton lifetimes and simple accessors.

Long Description

UGorgeousSingleton implements a small, safe singleton registry for UObject-derived types. Use the GORGEOUS_ATTACH_SINGLETON(ClassType) macro in your class declaration to expose GetSingleton() and DestroySingleton() helpers. The system supports scoping (Global, World, GameInstance, Editor) and will create instances on first access.

๐Ÿš€ Features

  • Macro-based attachment: GORGEOUS_ATTACH_SINGLETON(ClassType).
  • Scoped singletons via EGorgeousSingletonScope (Global, World, GameInstance, Editor).
  • Template-safe helpers: GetSingleton<T>, DestroySingleton<T>, plus DestroyAllSingletons().

EGorgeousSingletonScope

Value Meaning
Global Single instance shared across the running process.
World Instance per UWorld. Requires a WorldContextObject to resolve.
GameInstance Instance scoped to the UGameInstance.
Editor Editor-scoped instance (editor-only use).

GORGEOUS_ATTACH_SINGLETON

Macro that injects GetSingleton(...) and DestroySingleton(...) static helpers into the declaring class. It expands to thin forwarding wrappers that call UGorgeousSingleton::GetSingleton<T>() / DestroySingleton<T>().

UCLASS()
class UMyService : public UObject
{
    GENERATED_BODY()
    GORGEOUS_ATTACH_SINGLETON(UMyService)
};

// Access the global singleton UMyService* S = UMyService::GetSingleton();

GetSingleton<T>

Template helper that returns the singleton instance for T in the requested Scope. If the instance does not exist it will be created. When using World or GameInstance scopes, pass a sensible WorldContextObject (an AActor, UObject with an outer world, etc.).

Parameter Name Type Description
WorldContextObject UObject* Optional context used to resolve world/game-instance singletons.
Scope EGorgeousSingletonScope Scope for the singleton instance.
// Global singleton
UMyService* GlobalSvc = UMyService::GetSingleton();

// World-scoped singleton from an actor UMyService* WorldSvc = UMyService::GetSingleton(GetWorld(), EGorgeousSingletonScope::World);

DestroySingleton<T>

Destroy the singleton instance for type T in the specified Scope. Use this when you need explicit teardown (editor tools, hot-reload paths, tests).

Parameter Name Type Description
WorldContextObject UObject* Optional context used to resolve world/game-instance singletons.
Scope EGorgeousSingletonScope Scope for the singleton instance to destroy.
// Destroy the global singleton
UMyService::DestroySingleton();

// Destroy a world-scoped singleton from an actor UMyService::DestroySingleton(GetWorld(), EGorgeousSingletonScope::World);

DestroyAllSingletons

Utility that destroys all registered singleton instances across all scopes. Intended for editor-level teardown and tests.

This function takes no parameters.

// Tear down every singleton instance
UGorgeousSingleton::DestroyAllSingletons();

GetOrCreateSingleton & DestroySingletonByClass

These internal helpers perform the actual lookup/creation and destruction logic. They are not part of the public per-type template API but are useful to read when debugging lifecycle or scope resolution.