How can I create a Blueprint singleton object?

How I can create an object in the Blueprint so that it was singleton and was available in different worlds and maps?
Thank you.

In the construction script, you can use an iterator to check the world for other instances of the same class, and prevent the new one from being created.

This isn’t ideal though. You’re probably better off handling your singleton in native code (then implementing a BP on top of that) unless someone has a better solution.

You can use GameSingletonClassName in the project settings or ini file, it’s on Engine.h, if you set that to the blueprint you want it will make an instance for you that is globally available.

There’s currently no way to access it directly from blueprints, but you can write a trivial blueprint function in C++ to expose it if you want.

1 Like

That’s what I need. Thank you.

But you can’t implement AActor, because it use Level object to register Tick function. Work example:

Header file:

UCLASS(Blueprintable)
class USingletonBP: public UObject
{
	GENERATED_UCLASS_BODY()

	/**
	* Singleton access 
	*/
	static USingletonBP* Get();
};

Code file:

USingletonBP* USingletonBP::Get()
{
	static USingletonBP* Singleton;
	if (!Singleton || !IsValid(Singleton))
	{
		UClass *SingletonClass = LoadClass<UObject>(NULL, TEXT("/Script/ModuleName.SingletonBP"), NULL, LOAD_None, NULL);
		Singleton = (USingletonBP*) ConstructObject<UObject>(SingletonClass);
	}

	return Singleton;
}

Was looking up how to create a singleton class and stumbled upon this. I implemented this in my game, but it keeps crashing on start up. Here’s my code:

UClock* UClock::getInstance()
{
static UClock* clock;
if (!clock || !IsValid(clock))
{
UClass SingletonClass = LoadClass(NULL, TEXT("/Script/ModuleName.Clock"), NULL, LOAD_None, NULL);
clock = (UClock
)ConstructObject(SingletonClass);
}
return clock;
}

So in my level blueprint, i have a variable, Clock, that i then use a Set Node connected to getInstance() function. I get an error stating that ConstructObject used a NULL object, or something. Any idea?

edit: Sorry, looks like code doesn’t work in the comments section.

I would suggest trying out my solution above, it avoids a lot of the issues from trying to load an object at runtime, if you have the engine do it for you it always works.

1 Like

Hello. There is still no way to access Singleton class from BP without any C++ modifications?