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
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.