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 type of workers can have write access (also known as ”authority”) on a per-component basis.

You use the EntityTemplate class.

There are examples of how to use this class below.

Create component snapshots

For each schema component you define, the code generator creates a struct which inherits from ISpatialComponentSnapshot, the generated snapshot struct. For example, for the following schema:

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

The code generator creates:

public partial class Health
    public struct Snapshot
        int CurrentHealth;

The following code snippet shows an example of how to define an EntityTemplate. 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.

Example defining a template for a SpatialOS entity creature.

public static class CreatureTemplate

    public static EntityTemplate CreateCreatureEntityTemplate(Coordinates coords)
        var entityTemplate = new EntityTemplate();

        entityTemplate.AddComponent(new Position.Snapshot { Coords = coords }, "UnityGameLogic");
        entityTemplate.AddComponent(new Metadata.Snapshot { EntityType = "Creature"}, "UnityGameLogic");
        entityTemplate.AddComponent(new Persistence.Snapshot(), "UnityGameLogic");
        entityTemplate.AddComponent(new Health.Snapshot { CurrentHealth = 100 }, "UnityGameLogic");
        entityTemplate.SetReadAccess("UnityGameLogic", "UnityClient");
        entityTemplate.SetComponentWriteAccess(EntityAcl.ComponentId, "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