Skip to content

Scene files, prefabs and snapshots

API → Scene

How a scene becomes a .scene.json file and comes back, how prefabs are built, instantiated, diffed and applied, how a scene is snapshotted for play and stop, and how the scene_manager loads scenes as assets. Headers: conjure/scene/scene_serializer.h, conjure/scene/prefab.h, conjure/scene/scene_snapshot.h and conjure/scene/scene_manager.h. Everything is in namespace conjure::scene.

Syntax

#include "conjure/scene/prefab.h"
#include "conjure/scene/scene_manager.h"
#include "conjure/scene/scene_serializer.h"

using namespace conjure;

core::result<void> stamp_crates(scene::scene_manager &scenes, const scene::prefab_asset &crate) {
    scene::scene &level = scenes.active();
    std::array<ecs::entity, 10> roots = {};
    scene::instantiate_options options = {};
    options.parent = level.find_path("shelf");
    CONJURE_TRY(scene::prefab::instantiate_many(level, crate, 10, options, roots));

    // Save the result; a later load reproduces it byte for byte.
    core::vfs::memory_file_system fs = {};
    CONJURE_TRY(scene::scene_serializer::save_scene_file(level, fs, "scenes/warehouse.scene.json"));
    CONJURE_TRY_ASSIGN(const scene::scene_id loaded, scenes.load_file(fs, "scenes/warehouse.scene.json"));
    return scenes.unload(loaded);
}

At a glance

You want to... Use
Save the scene to a file scene_serializer::save_scene_file(scene, fs, "scenes/main.scene.json")
Load a file into the scene scene_manager.load_file(fs, path, load_mode::REPLACE) or scene_serializer::load_scene_file(...)
Load a scene asset by uuid (dependencies first) scene_manager.load(assetId, load_mode::REPLACE)
Load additively, later, without blocking scene_manager.request_load(assetId, load_mode::ADDITIVE); applied at scene.apply_pending_loads
Unload a loaded scene scene_manager.unload(sceneId), request_unload(sceneId), unload_all()
Turn a subtree into a prefab document prefab::build_document(scene, root, "name")
Instantiate a prefab asset prefab::instantiate(scene, asset, options), prefab::instantiate_many(...)
See what changed in an instance / undo it / keep it prefab::diff, prefab::revert, prefab::apply
Stop treating entities as an instance prefab::unpack(scene, root, recursive)
Build a prefab without naming component types (model import) prefab_builder
Snapshot for play, restore on stop capture(scene) then restore(scene, snapshot)
Ask a snapshot about one entity (did it exist, what did it hold) snapshot.contains(guid), has_component(guid, id), component_json(guid, id, scene)
Save one entity without its children scene_serializer::save_entity(scene, entity, writer)
Register the scene asset types and importers register_asset_types(types, importers, registry)

Variables and Properties

Types, fields, and state are described in the reference sections below.

Scene files

A .scene.json file is a small header plus the ECS archive of every entity in hierarchy order:

{"format":"conjure.scene","version":1,"name":"main","settings":{...},
 "roots":["<guid>", ...],"archive":{"entities":[{"components":{"conjure.scene.name":{...}, ...}}, ...]}}

save_scene(scene, writer) and save_scene_file(scene, fs, path) write it (the file write is atomic); parse_document(value, registry) reads it into a scene_document {name, settings, roots, archive}, and load_scene(scene, document, owner, guids) instantiates it: every entity in its final archetype, children and depth tags linked, the roots ordered as the header lists them, and every asset_ref field bound through the asset manager (the bind pass). load_scene_file does all three. An entity's component keys are written in name order (the byte order of the names), not in the order the components were registered, so a file does not depend on the build that saved it; loading accepts any order. Save, load, save again is byte-identical; components the registry does not know survive untouched in unresolved_components and are written in the same name order. A save refuses a scene whose entity carries a layer index of 32 or more (BAD_FORMAT naming the entity, nothing written), because loading would refuse the file.

Errors: BAD_FORMAT (wrong format name, missing archive, malformed roots or settings, or a layer index of 32 or more, naming the entry), UNSUPPORTED_VERSION, DUPLICATE_GUID with guid_policy::KEEP, HIERARCHY_CYCLE, HIERARCHY_TOO_DEEP, and the ECS's SERIALIZATION_FAILED for a malformed component. Nothing is created when a load fails.

