vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
Vkm::Engine::SceneSerializer Namespace Reference

Save / load a Scene + the assets it references to/from JSON. More...

Functions

void saveComponents (const Scene &scene, EntityId id, nlohmann::json &out, const ResourceManager &resources)
 Write every serialized component of id into out, naming the entities they refer to by their scene slot.
void saveComponents (const Scene &scene, EntityId id, nlohmann::json &out, const ResourceManager &resources, const EntityNamer &name)
 Write every serialized component of id into out, naming the entities they refer to through name.
void loadComponents (const nlohmann::json &src, Scene &scene, EntityId entity, const ResourceManager &resources, const EntityResolver &resolve)
 Add every component present in src to entity.
bool save (const Scene &scene, const ResourceManager &resources, const std::string &path)
 Save scene + the assets it references to path.
bool load (Scene &scene, ResourceManager &resources, const std::string &path)
 Load a scene from path, replacing the live scene atomically.
std::vector< MissingAssetRef > unresolvedRefs (const Scene &scene, const ResourceManager &resources, EntityId id)
 The entries of id's unresolved-reference record whose field is still empty.
std::string saveToString (const Scene &scene, const ResourceManager &resources)
 Serialize scene + the assets it references to an in-memory JSON string (same content as save(), no file written).
bool loadFromString (const std::string &text, Scene &scene, ResourceManager &resources, const Prefab::InstanceSlots *instanceSlots=nullptr)
 Load a scene from an in-memory JSON string produced by saveToString(), replacing scene and merging into resources.

Detailed Description

Save / load a Scene + the assets it references to/from JSON.

An assets block sits beside entities, rebuilt through AssetSerializer::loadAssets; components name assets by name.

load() stages the scene and the asset graph and swaps both in only on full success; loadFromString() stages the scene only. Entities return to their saved slots (Scene::createEntityAt), so references in the file need no remap.

Function Documentation

◆ saveComponents() [1/2]

void Vkm::Engine::SceneSerializer::saveComponents ( const Scene & scene,
EntityId id,
nlohmann::json & out,
const ResourceManager & resources )

Write every serialized component of id into out, naming the entities they refer to by their scene slot.

Parameters
sceneScene holding the entity.
idEntity to serialize.
outJSON object to fill, keyed by component name.
resourcesResolves asset handles to their names.

◆ saveComponents() [2/2]

void Vkm::Engine::SceneSerializer::saveComponents ( const Scene & scene,
EntityId id,
nlohmann::json & out,
const ResourceManager & resources,
const EntityNamer & name )

Write every serialized component of id into out, naming the entities they refer to through name.

For a carrier with its own numbering, such as a prefab's file order.

Parameters
sceneScene holding the entity.
idEntity to serialize.
outJSON object to fill, keyed by component name.
resourcesResolves asset handles to their names.
nameNames the entities the components refer to.

◆ loadComponents()

void Vkm::Engine::SceneSerializer::loadComponents ( const nlohmann::json & src,
Scene & scene,
EntityId entity,
const ResourceManager & resources,
const EntityResolver & resolve )

Add every component present in src to entity.

Hierarchy is not read: its parent may not exist yet, so callers wire it in a second pass.

Parameters
srcJSON object written by saveComponents.
sceneScene to add into.
entityThe entity receiving the components.
resourcesResolves asset names back to handles.
resolveReads the carrier's names for entities back to entities.

◆ save()

bool Vkm::Engine::SceneSerializer::save ( const Scene & scene,
const ResourceManager & resources,
const std::string & path )
nodiscard

Save scene + the assets it references to path.

Parameters
sceneScene to write.
resourcesResolves each handle the scene holds to the name written.
pathFile to write.
Returns
true on success; false (and a logged error) on I/O failure.

◆ load()

bool Vkm::Engine::SceneSerializer::load ( Scene & scene,
ResourceManager & resources,
const std::string & path )
nodiscard

Load a scene from path, replacing the live scene atomically.

Parameters
sceneScene to replace.
resourcesAsset graph, replaced by the one the file's assets build.
pathFile to read.
Returns
true on success; false (and a logged error) on failure, with the live scene untouched.

◆ unresolvedRefs()

std::vector< MissingAssetRef > Vkm::Engine::SceneSerializer::unresolvedRefs ( const Scene & scene,
const ResourceManager & resources,
EntityId id )

The entries of id's unresolved-reference record whose field is still empty.

The record is never trimmed, since an undo can empty a filled field again and the name must still be there; readers ask this instead. Answered by writing the entity and reading the field back, as the save does.

Parameters
sceneScene holding the entity.
resourcesAsset graph the entity's references resolve against.
idEntity to ask about.
Returns
The entries still unresolved, in record order; empty when none are.

◆ saveToString()

std::string Vkm::Engine::SceneSerializer::saveToString ( const Scene & scene,
const ResourceManager & resources )

Serialize scene + the assets it references to an in-memory JSON string (same content as save(), no file written).

Pairs with loadFromString(); see PlaySnapshot.

Parameters
sceneScene to write.
resourcesResolves each handle the scene holds to the name written.
Returns
The serialized document.

◆ loadFromString()

bool Vkm::Engine::SceneSerializer::loadFromString ( const std::string & text,
Scene & scene,
ResourceManager & resources,
const Prefab::InstanceSlots * instanceSlots = nullptr )
nodiscard

Load a scene from an in-memory JSON string produced by saveToString(), replacing scene and merging into resources.

The scene swaps in as in load(); the asset graph is not swapped, so every handle issued from it stays valid. The document stores an instance as a reference, so instanceSlots restores where its own entities stood.

Parameters
textA document from saveToString().
sceneScene to replace on success.
resourcesAsset graph to resolve against, added to only where a name is absent.
instanceSlotsOptional: Prefab::instanceSlotsOf the source world; without it an instance's entities take whatever slots are free.
Returns
true on success; false (and a logged error) on failure.