🌐Singletons
Last updated
Last updated
A singleton is a server-authoritative MorpheusActor
which is spawned exactly once, when the server starts up. A singleton can replicate data to game clients, and game clients can send data to the server with server RPCs.
Singletons are useful for the common case where you want to put a single easily-accessed object in charge of some behaviour. If no replication is required, you could handle this with server-only logic on a subsystem. However, if you want to communicate between the client and server, you need a MorpheusActor
, so you need to use a singleton.
To make a singleton, create a new blueprint class from a MorpheusActor
.
In the World Settings panel, locate the Singletons
section, and add your new class to the Additional Singletons
array.
If you want this singleton behaviour but for non-Morpheus Actors, use the Non Morpheus Singletons
array. Disconnected instances of your class will be spawned on the server and all clients. (This is similar to a subsystem, but preferable if you want control over which levels the object is present in.)
Singletons do not spawn automatically during automated tests. Manually spawn the required Singletons when creating automated tests that rely on them.
The easiest way to get your singleton actor is using the GetActorOfClass
method.
This will fail (return null) if called before the singleton has been spawned.
To defer until your singleton is available, you can use the bootflow subsystem's function HandleCommonActorsSpawned
in your actor's BeginPlay
/MorpheusBeginPlay
:
Alternatively, you can use the global function GetSingletonOnceAvailable
, specifying the SingletonClass
.
The result will be a base Actor
, so you will need to cast to the class you need.