Server & Client Worlds

DOTSNET creates a ServerWorld and a ClientWorld when starting:

ConvertToNetworkEntity

ECS comes with a ConvertToEntity component that allows us to convert GameObjects to Entities in the Default World.

DOTSNET comes with a ConvertToNetworkEntity component that converts GameObjects to Entities in the Server/ClientWorld:

Most entities will live in both worlds.

For example, a monster would live in both worlds so that the server can move it, then sync the position to the same entity in the client world.

World Attributes

You can put a system into the server world by using the [ServerWorld] attribute:

using DOTSNET;

[ServerWorld]
public class ServerSystem : SystemBase
{
}

You can put a system into the client world by using the [ClientWorld] attribute:

using DOTSNET;

[ClientWorld]
public class ClientSystem : ComponentSystem
{
}

A system can also be in both worlds:

using DOTSNET;

[ServerWorld, ClientWorld]
public class ServerAndClientSystem : ComponentSystem
{
}

If a system has no attributes, then it’s only in the DefaultWorld.

DOTSNET entirely operates in the Server/Client world, not in the DefaultWorld.

Simulation System Groups

For convenience, DOTSNET comes with custom simulation system groups.

On the server, you can put a system into the ServerActiveSimulationSystemGroup, so it’s only updated while the server is active (after NetworkServerSystem.StartServer was called, until StopServer is called). For example, monster movement should only be updated while the server is actually active:

using DOTSNET;

[ServerWorld]
[UpdateInGroup(typeof(ServerActiveSimulationSystemGroup))]
public class MonsterMovementSystem : ComponentSystem
{
}

On the client, you can put a system into the ClientConnectedSimulationSystemGroup, so it’s only updated while the client is connected to the server (after a successful NetworkClientSystem.Connect until Disconnect). For example, local player movement should only be updated while the client is actually connected:

using DOTSNET;

[ClientWorld]
[UpdateInGroup(typeof(ClientConnectedSimulationSystemGroup))]
public class LocalPlayerMovementSystem : ComponentSystem
{
}

Accessing Server/ClientWorld from the Outside

Use the static Bootstrap class to access Server/Client World systems from anywhere in Unity:

using DOTSNET;

public class SomeClass
{
    void Example()
    {
        // log the names
        Debug.Log(Bootstrap.ClientWorld.Name);
        Debug.Log(Bootstrap.ServerWorld.Name);

        // find the server/client systems
        Bootstrap.ServerWorld.GetExistingSystem<NetworkServerSystem>();
        Bootstrap.ClientWorld.GetExistingSystem<NetworkClientSystem>();
    }
}

Last updated