Get SpatialOS



SpatialOS entities: Creating entity templates

This document relates to both MonoBehaviour and ECS workflows.

Before reading this document, make sure you are familiar with the documentation on the MonoBehaviour and ECS workflows and the workers in the GDK overview.

Whether you are using the MonoBehaviour workflow or the ECS workflow, you need to set up entity templates to create a SpatialOS entity. You use the EntityTemplate class to specify all the components that a SpatialOS entity has, the initial values of those components, and which workers have write access to each component.

For information on how to create SpatialOS entities once you have set up entity templates, see the documentation on creating entities (MonoBehaviour workflow) and ECS World commands (ECS workflow).

How to create a SpatialOS entity template

You have to create an EntityTemplate to specify which components a SpatialOS entity has and the initial values of those components. You have to also specify which workers have write access (also known as ”authority” on a per-component basis.

You use the EntityBuilder class to do this.

There are examples of how to use this class below.

Create ComponentData

For each schema component you define, the class created by the code generator has a method to create a ComponentData object for that component. For example, for the following schema:

component Health {
  id = 1;
  int32 current_health = 1;

The code generator creates a static method Health.Component.CreateSchemaComponentData(int currentHealth) which instantiates and populates a ComponentData object.

The following code snippet shows an example of how to define an EntityTemplate using the EntityBuilder class. You can use this EntityTemplate to spawn a SpatialOS creature entity via either the MonoBehaviour world commands or ECS world commands, depending on your workflow.

You need to create a new EntityTemplate for each call to CreateEntity.

Example defining a template for a SpatialOS entity creature.

public static class CreatureTemplate
    public static readonly List<string> AllWorkerAttributes =
        new List<string>

    public static EntityTemplate CreateCreatureEntityTemplate(Coordinates coords)
        var healthComponent = Health.Component.CreateSchemaComponentData(currentHealth: 100);

        var entity = EntityBuilder.Begin()
            .AddPosition(coords.X, coords.Y, coords.Z, "UnityGameLogic")
            .AddMetadata("Creature", "UnityGameLogic")
            .AddComponent(healthComponent, "UnityGameLogic")

        return entityTemplate;

Search results

Was this page helpful?

Thanks for letting us know!

Thanks for your feedback

Need more help? Ask on the forums