Sites

Menu

ECS: Worker entity

Before reading this document, make sure you have read:

Each worker has an ECS world to represent the entities currently in a worker’s view. This world contains a worker entity, which can be uniquely identified by the WorkerEntityTag component attached to it.

The worker entity enables you to react to changes in the connection to the Runtime. That is, whether the worker is connected to the Runtime or not.

How to run logic when the worker has just connected

The GDK adds the OnConnected temporary component to your worker entity when it has just connected.

Example usage

public class HandleConnectSystem : ComponentSystem
{
    private EntityQuery query;

    protected override void OnCreate()
    {
        base.OnCreate();

        query = GetEntityQuery(
            ComponentType.ReadOnly<OnConnected>(),
            ComponentType.ReadOnly<WorkerEntityTag>()
        );
    }

    protected override void OnUpdate()
    {
        // You can iterate through the matching components using the ECS `.ForEach` syntax.
        Entities.With(query).ForEach(entity =>
        {
            Debug.Log("Worker just connected!");
        });
    }
}

How to run logic when the worker has just disconnected

The GDK adds the OnDisconnected temporary component to your worker entity when it has just disconnected. This component contains a single string field storing the reason for disconnecting.

Example usage

public class HandleDisconnectSystem : ComponentSystem
{
    private EntityQuery query;

    protected override void OnCreate()
    {
        base.OnCreate();

        query = GetEntityQuery(
            ComponentType.ReadOnly<OnDisconnected>(),
            ComponentType.ReadOnly<WorkerEntityTag>()
        );
    }

    protected override void OnUpdate()
    {
        // You can iterate through the matching components using the ECS `.ForEach` syntax.
        Entities.With(query).ForEach((OnDisconnected onDisconnected) =>
        {
            Debug.Log($"Got disconnected: {onDisconnected.ReasonForDisconnect}");
        });
    }
}

Search results

Was this page helpful?

Thanks for letting us know!

Thanks for your feedback

Need more help? Ask on the forums