Get SpatialOS

Sites

Menu

Worker entity

This document relates to the ECS workflow.

Before reading this document, see the documentation on workers in the GDK.

Each of the workers in your project must have exactly one ECS entity in its worker-ECS world at any point in time. To uniquely identify the worker entity of your current worker, the worker entity has the WorkerEntityTag component attached to it.

The worker’s worker entity performs certain tasks:

  • send and receive commands before the worker has checked out any SpatialOS entities.
  • register changes to the state of the Runtime connection (that is whether the worker is connected to the Runtime or not) by filtering for the following temporary components:
    • OnConnected: the worker just connected to the SpatialOS Runtime.
    • OnDisconnected: the worker just disconnected from the SpatialOS Runtime. This is an ISharedComponentData and stores the reason for the disconnection as a string.

How to run logic when the worker has just connected

You can use the worker to check in an ECS system to see whether the worker just connected. This allows you to handle any initialization logic necessary.

Example

using Improbable.Gdk.Core;
using Unity.Entities;
using UnityEngine;

public class HandleConnectSystem : ComponentSystem
{
    private struct Data
    {
        public readonly int Length;
        [ReadOnly] public ComponentDataArray<OnConnected> OnConnected;
        [ReadOnly] public ComponentDataArray<WorkerEntityTag> DenotesWorkerEntity;
    }

    [Inject] private Data data;

    protected override void OnUpdate()
    {
        Debug.Log("Worker just connected!");
    }
}

How to run logic when the worker has just disconnected

You can use the worker to check in an ECS system to see whether the worker just disconnected. This allows you to handle any clean-up logic necessary.

Example

using Improbable.Gdk.Core;
using Unity.Entities;
using UnityEngine;

public class HandleDisconnectSystem : ComponentSystem
{
    private struct Data
    {
        public readonly int Length;
        [ReadOnly] public SharedComponentDataArray<OnDisconnected> OnDisconnected;
        [ReadOnly] public ComponentDataArray<WorkerEntityTag> DenotesWorkerEntity;
    }

    [Inject] private Data data;

    protected override void OnUpdate()
    {
        var reasonForDisconnect = data.OnDisconnected[0].ReasonForDisconnect;
        Debug.Log($"Got disconnected: {reasonForDisconnect}");
    }
}

How to send a command using the worker entity

The worker entity has all command sender components attached to it. By filtering for these components, you are able to send commands even if you don’t have any SpatialOS entities which is checked out.

public class CreateCreatureSystem : ComponentSystem
{
    private struct Data
    {
        public readonly int Length;
        [ReadOnly] public ComponentDataArray<WorkerEntityTag> DenotesWorkerEntity;
        public ComponentDataArray<WorldCommands.CreateEntity.CommandSender> CreateEntitySender;
    }

    [Inject] private Data data;

    protected override void OnUpdate()
    {
        var requestSender = data.CreateEntitySender[0];
        var entity = CreatureTemplate.CreateCreatureEntityTemplate(new Coordinates(0, 0, 0));
        requestSender.RequestsToSend.Add(new WorldCommands.CreateEntity.Request(entity));
        data.CreateEntitySender[0] = requestSender;
    }
}

Search results

Was this page helpful?

Thanks for letting us know!

Thanks for your feedback

Need more help? Ask on the forums