Get SpatialOS



Designing and defining entities

Designing entities

You need to work out what things you want to populate your simulated world with, and decide which to model as entities, and which as components on another entity. There aren’t any hard and fast rules for which things should be which, but here are some principles.

Something should be an entity if:

  • it can exist on its own
  • it will be around for a while
  • it will interact with other things in the simulated world

Something should probably not be an entity if:

  • it is purely a rendering object that does not affect the simulated world, like particle effects and small pieces of rubble
  • it is always part of another object, like ammo in gun

For example, let’s consider a hat in the context of an online game, where players can collect different hats and chose one to wear on their character. Should the hats be entities?

It depends on how hats work in the game. If hats are purely a cosmetic enhancement, then it might be best to simply have the type of hat you are wearing as an enum stored as a component. However, if a hat can be shot off a player’s head, kicked around or piled into hat-towers, then it should be an entity in its own right.

Designing components

Once you’ve worked out what entities your world will have, the next step is to work out what the components of the entities should be.

Components can have three elements:

  • properties describe persistent values that change over time

  • events describe transient things that have happened to an entity

  • commands describe things that other entities can ask this entity to do

Each component can have many properties, events and commands. For example, an explosive barrel might have the following components:

Component Name Type
Position Coordinates Property: coordinates
FuelContainer FuelType Property: enum
Capacity Property: float
CurrentAmount Property: float
Explosive Explode Command
ExplosiveRange Property: float
HasExploded Property: boolean
Damageable CurrentDamage Property: float
MaxDamage Property: float
Flammable OnFire Property: boolean
CatchFire Event

It’s important that everything that you need to define your entity is stored in components. SpatialOS is a distributed system, running over many machines which may fail at any time, so your entity must be able to be recreated from the components. Any local state stored on a worker can be easily lost.

Writing components

Write up your components in the schema, using the SpatialOS schemalang. This schema is used to generate code that all workers can use to read and manipulate components.

For full details, see the schemalang documentation.

Creating an entity template

Now you’ve written up your components into schema, you need to create a template for each entity.

An entity template lists all the components that an entity has. When you want to create an entity, you’ll do this using its template.

For details on how to create an entity template, see Entity templates.

Was this page helpful?

Thanks for letting us know!

Thanks for your feedback

Need more help? Ask on the forums