Skip to main content

Manual Scene Loading

If NetworkSceneManager doesn't work for your case you can control everything yourself.

Using Messages

These messages are built-in and used by NetworkSceneManager. If you are creating your own scene logic then you can re-use these messages for your purpose.

Loading a Scene

Server

  1. Mark Player as not ready, using NetworkPlayer.SceneIsReady
  2. Send SceneMessage to clients

If the scene is already loaded on server, skip steps 3/4. This might be the case if you are loading a scene for a player that joins late.

  1. Load the scene on server
  2. Call ServerObjectManager.SpawnSceneObjects (This will tell Mirage to call Spawn on unspawned Scene objects)

Client

After receiving SceneMessage

  1. (optional) Mark local player as not ready
  2. Load the scene on client

After loading finished

  1. Call ClientObjectManager.PrepareToSpawnSceneObjects (This will tell Mirage about any new scene objects)
  2. (optional) Mark local player as ready
  3. Send SceneReadyMessage to the server

Server

After receiving SceneReadyMessage

  1. Mark the player as ready using: player.SceneIsReady = true
  2. Call ServerObjectManager.SpawnVisibleObjects or ServerObjectManager.AddCharacter (Mirage will send spawn message to client)

SpawnVisibleObjects vs AddCharacter

When calling SpawnVisibleObjects it will only spawn objects if the player has a character. This check can be avoided by using the IgnoreHasCharacter argument.

When AddCharacter is called it will send a spawn message for the new character to the client. After that, it will call SpawnVisibleObjects to spawn any objects that are visible to the new character.

If your game has a player character you'll want to use AddCharacter most of the time. But if your game does not have a player character or you want to spawn objects earlier then you should use SpawnVisibleObjects with IgnoreHasCharacter set up true.

You can also use SpawnVisibleObjects(player, true) to spawn scene objects before the player character by calling it before AddCharacter.

note

Make sure to call ClientObjectManager.PrepareToSpawnSceneObjects client side before calling SpawnVisibleObjects or AddCharacter. If that function is not called the client will not be able to find scene objects when spawn messages are received.

Host mode

If using this setup in Host mode make sure you only load the Scene once, this can be done by checking if the server is active before loading the scene on the client.

The rest of the setup should stay the same. In host mode, there will be 2 copies of the NetworkPlayer one for the client-side and one for the server-side. When using player.SceneIsReady you will need to make sure you are setting it on both copies of the player. The easiest way to do this is just to treat the host client as a normal client and send the message, but be aware of any functions you don't want to be called twice.