Get SpatialOS



Schema introduction

The schema is where you define all the components that entities in your SpatialOS world can have.

For information about how to design what goes into components, see Designing components.

We also recommend reading Component best practices.

Writing .schema files

Define components in .schema files, written in schemalang. Schema files are stored in the schema/ directory.

For information how to write schema files, see the schemalang reference page.

Generating code from the schema

SpatialOS uses the schema to generate code in various languages (including C#, C++, and Java). You use this generated code in your worker code to interact with the entities in the world.

There are two ways to generate code: using the CLI, or using the schema compiler directly.

Using the CLI

To generate code, you can use the command spatial worker codegen. This will execute the build steps specified in your worker build configuration file.

Using the schema compiler directly

If you are using the flexible project layout (currently in beta), you can use the schema compiler directly. You first need to retrieve the package using:

spatial package get tools schema_compiler-x86_64-{PLATFORM} <SDK version> schema_compiler-x86_64-{PLATFORM}.zip

Remember to specify your SDK version, and replace PLATFORM with win32, linux or macos.

You can then call the schema compiler with these arguments:

./schema_compiler --schema_path=... [arguments] input_files

Note: When compiling with the schema standard library, ensure the schema_path is set to the directory containing the ‘improbable’ directory (where the ‘improbable’ directory contains the standard library schema).

We recommend generating code for schema files individually to make use of any caching your build system may implement.

Argument Description
--schema_path=<path_to_directory> Root path where the schema files you want to compile are stored. This flag can be specified more than once to provide multiple directories. Note, the directory must be set to the root of import paths for schema imports to resolve correctly. For example, if you have the statement import "example/player.schema", you must pass a schema path to the location of example for it to be resolved correctly.
--load_all_schema_on_schema_path Loads all schema files in the path as if each .schema file in the schema_path was passed in individually
--print_components Prints the components in your schema on the standard output as they are generated
--dependency_out=<path_to_file> Dumps the dependencies to a file
--ast_json_out=<path_to_directory> Dumps the AST in JSON format in a directory
--cpp_out=<path_to_directory> Generates C++ code from the schema to a directory
--csharp_out=<path_to_directory> Generates C# code from the schema to a directory
--java_out=<path_to_directory> Generates Java code from the schema to a directory
--descriptor_set_out=<path_to_directory> Generates a protobuf descriptor file, used internally by the Runtime

Using the generated code

For details on how to use the generated code to send and receive component updates, see the docs for your SDK:

Search results

Was this page helpful?

Thanks for letting us know!

Thanks for your feedback

Need more help? Ask on the forums