Get SpatialOS

Sites

Menu

Creating workers

This document relates to the MonoBehaviour workflow.

Before reading this document, make sure you have read:

To demonstrate the use of the Worker class, the GDK contains an example implementation of how to create a worker instance (server-worker or client-worker) and connect to SpatialOS. We provide an abstract WorkerConnector class and two child classes implementing the abstract methods to get you started quickly:

The WorkerConnector is a MonoBehaviour script. You can use it to create multiple server-worker or client-worker instances in one Scene by adding it to multiple GameObjects, each GameObject creating a different worker instance.

ECS entity translation
When you have multiple worker instances represented as GameObjects in a Scene, you are likely to have multiple ECS entities checked out to each worker instance/GameObject. To make sure you don’t have all the ECS entities which are checked out to a worker instance/GameObject in the same (x, y, z) location, we use an offset for each ECS entity against the origin of the worker instance/GameObject. We call the offset of the worker instance/GameObject origin, the “translation”.

How to use worker prefabs

In the GDK’s Blank project, we provide an example implementation of a server-worker and client-worker on different platforms connecting using the WorkerConnector. There are three example scripts and four example Scenes.

Scripts

The scripts are stored inside workers/unity/Assets/Scripts/Workers:

  • UnityClientConnector: This is a sample implementation to connect as a client-worker on Windows or MacOS. (Note that the client-worker is sometimes called UnityClient.)
  • UnityGameLogicConnector: This is a sample implementation to connect as a server-worker.
  • MobileClientWorkerConnector: This is a sample implementation to connect as a client-worker on an Android or iOS device.

Scenes

  • ClientScene: This Scene contains only the ClientWorker GameObject which you can use to build a client-worker instance for cloud deployments (for a game client on Windows or MacOS)
  • MobileClientScene: This Scene contains only the ClientWorker GameObject which you can use to build your client-worker for local and cloud deployments (for a game client on Android or iOS).
  • GameLogicScene: This Scene contains only the GameLogicWorker GameObject which you can use to build a server-worker instance for cloud deployments (as it’s a server-worker instance, the game client platform you are using is irrelevant).
  • DevelopmentScene: This Scene contains both the ClientWorker GameObject representing the client-worker instance (on Windows or MacOS) and the GameLogicWorker GameObject representing the server-worker instance and starts both worker instances as soon as you load the Scene.

How to create your own WorkerConnector

You can inherit from the WorkerConnector class to create your own connection logic, dependent on the type of the worker that you want to create.

How to modify the connection configuration

When inheriting from the WorkerConnector, you can override GetAlphaLocatorConfig, GetLocatorConfig and GetReceptionistConfig to modify the connection configuration a worker type uses to connect to the Locator or Receptionist. See Connecting to SpatialOS to find out more about the connection flows for client-workers and server-workers.

How to decide which connect service to use

First, see Connecting to SpatialOS to find out more about the connection flows for client-workers and server-workers).

The DefaultWorkerConnector provides a default implementation on how to decide which connection flow to use based on the command line arguments defined in your worker launch configuration. These arguments are used by SpatialOS to start your workers. You can change the behavior by overriding the GetConnectionService method.

How to connect using the development authentication flow

We provide an integration with the development authentication flow that you can use to connect your client-workers to a cloud deployment. This is especially important when working with client-workers for mobile devices or when you want your client to be able to choose the deployment it connects to.

The DefaultMobileWorkerConnector provides an example implementation on how to use the development authentication flow connecting via the new v13.5+ Locator connection flow.

Search results

Was this page helpful?

Thanks for letting us know!

Thanks for your feedback

Need more help? Ask on the forums