This sample contains a demonstration for how to manage level synchronization between server and clients when using the netcode package.
- The main module is the NetcodeLevelSync.cs file which implements the networked level sync flow. It does no scene load/unloading itself as that is very use case specific.
- The LevelSync_Bootstrap scene contains a subscene based level flow which demonstrates how to make use of the networked sync module.
- It has a UI which makes use of the LevelManager.cs as entry point.
- It uses LevelTracker* systems to handle loading/unloading routines.
NetcodeLevelSync implements the following flow:
- Server initiates level loading
- Disable ghost sync (NetworkStreamInGame on all connections)
- Unload current level and start loading the new level
- Send RPC command to connected clients telling them to also switch levels
- When client receives the RPC command
- Disable ghost sync (NetworkStreamInGame on server connection)
- Unload current level and start loading next one
- When done loading send RPC to server signalling you are ready
- When server has received ready message from all clients and is himself done loading
- Enable ghost sync (NetworkStreamInGame on all connections)
NetcodeLevelSync is controlled through LevelSyncStateComponent data.
public struct LevelSyncStateComponent : IComponentData
{
public LevelSyncState State;
public int CurrentLevel;
public int NextLevel;
}
The valid level sync states are
public enum LevelSyncState
{
Idle,
LevelLoadRequest,
LevelLoadInProgress,
LevelLoaded
}
- Normally the state will be Idle.
- When server starts level loading he should place the state in LevelLoadInProgress
- When a command to load a new level has arrived the client will see the LevelLoadRequest and needs to react to that by starting the level load routine himself and setting state to LevelLoadInProgress.
- When either server or client are done loading they should set the state to LevelLoaded
- State will go back to Idle when processing is done.
A good way to see the flow more clearly is to have a look at the test for it, as it goes though all the steps in a simple way in one file. See SceneAutomationSystems.cs