# Introduction to Morpheus Networking

## What is Morpheus Networking? <a href="#networking-actorreplication" id="networking-actorreplication"></a>

In MSquared, we replace Unreal's standard networking with our own custom implementation, called Morpheus.

Morpheus is the technology that enables thousands of players and objects to be networked together in the same space, in real time, whilst using the bandwidth of a standard battle royale game.

The fundamental concepts of Morpheus networking are the same as [in Unreal](https://dev.epicgames.com/documentation/en-us/unreal-engine/networking-and-multiplayer-in-unreal-engine):

* A single server coordinates multiple clients
* The basic unit of networking is the actor
* Actors are synchronised ("replicated") between server and client using two mechanisms:
  * Variable replication
  * RPCs

This document assumes a rough familiarity with these concepts, and focuses on Morpheus's essential differences from Unreal.

## Morpheus Actors <a href="#networking-actorreplication" id="networking-actorreplication"></a>

In a Morpheus game, only actors that inherit from `AMorpheusActor` are networked. All other actor types are local-only. If you spawn an `AMorpheusActor` on the server, it will be replicated to all clients immediately. `AMorpheusActors` should only be destroyed on the server, and the actor's destruction will also be replicated to all clients immediately.

Each client sees every `AMorpheusActor` in the world; there is no concept of 'net relevancy' or 'checking in and out' an actor.

## Changes to Core Unreal Classes

Since only Morpheus Actors are networked, a number of the core Unreal classes behave slightly differently when using Morpheus networking:

* `GameMode` - Functionally the same as native Unreal, except that it adds a default Morpheus Actor class, similar to the `Default Pawn Class`, and it replaces the normal `GameState` with a `MorpheusGameState`. \*

  ```
  <figure><img src="../../../../.gitbook/assets/image (1426).png" alt=""><figcaption></figcaption></figure>
  ```

  * Note that the `GameMode` is also created on the local client, rather than exclusively existing on the server, since a client using Morpheus networking is configured to be a "standalone" game, rather than a "client", since it does not use native Unreal networking.
* `GameInstance` - same as in native Unreal networking. It exists on all machines, but is not networked.
  * NOTE: This cannot be modified by downstream projects. Due to our [Worlds](/creation/worlds.md) setup where a base Morpheus Platform build is launched, and user content is loaded in as a Mod, the game instance used will be the MSquared default, not what is provided by the user's content. This is required to support interoperability between different worlds built on the same platform.
* `GameState` - Not used in Morpheus networking. Replaced with a replicated `MorpheusGameState` (a Morpheus Actor that is replicated, and can be used functionally like a `GameState`.)
* `GameSession` - Not used in Morpheus networking. We handle online services and player connection separately.
* `PlayerController` - This is only present on the local client's machine. It is not replicated, and does not exist on the server.
* `PlayerState` - This is not networked. It is largely unused and untested with Morpheus networking - if you want replicated player data, you can use the Morpheus Actor.
* `Pawn`/`Character` - This is not networked. For the local player, this works the same as native local-only games. For other players, their in-game representation is determined by their morpheus actor's render target. (See [Morpheus Render Targets](/creation/unreal-development/getting-started/networking/morpheus-render-targets.md)).
  * If other players' characters are represented with actors, they are also subject to pooling (see [Actor Pooling](/creation/unreal-development/features-and-tutorials/actor-pooling.md))

## Replication

### Network Levels <a href="#networking-networklevels" id="networking-networklevels"></a>

Although every client sees every `AMorpheusActor` in the world, it may not hold the full state for all of them. On each client, each `AMorpheusActor` is present at one of three network levels:

* Background
* Midground
* Foreground

Only actors in the foreground are fully replicated.

For a detailed description of how network levels work, see [Network Levels](/creation/unreal-development/getting-started/networking/network-levels.md).

### Authority

Unlike in standard Unreal, a `AMorpheusActor` may have an **authoritative client**. This is an immutable property assigned when the actor is spawned. An actor's client authority affects both variable replication and RPCs.

(For how to assign a client authority to an actor, see "Spawning and Destroying" below.)

### Variable Replication <a href="#networking-properties" id="networking-properties"></a>

In Morpheus, you can mark blueprint variables for replication the same way you would in Unreal. However, **existing Unreal replication features such as RepNotify behave differently in Morpheus**. For more information on how these features work and how to apply them in your variable definitions, see [Replicated Properties](/creation/unreal-development/getting-started/networking/replicated-properties.md).

### RPCs <a href="#networking-rpcs" id="networking-rpcs"></a>

Morpheus supports the three main types of Unreal RPC: `Server`, `Client` and `NetMulticast`. However, the semantics of these differ from standard Unreal networking. For further information, see [RPCs](/creation/unreal-development/getting-started/networking/morpheus-rpcs.md).

## Morpheus Actor Components <a href="#networking-morpheusactorcomponents" id="networking-morpheusactorcomponents"></a>

In order to be networked, a component must:

* Inherit from `UMorpheusActorComponent`
* Belong to an `AMorpheusActor`
* Be created in its owning actor's constructor, using `CreateDefaultSubobject`. **Components added to existing actors won't be replicated.**

Authority and network level are delegated to the component's owning actor.

Variable replication and RPCs work the same way as for `AMorpheusActor`.

## Spawning and Destroying <a href="#networking-spawninganddestroying" id="networking-spawninganddestroying"></a>

### Spawning an `AMorpheusActor` <a href="#networking-spawning" id="networking-spawning"></a>

**Only the server** can spawn replicated `AMorpheusActor`s. You can spawn them from server blueprints, using Unreal's standard `SpawnActor` functions. In addition, Net Startup Actors that are `AMorpheusActors` will be automatically spawned and replicated when their level starts up or their sublevel streams in.

By default, an `AMorpheusActor` has server authority. To spawn an `AMorpheusActor` with client authority, you must call `SpawnMorpheusActorWithClientAuthority` **on the server**. You identify the authoritative client by passing in an `AMorpheusClientConnection`; this is typically obtained by calling the `AMorpheusActor` function `ServerGetRpcCallerConnection` from within the handler for a `Server` RPC.

Morpheus does not support authority transfer. If a client disconnects, the server destroys any `AMorpheusActor`s which that client had authority over.

### Destroying an `AMorpheusActor` <a href="#networking-destroyinganamorpheusactor" id="networking-destroyinganamorpheusactor"></a>

Only the server can destroy replicated `AMorpheusActor`s, using the standard Unreal `Destroy` functions.

### Lifecycle Events <a href="#networking-lifecycleevents" id="networking-lifecycleevents"></a>

For `AMorpheusActor`s and `UMorpheusActorComponent`s, there's a single change to the standard [Unreal actor life cycle](https://dev.epicgames.com/documentation/en-us/unreal-engine/unreal-engine-actor-lifecycle). Instead of the standard `BeginPlay` event, you need to use `MorpheusBeginPlay`. (`BeginPlay` still exists, but shouldn't be used - it will print warnings in the blueprint compiler if you do use it.)

The call to `MorpheusBeginPlay` marks the point at which networking functionality becomes available. RPCs sent from within `MorpheusBeginPlay` are queued up and sent later, once the actor has completed initialization.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.msquared.io/creation/unreal-development/getting-started/networking/networking.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
