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"

Plugin : Initialize protected, persistent object

Hello,

I'm a Games Engineer student and currently developing a plugin. The plugin will be used through a BPFunctionLibrary. For the plugin I'll need an object (let's call it Watcher), that is created on Plugin StartUpModule() and can be accessed by all classes inside my plugin module. It's supposed to hold values like position, materials etc.

My first idea was to declare it inside my PluginModule.h and use the StartUpModule() method to create an instance of it. Since the StartUpModule() is called once, Watcher would be a Singleton as intended.

 #pragma once
 
 #include "ModuleManager.h"
 #include "Watcher.h"
 
 class FEasyFootPrintsModule : public IModuleInterface
 {
 public:
      UWatcher* watcher;
      UWatcher* getWatcher() { return watcher; }

     /** IModuleInterface implementation */
     virtual void StartupModule() override;
     virtual void ShutdownModule() override;
 };

Inside the cpp:

 void FEasyFootPrintsModule::StartupModule()
 {
     watcher = NewObject<UWatcher>();
 }

Inside my functions of the BPFunctionLibrary.cpp I'd like to have access to the Watcher object to define different behaviour based on the values inside Watcher. Since Watcher is a member variable of my PluginModule, I need a reference to it. Where do I get it?

Or is there a better solution?

Product Version: UE 4.17
Tags:
more ▼

asked Nov 15 '17 at 03:26 PM in C++ Programming

avatar image

kosmopilot
22 4 6 8

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

2 answers: sort voted first

My Solution was a Singleton, accessed through a static method

 UWatcher * UWatcher::GetInstance()
 {
     static UWatcher* i = 0;
     if (!i) {
         i = NewObject<UWatcher>();
         i->AddToRoot();
     }
     return i;
 }

more ▼

answered Nov 15 '17 at 09:41 PM

avatar image

kosmopilot
22 4 6 8

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

First of all insted of UWatcher* use WeakObjectPtr<UWatcher> or else you risking invalid pointer, also read here how to keep this object alive and prevent it from garbage collection:

https://wiki.unrealengine.com/Garbage_Collection_%26_Dynamic_Memory_Allocation

As main issue, you eather need to get module class object form module manager:

https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Modules/FModuleManager/index.html

Or create singleton in your module class so you can direcly refrence it, you cna look up in other modules in engine to see how it works, here example of HTTP:

https://github.com/EpicGames/UnrealEngine/blob/76085d1106078d8988e4404391428252ba1eb9a7/Engine/Source/Runtime/Online/HTTP/Private/HttpModule.cpp

https://github.com/EpicGames/UnrealEngine/blob/76085d1106078d8988e4404391428252ba1eb9a7/Engine/Source/Runtime/Online/HTTP/Public/HttpModule.h

  HTTP_API static FHttpModule& Get();

 /** singleton for the module while loaded and available */
 static FHttpModule* Singleton;

and in CPP

 FHttpModule& FHttpModule::Get()
 {
     if (Singleton == NULL)
     {
         check(IsInGameThread());
         FModuleManager::LoadModuleChecked<FHttpModule>("HTTP");
     }
     check(Singleton != NULL);
     return *Singleton;
 }

I know you plan to do so with UObject, but i'm not sure if that is safe thing to do, so i recomand you to it with module class as most module classes in engine do.

more ▼

answered Nov 15 '17 at 09:29 PM

avatar image

Shadowriver
37.8k 937 172 1123

(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