How do i create a class that only exists once?

I am creating a custom component that needs user typed text input.
To handle this input i want to create a class named InputHandler that inherits from UEditableText (Which is a widget text input box).
Ideally I want all my components to listen to this one InputHandler.
How do I set up a class in a way that there exists only one instance of it and any component can easily find it?

EDIT
I forgot to mention I am making a plugin, so I would like my code to be as standalone as possible. Is there a way to make a static class or a normal class with a static function so it can easily be referenced by other classes? So i can just use GetInputHandler like you would GetPlayerController.

You can use the Level Script, GameMode, GameInstance, PlayerController, etc. to create a single InputHandler in its Constructor or OnConstruct method.

The InputHandler class can check for other instances and complain about it, or even destroy itself or others.

if (this->GetWorld())
{
    TArray<AActor*> InputHandlers;

    // Get all input handlers.
    UGameplayStatics::GetAllActorsOfClass(
       this->GetWorld(),
       InputHandler::StaticClass(),
       OUT InputHandlers
    );

    // Check if there is more than one input handler.
    if (InputHandlers.Num() > 1)
    {
        // Do something
    }
}

GameInstance is the only object that guarantee to exist throughout a game session.

Other options like GameMode, PlayerController get destroyed and recreated when level changes.

Both options can fit your requirement based on the use case. You can create a subclass of one of these class (Ex: MyGameInstance) and put your InputHandler as a variable there.

To access InputHandler from any Actor, simply call

GetGameInstance<UMyGameInstance>() -> GetInputHandler()

I forgot to mention I am making a plugin, so I would like my code to be as standalone as possible. Is there a way to make a static class or a normal class with a static function so it can easily be referenced by other classes? So i can just use GetInputHandler like you would GetPlayerController?

I forgot to mention I am making a plugin, so I would like my code to be as standalone as possible. Is there a way to make a static class or a normal class with a static function so it can easily be referenced by other classes? So i can just use GetInputHandler like you would GetPlayerController.

You can implement Singleton pattern in native C++ if you don’t want to reference engine code. Singleton pattern - Wikipedia

class InputManager
{
public: 
  static InputManager* GetInstance() { 
    if (Instance == nullptr) {
      Instance = new InputManager();
    }
    return Instance;
  }

private: 
  static InputManager* Instance;
}

Then you can call InputManager::GetInstance() to access this singleton instance from any other classes.

I’ve looked into the singleton suggestion. Many people advice to use the ue4 framework suggestion instead of using a native singleton.

thread: