🌐Singletons
Last updated
Last updated
A singleton is a single-instance, server-authoritative Actor. When a server starts up, each singleton is spawned exactly one. As a server authoritative Actor, singletons can replicate data to game clients and game clients can send data to the server with server RPCs.
Singletons solve a common problem where we want to control/perform functionality for a world in a single location. If no replication is required, we could handle this with subsystems. However, if we want to communicate between the client and server, we need to use a singleton.
To create a new 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
field.
If you want this singleton behaviour but for non-Morpheus Actors
, e.g. if you want blueprint overridable common functionality, you can control which maps it is present on, use the additional NonMorpheusSingletons
array. These will similarly be spawned on both the server and all clients
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 too early (e.g. if the singleton hasn't been spawned yet). To get around this, you can use the bootflow subsystem's HandleCommonActorsSpawned
in your actor's BeginPlay
/MorpheusBeginPlay
as follows:
Note: We also have a GetSingletonOnceAvailable
method that can be used to instead of the bootflow approach, but this only works for singletons that have been explicitly hooked up to support it in code, e.g. the MMLSpawnerSingleton
. Otherwise, it will fail. For more information on MML spawning see here.
If being called on a valid singleton class, in the blueprint you want to get your singleton in, use the Get Singleton Once Available
node, specifying the SingletonClass
.
The result will be a base Actor
, so you will need to cast to the class you need