Singletons

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.

How are they set up?

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.

Additional Singletons

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.)

Non Morpheus Singletons

How to access your singleton

  • 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:

This example will cache the singleton of type BPM_VendingMachineManager once it has been spawned. Other bootflow steps after HandleCommonActorsSpawned would also work here, e.g. HandleBootflowFinished.
  • 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.

Last updated

Was this helpful?