/ActorSingleton

Small code plugin for Unreal 5 which adds inheritable Actor class that can be spawned only once within same World context.

Primary LanguageC++MIT LicenseMIT

ActorSingleton

Tiny UE5 plugin that adds AActorSingleton class.

Install like every other plugin, by cloning into project's Plugins directory.

For context see the information below or read the source code.

About

This plugin has been created for my personal use, but I decided to publish it, since it has proven to be somehow usefull.

Imagine the following case: you need an UObject that lives within UWorld, there can be only one spawned instance of said object and it must be accessible from the Editor's Level Viewport. Basically, something like "Singleton".

So what options do we have? Unreal Engine has few ways of creating singletons, the best out of them is creating a class derived from USubsytem. The one that suits most of our needs is UWorldSubsystem, but said class isn't spawnable and won't appear in the Level Viewport, so it doesn't fully fit our needs.

The other option would be creating an AActor. Actors are easily accessible from Level Viewport. However, how do we ensure that there is only one spawned instance of said Actor?...

...and this is why this plugin exists. It introduces AActorSingleton class. If you try to spawn multiple instances of said class, it will only allow to spawn the first instance and will automaticly destroy the rest.

Usage

Derive from AActorSingleton and make sure that ActorSingleton::IsFinalParent returns true for your class.

Whenever you try to spawn a duplicate instace, you will get a meaningfull error about it. If you try to do this by placing an Actor to the Level Viewport you will even get a clear popup message:

image

Tested on Linux with UE 5.3.2 and clang