How to Change Scenes Preserving Replicas¶
Games will often move between different scenes as gameplay progresses.
Strix server and room connections are separate from the game world and current scene. However, changes to the scene may destroy existing replicas and have an effect on multiplayer synchronization.
To prevent Strix from automatically destroying the network objects representing replicas during a scene change, Strix provides a way of Pausing replication and re-enabling it later.
Problem¶
Suppose you are playing an RPG as a party member, and the party enters a dungeon. On each of the game clients of all party members, the new scene (for the dungeon entrance) will be loaded. The exact timing of the scene changes may vary on different clients, and it is possible that the fastest client may start replicating objects from the new scene to other clients, before some of the other clients leaves the old scene.
If it happened, the replicas are instantiated in the old scene on the slower client, and, as it loads the new scene, those replicas are destroyed.
You can solve such issues by pausing the replication.
Solution¶
Pausing¶
To pause the creation/deletion of network objects in the world,
set the autoInstantiateReplica
property value of the StrixNetwork
singleton
to false.
StrixNetwork.instance.autoInstantiateReplica = false;
Changing Scene¶
After all your party members paused the replication, load the new scene.
SceneManager.LoadScene("OtherSceneName");
Unpausing¶
On the new scene, unpause the replication.
Unpausing implies a need to reinstantiate the replicas that were deleted,
which is done with the RespawnReplicas
method.
Also, as we are now unpaused, we can set autoInstantiateReplica
to true.
StrixNetwork.instance.RespawnReplicas();
StrixNetwork.instance.autoInstantiateReplica = true;