# Plugin : Initialize protected, persistent object

**URL:** <https://forums.unrealengine.com/t/plugin-initialize-protected-persistent-object/409707>\
**Category:** Plugins\
**Tags:** unreal-engine, global, Plugins, question, CPP\
**Created:** [November 15, 2017, 3:26pm UTC](https://forums.unrealengine.com/t/plugin-initialize-protected-persistent-object/409707 "2017-11-15T15:26:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_a95c0d08](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a95c0d08](https://forums.unrealengine.com/u/anonymous_user_a95c0d08)\
**Post date:** [November 15, 2017, 3:26pm UTC](https://forums.unrealengine.com/t/plugin-initialize-protected-persistent-object/409707/1 "2017-11-15T15:26:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [November 15, 2017, 9:29pm UTC](https://forums.unrealengine.com/t/plugin-initialize-protected-persistent-object/409707/2 "2017-11-15T21:29:07Z")

</div>

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:

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — https://unrealcommunity.wiki/! You will be able to find content from...

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

> **[FModuleManager](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Core/Modules/FModuleManager/)**
>
> Implements the module manager.

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/Private/HttpModule.cpp)

[https://github.com/EpicGames/UnrealEngine/blob/76085d1106078d8988e4404391428252ba1eb9a7/Engine/Source/Runtime/Online/HTTP/Public/HttpModule.h](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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a95c0d08](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a95c0d08](https://forums.unrealengine.com/u/anonymous_user_a95c0d08)\
**Post date:** [November 15, 2017, 9:41pm UTC](https://forums.unrealengine.com/t/plugin-initialize-protected-persistent-object/409707/3 "2017-11-15T21:41:50Z")

</div>

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

```
