1. Design the SpatialOS component

To satisfy our design constraints, we need our health packs to contain some state that is replicated between our server-workers and client-workers:

  • A property that represents whether the health pack is "active".
  • A property that represents the "value" of the health pack, or, how much health the health pack will grant when consumed.

Strictly speaking, the property that represents the value of the health pack could be a hardcoded value in your server-worker instances, but this design opens up the possibility for dynamic health packs which have varying values.

We write the definitions for these properties in schemalang, a SpatialOS specific language. Let's do that now!

Step 1. Using your file manager, navigate to the gdk-for-unity-fps-starter-project/schema/ directory, then create a pickups directory.

Step 2. Inside the gdk-for-unity-fps-starter-project/schema/pickups/ directory, use a text editor of your choice to create a new file called health_pickup.schema.

Step 3. Copy and paste the following definition into the file and save the file:

package pickups;

component HealthPickup {
    id = 21001;
    bool is_active = 1;
    uint32 health_value = 2;
}

This defines a new SpatialOS component called HealthPickup, and defines the two properties that we discussed above:

  • is_active: A boolean that indicates if the health pack is active.
  • health_value: An integer value which indicates the amount of health the pack will grant to a player.

The id property is a required property for every SpatialOS component defined in schema. Each component must have a unique ID in your project.

See the schemalang reference for more information.

Each field in a component must have a unique id (within the component) which it is assigned to.

This allows us to maintain backwards compatibility when schema changes. See schemalang reference for more info.

Step 4. Run the code generator.

From your Unity Editor menu, select SpatialOS > Generate code to invoke the code generator.

The code generator creates C# code based on the components and types defined in the schemalang. Any time you modify your schema files you must then run the code generator to see your changes reflected in code.

When writing schema files, your properties must use snake case (for example, health_value), but the the code generator will create C# code in the standard C# capitalisation conventions.

Code generation is automatically run whenever you open a GDK for Unity project in your Unity editor.

If you are worried your generated code is in a bad state, select SpatialOS > Generate code (force) from the Unity Editor menu to delete the generated code and regenerate it.

The generated code for your component can be found in the Assets/Generated/Source/pickups/ directory of your Unity project. Feel free to have a look if you want to see what happens behind the scenes.

Note that you don’t need to understand the generated code in order to follow this tutorial.

Updated about a year ago



1. Design the SpatialOS component


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.