Skip to content

Scenes and Serialization

SIECS can save the current world to a native binary scene and instantiate that scene later.

The typed C++ facade does not currently wrap scene serialization. C++ programs therefore call the native scene API directly, with normal C++ ownership and null-pointer syntax; this is a documented gap, not a second C++ API.

void *data = NULL;
size_t size = 0;
if (ecs_save_memory(&data, &size)) {
/* use data */
ecs_scene_free(data);
}

The caller owns the returned scene buffer and releases it with ecs_scene_free().

bool ok = ecs_scene_validate(data, size);

Validation checks the complete scene without creating entities or modifying the world.

The binary format is version 2. Reflected nested structs, arrays, and owning vectors are traversed through their fields and elements; C strings use length and bytes, and entity values use local scene indices. Typed pair rows retain their element namespaces, payload component, and source; entity elements and reflected entity values are remapped on load. Arbitrary pointers and function pointers make a component unsupported for scene saving.

Use it before accepting scene data from storage/tooling paths.

bool ok = ecs_load_memory(data, size);

Loading instantiates entities into the current world.

It does not preserve runtime entity handles from the saved world.

Entity references stored in supported reflected data/relations are encoded in a scene-local form and remapped to fresh runtime entities during load.

ecs_save("scene.siecs");
ecs_load("scene.siecs");

The memory API remains useful for archives, networking layers or REST tooling.

The loading world must have compatible components, relations and modules registered.

The scene format is intentionally tied to the schema/layout expected by the binary that loads it.

Do not treat it as a forever-stable cross-version interchange format unless the application provides its own migration/versioning layer.

Loading the same scene multiple times creates independent fresh entity instances.

This makes scenes useful as reusable runtime templates/assets.

Reflected fields with owned dynamic data must follow the component lifecycle and serialization rules of the registered type.

When a component owns resources outside plain reflected storage, provide appropriate type operations and application-level serialization strategy.

Runtime entity handles contain world-specific indexes/generations.

Saving those raw values directly would be incorrect because the destination world can allocate different handles.

The scene format therefore represents supported references using scene-local identity and remaps them during instantiation.

This applies to ECS relations and reflected entity-reference fields supported by the serializer.

Loading does not clear the world first.

Existing entities remain and the scene creates additional entities.

If the application needs “replace current level” semantics:

  1. destroy/unload the current level entities;
  2. load/instantiate the new scene.

Keep that policy outside the low-level scene loader.

A load that fails returns false and kills the entities it already created, so the world keeps only its previous entities. Edges that would close a cycle on an acyclic relation make the load fail. Name collisions still follow the naming rules: a loaded name replaces an existing sibling with the same name, and that replaced entity is not restored if the load fails later.

A binary can be structurally valid but still be incompatible with a differently registered schema/version.

Applications shipping long-lived scene assets should version their content and test compatibility as part of release migration.

Use JSON/reflection for human/tool-oriented editable values.

Use native scenes for compact runtime instantiation of compatible SIECS world state.

They solve different problems and do not need identical stability guarantees.