x

Search in
Sort by:

Question Status:

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
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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.

Product Version: Not Selected
Tags:
more ▼

asked Mar 01 '19 at 12:51 PM in C++ Programming

avatar image

EstherZuidgeest
48 1 6 11

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

3 answers: sort voted first

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

answered Mar 01 '19 at 05:06 PM

avatar image

BrunoCaxito
190 2 7 6

avatar image EstherZuidgeest Mar 04 '19 at 11:16 AM

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?

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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()

more ▼

answered Mar 01 '19 at 10:38 PM

avatar image

madturtleGGinin
346 1 4 4

avatar image EstherZuidgeest Mar 04 '19 at 11:16 AM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

You can implement Singleton pattern in native C++ if you don't want to reference engine code. https://en.wikipedia.org/wiki/Singleton_pattern

 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.

more ▼

answered Mar 04 '19 at 12:31 PM

avatar image

madturtleGGinin
346 1 4 4

avatar image EstherZuidgeest Mar 04 '19 at 03:28 PM

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

thread: https://answers.unrealengine.com/questions/812807/singleton-pattern-in-c.html

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question