guid_policy says what happens to the file's guids: KEEP (a collision fails the load), REGENERATE_ON_CONFLICT (colliding entries get fresh guids with one WARN) or REGENERATE_ALL (prefabs and copies). Both regenerate policies also give an entity whose guid is the null uuid a fresh one; KEEP leaves it null.

Important

Scripts, bodies and renderer components in a file are just components to the scene. A component that owns external state (the scripting module's script_component) carries its own serializer hooks, so it rides along; the scene never calls into those modules.

The scene manager

scene_manager(world, assets) owns the one scene over the world (active()), the loaded-scene records and their asset handles, the layer_table (32 layer names the engine fills from project.json) and the transform system the scheduler runs.

Call What happens
load(assetId, mode) load_blocking of the scene asset and its dependencies, then the document is applied; LOAD_FAILED when the asset is not a scene
load_file(fs, path, mode) parses a file, applies it, and loads the referenced assets as a batch
request_load(assetId, mode) queues; apply_pending_loads() (the FRAME_BEGIN system) applies it once the asset is LOADED, in request order; a FAILED asset drops the request with ERROR and on_load_failed
unload(sceneId) fires on_before_unload(id, roots) with the roots still alive, destroys them in reverse order, drops the handles and runs asset_manager::collect()
unload_all() every scene in reverse load order, then the remaining authored entities; when any entity is left, on_before_unload fires once more with SCENE_ID_AUTHORED and the remaining roots before they are cleared; a scene a handler loads meanwhile is cleared and unloaded with the rest
is_loaded(id), find_loaded(assetId), loaded_scenes(), info(id), is_load_pending(assetId) queries; loaded_scene_info lists the current roots on every call

load_mode::REPLACE first checks that the new file would load (so a bad file leaves the current content in place), unloads everything, and takes the file's name and settings; ADDITIVE keeps what is there and regenerates colliding guids with one WARN. Ids retired by an unload or a REPLACE are never reused. A load whose scene an on_structure_changed handler unloads before the load finishes returns SCENE_NOT_LOADED.

Warning

on_before_unload is where modules that own per-entity state outside the ECS (scripting's managed instances) clean up. Subscribe before the first load; the event fires on the main thread inside unload and unload_all (a REPLACE load runs unload_all).

Prefabs

A prefab is a prefab_document {name, archive} whose entry 0 is the single root. prefab::build_document(scene, root, name) captures a subtree (references to entities outside the subtree become null); the conjure.scene.prefab asset type publishes it as prefab_asset {archive, name, rootGuid, dependencies, id}. Like every subtree capture (capture_subtree, save_subtree, duplicate_subtree, and diff and apply, which capture the instance), it refuses an entity whose layer index is 32 or more with BAD_FORMAT naming the entity, because instantiating the result would fail.

Instances are fully materialised: prefab::instantiate(scene, asset, options) creates real entities with fresh guids, puts prefab_instance {asset_ref<prefab_asset>} on the root and prefab_link {assetGuid} on every entity, and binds the asset references. instantiate_many(scene, asset, count, options, outRoots) does it in one batch. instantiate_options places the copies (parent, index, owner), may rename the root or override its transform, and can skip the bind pass.

Function Purpose
prefab::instance_root_of(scene, entity) the nearest ancestor (itself included) carrying prefab_instance
prefab::diff(scene, root) prefab_diff of entity_diff {assetGuid, instanceEntity, kind, components} entries: ADDED, REMOVED or MODIFIED with the component names; components are compared as canonical JSON with local references remapped, and the parent link by parent guid and sibling index
prefab::revert(scene, root) replaces the instance with a fresh one at the same place, keeping the root's guid, name and transform; when the current asset no longer fits there (HIERARCHY_TOO_DEEP, PREFAB_MULTIPLE_ROOTS, BAD_FORMAT for a layer index of 32 or more) the instance is left untouched
prefab::apply(scene, root) the instance as a new document: link guids become entry guids (a link guid already used by an earlier entity gets a fresh one), the root loses its marker
prefab::refresh_instances(scene, prefabId, onlyUnmodified = true) reverts every instance of the asset; returns how many
prefab::unpack(scene, root, recursive = false) removes the markers, stopping at nested instance roots unless recursive
prefab::mark_instance(scene, root, asset_ref) marks an existing subtree as an instance (guids generated where missing)

Errors: NOT_A_PREFAB_INSTANCE, PREFAB_NOT_LOADED (the root's prefab_instance reference is not resident), PREFAB_MULTIPLE_ROOTS for a document whose entry 0 is not the only root, BAD_FORMAT for a document with an empty archive or a layer index of 32 or more (an entity in the captured subtree, or an entry of the instantiated asset), and DUPLICATE_GUID from parse_prefab when two entries share a guid.

prefab_builder(registry) builds a document without naming any C++ component type: add_node(parentIndex, name, transform), then add_component_json(node, componentName, value), add_component_raw(node, id, bytes) or add_tag(node, id), and build(name). The engine's model importer turns a glTF node tree into a prefab this way. BUILDER_INVALID names a bad parent, a second root or a component the builder writes itself (parent, name, local_transform, guids, depth tags).

One entity without its children

scene_serializer::save_entity(scene, entity, writer) writes one entity in the prefab format, without its subtree: it is save_subtree for the entity alone, and capture_entity(scene, entity) returns the same thing as an ecs::entity_archive of one entry. The editor's play-mode persist uses it to restore one entity on its own, without touching its children.

  • The entity keeps its guid and its name, which also names the document.
  • Its parent link is dropped, so the entry is a root.
  • Every reflected entity reference that points outside the entity is written as null. Since nothing but the entity is in the archive, that includes a reference to its own child.
  • A component whose own serializer writes its JSON (the scripting module's script_component) keeps the live ids of the entities it names. Retarget those yourself; the editor writes them through their guids.

instantiate_subtree(scene, value, parent, true, index) (keepGuids true) puts the entity back with its guid, under the parent and at the sibling index you choose, and creates no children; the original must be gone by then, or its guid collides (DUPLICATE_GUID). save_entity and capture_entity fail with INVALID_ENTITY for a dead entity and BAD_FORMAT naming the entity for a layer index of 32 or more.

#include "conjure/scene/scene_serializer.h"

#include "conjure/core/json/writer.h"

#include <string>

using namespace conjure;

// The entity alone, as prefab-format JSON: its components, its name and its guid.
core::result<std::string> entity_json(const scene::scene &level, ecs::entity entity) {
    core::json::writer out = {};
    CONJURE_TRY(scene::scene_serializer::save_entity(level, entity, out));
    return out.finish();
}

Snapshots

capture(scene) returns a scene_snapshot: the typed archive of every entity in hierarchy order plus the root order, owners, settings, name and the structure version at capture; no JSON is involved, so it is fast enough for play and stop. The structure version is informational: restore cannot rewind the counter, which keeps counting up through the restore like through any other structural change. restore(scene, snapshot) clears the scene and instantiates the snapshot with the guids kept; on failure the scene is left empty rather than half-restored. snapshot.entity_count() says how much it holds. Because a failed restore cannot give the scene back, capture refuses what restore would refuse: an entity whose layer index is 32 or more fails the capture with BAD_FORMAT naming the entity, so play never starts on a scene that stop would lose.

Asking a snapshot about one entity

A snapshot also answers questions about single entities, by guid: did this entity exist before play, and what did it look like then? The editor asks them while playing. capture indexes every guid once (guidIndex, sorted snapshot_guid_entry {guid, entry} rows), so each lookup is a binary search.

Query Answers
contains(guid) the entity existed when the snapshot was taken; an entity created later is not in it
find_entry(guid) its archive index, or std::nullopt
entry_guid(entry) the guid of an archive entry, or std::nullopt (no guid, or out of range)
subtree_guids(guid) the guids of its subtree as it was then, the entity first; empty for an unknown guid
has_component(guid, id) it held that component then (a tag counts; TRANSIENT components are never captured)
component_json(guid, id, scene) that component as JSON, in the form the editor reads a live component

component_json writes an entity field of a reflected component as the guid of the entity it names, as the editor's live read does. A component whose own serializer writes its JSON (the script component) names the live entity of scene that carries the referenced guid, as its live JSON does. So a component the play session did not change reads equal to its live JSON, with two exceptions:

  • A running script's live JSON comes from its instance: its started and created flags and every field of its class. The snapshot holds the component as it was, with the fields set before play.
  • A reflected field naming an entity destroyed since the capture holds that entity's guid in the snapshot's JSON, and null in the live read.

It fails with GUID_NOT_FOUND for a guid the snapshot does not hold, UNKNOWN_COMPONENT for an id the registry does not know and COMPONENT_NOT_PRESENT when the entity did not have the component.

#include "conjure/scene/scene_snapshot.h"

#include <optional>
#include <string>

using namespace conjure;

// True when the entity existed before play and its component still reads as it did then.
// 'liveJson' is the component as the editor reads it now.
bool unchanged_since_play(
  const scene::scene_snapshot &before,
  const scene::scene &level,
  ecs::entity entity,
  ecs::component_id id,
  const std::string &liveJson
) {
    const std::optional<core::uuid> guid = level.guid_of(entity);
    if (!guid.has_value() || !before.has_component(*guid, id)) {
        return false;
    }
    const core::result<core::json::document> then = before.component_json(*guid, id, level);
    return then.has_value() && then.value().to_string() == liveJson;
}

Important

A snapshot you assemble by hand, rather than with capture, has no index: every by-guid query answers "not found" until you call index_guids() once its archive is complete. Entries without a guid, or with the null guid, are not indexed, and when two entries share a guid the first in archive order wins.

Functions

Signature (short) Purpose
static result<void> scene_serializer::save_scene(const scene &, core::json::writer &) write the scene format
static result<void> scene_serializer::save_scene_file(const scene &, vfs::file_system &, string_view path) atomic file write
static result<scene_document> scene_serializer::parse_document(const json::value &, const ecs::component_registry &) header and archive
static result<prefab_document> scene_serializer::parse_prefab(const json::value &, const ecs::component_registry &) prefab format
static result<void> scene_serializer::write_prefab(const prefab_document &, json::writer &) / write_document(const scene_document &, json::writer &) writers
static result<instantiate_report> scene_serializer::load_scene(scene &, const scene_document &, scene_id owner, guid_policy) instantiate
static result<instantiate_report> scene_serializer::load_scene_file(scene &, vfs::file_system &, string_view, scene_id, guid_policy) read, parse, load
static result<void> scene_serializer::save_subtree(const scene &, entity root, json::writer &) a subtree in prefab format
static result<void> scene_serializer::save_entity(const scene &, entity, json::writer &) / static result<ecs::entity_archive> capture_entity(const scene &, entity) one entity without its children
static result<entity> scene_serializer::instantiate_subtree(scene &, const json::value &, entity parent, bool keepGuids, u32 index = APPEND_CHILD) parse and place
static result<ecs::entity_archive> scene_serializer::capture_subtree(const scene &, entity root, ecs::archive_options = {}) hierarchy-ordered capture
static void scene_serializer::bind_asset_refs(ecs::world &, span<const entity>, assets::asset_manager &, vector<asset_id> *outIds) / bind_entity(world &, entity, asset_manager &) the bind pass
result<instantiate_report> scene::instantiate_archive(const shared_ptr<const entity_archive> &, u32 count, const instantiate_options &, span<entity> outRoots) the primitive behind all of the above
scene_manager(ecs::world &, assets::asset_manager &) construction
result<scene_id> scene_manager::load(const asset_id &, load_mode = REPLACE), load_file(vfs::file_system &, string_view, load_mode = REPLACE) synchronous loads
load_request_id scene_manager::request_load(const asset_id &, load_mode = ADDITIVE), void request_unload(scene_id), void apply_pending_loads() deferred
result<void> scene_manager::unload(scene_id), void unload_all() unloads
core::event<scene_id, span<const entity>> &on_before_unload(), event<scene_id> &on_loaded(), event<load_request_id, const error &> &on_load_failed() events
result<prefab_document> prefab::build_document(const scene &, entity root, string_view name) capture
result<entity> prefab::instantiate(scene &, const prefab_asset &, const instantiate_options & = {}) one instance
result<void> prefab::instantiate_many(scene &, const prefab_asset &, u32 count, const instantiate_options &, span<entity> outRoots) many
result<prefab_diff> prefab::diff(const scene &, entity root), result<void> revert(scene &, entity root), result<prefab_document> apply(const scene &, entity root) overrides
result<scene_snapshot> capture(const scene &), result<void> restore(scene &, const scene_snapshot &) play / stop
bool scene_snapshot::contains(const uuid &), optional<u32> find_entry(const uuid &), optional<uuid> entry_guid(u32), vector<uuid> subtree_guids(const uuid &), bool has_component(const uuid &, component_id), void index_guids() by-guid queries
result<json::document> scene_snapshot::component_json(const uuid &, component_id, const scene &live) a component as it was, in the live read's form
result<void> register_asset_types(assets::asset_type_registry &, assets::importer_registry &, const ecs::component_registry &) conjure.scene.scene (.scene.json) and conjure.scene.prefab (.prefab.json)

See also: