I have two scenes in my simple project. A MainMenu scene containing a very simple play button and a Game scene made by following the “Networked Cube” guide in the Getting Started page in the documentation Networked Cube | Netcode for Entities | 1.0.17
When I press the escape button in the Game scene it goes back to the MainMenu scene where I can press the play button again to go to the Game scene. Back and forth. I want to properly shut down both the client and the server, totally reset everything, between those scene switches. From the starting point of having finished the networked cube example, how do I achieve such a flow?
I think I’ve figured it out with the help of https://github.com/Unity-Technologies/multiplayer/tree/master/sampleproject/Assets/Samples/BootstrapAndFrontend
public class GameBootstrap : ClientServerBootstrap
{
public static World ServerWorld;
public static World ClientWorld;
public static World LocalWorld;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void Init()
{
ServerWorld = null;
ClientWorld = null;
LocalWorld = null;
}
public static void Start()
{
ServerWorld = CreateServerWorld("ServerWorld");
ClientWorld = CreateClientWorld("ClientWorld");
NetworkEndpoint ep = NetworkEndpoint.AnyIpv4.WithPort(2222);
{
using var drvQuery = ServerWorld.EntityManager.CreateEntityQuery(ComponentType.ReadWrite<NetworkStreamDriver>());
drvQuery.GetSingletonRW<NetworkStreamDriver>().ValueRW.Listen(ep);
}
ep = NetworkEndpoint.LoopbackIpv4.WithPort(2222);
{
using var drvQuery = ClientWorld.EntityManager.CreateEntityQuery(ComponentType.ReadWrite<NetworkStreamDriver>());
drvQuery.GetSingletonRW<NetworkStreamDriver>().ValueRW.Connect(ClientWorld.EntityManager, ep);
}
}
public static void Stop()
{
ServerWorld.Dispose();
ClientWorld.Dispose();
ServerWorld = null;
ClientWorld = null;
}
public override bool Initialize(string defaultWorldName)
{
AutoConnectPort = 0;
LocalWorld = CreateLocalWorld(defaultWorldName);
return true;
}
}
Hey PolarTron! This looks correct at first glance. LMK if you run into problems here.
1 Like
I’ve been digging through the samples, tutorials and such and I’m at the same point where you’re at. The thing I don’t understand is what is the purpose of the LocalWorld if all of the systems would be added to the Client/Server worlds?
I haven’t gotten to that point yet but on the client I’m going to add all my menu navigation logic and data to the LocalWorld. I will only create the ClientWorld when I click the “Connect” button in my server browser and dispose of it when I disconnect.
On the server I will only use ServerWorld on the standalone server build.