Search in

Sort by:

Everything C++ Programming

Current Space

Question Status:

All Questions With Answer With Accepted Answer

Quick results

Search help

Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both.
Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.

You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with
each other. Examples

How do i create a class that only exists once?

0

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

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.