Try SpatialOS

Sites

Menu
These are the docs for 12.2, an old version of SpatialOS. 13.2 is the newest →

Setting up a C# worker

Prerequisites

See Requirements.

Using the C# blank project

You can use the C# blank project as a starting point for a C# worker; it provides a minimal example of project structure and build configuration.

Setting up a worker manually

To add a C# worker to your project:

  1. Create a directory under workers/ of your SpatialOS project (for example, workers/csharp).
  2. Create a worker configuration file file (for example, workers/csharp/spatialos.MyCsharpWorker.worker.json) with the following contents:

    {
    "build": {
    "tasks_filename": "spatialos.csharp.build.json",
    "generated_build_scripts_type": "csharp_msbuild"
    },
    "bridge": {
    "worker_attribute_set": {
      "attributes": [
        "new_worker_attribute"
      ]
    },
    "entity_interest": {
      "range_entity_interest": {
        "radius": 2
      }
    },
    "streaming_query": [],
    "component_delivery": {
      "default": "RELIABLE_ORDERED",
      "checkout_all_initially": true
    }
    }
    }
    
    
  3. The default build setup expects a CsharpWorker.csproj in the same directory. This file is controlled by you, and should provide you with enough flexibility without the need to turn off automatically generated build scripts.

    We provide a seed csproj file which you should start with.

    This includes BuildTargets.targets and CsharpWorker.targets, which set up the expected platforms and output for you. You can choose any name for the assembly - this will then be the name of the worker and the executable.

    The csproj is set to build an executable, so you will need to provide a static int Main(string[] args) somewhere in the source code in order for the build to succeed.

    For example, you can create a src/Test.cs within the worker directory with the following contents:

    using System;
    class Test
    {
        static void Main()
        {
            Console.WriteLine("Hello World!");
        }
    }
    
  4. Build your worker using spatial worker build or spatial worker build <worker type>.

  5. Add the worker to your SpatialOS application.

    Assemblies produced by spatial worker build contain an exe file with the assembly name set in your csproj file. For example, the seed csproj produces assemblies containing CsharpWorkerName.exe.

    You should launch them using mono CsharpWorkerName.exe. Configure managed workers to be launched this way in the launch config.

For more about the build setup, see the Building page.

If you see a DllNotFoundException, this may be because you’re running the 32-bit version of Mono (on macOS, mono can default to 32-bit).

To ensure that SpatialOS SDK native libraries are loaded correctly, make sure you’re using the 64-bit version of Mono by:

  • Using the --arch=64 flag by passing it as the first argument, e.g. mono --arch=64 CsharpWorkerName.exe

    This flag is only valid for macOS versions of Mono.

  • Running mono64 directly.

Search results

Was this page helpful?

Thanks for letting us know!

Thanks for your feedback

Need more help? Ask on the forums