Skip to content

Serializing and Deserializing a DynamicScene with a custom serializer fails #6891

@hankjordan

Description

@hankjordan

Here is how I am currently serializing my game state:

pub fn save_state(world: &mut World) {
    let type_registry = world.resource::<AppTypeRegistry>();
    let scene = DynamicScene::from_world(world, type_registry);

    let ser = SceneSerializer::new(&scene, type_registry);

    let mut buf = Vec::new();

    ser.serialize(&mut Serializer::new(&mut buf)).expect("Error serializing scene");

    IoTaskPool::get()
        .spawn(async move {
            File::create(format!("save/gamestate.mp"))
                .and_then(|mut file| file.write(buf.as_slice()))
                .expect("Error writing scene to file");
        })
        .detach();
}

It serializes to the messagepack format perfectly fine, but when I try to deserialize:

fn load_state(registry: &Res<AppTypeRegistry>) -> Result<DynamicScene, ()> {
    let path = "save/gamestate.mp";

    if !Path::new(path).exists() {
        info!("Could not find path");
        return Err(());
    }

    let file = File::open(path).expect("Could not open save file");

    let mut reader = BufReader::new(file);

    let de = SceneDeserializer {
        type_registry: &registry.read(),
    };

    de.deserialize(&mut Deserializer::new(&mut reader)).map_err(|_| ())
}

It doesn't work, returning an Err at de.deserialize.

The error: Value Syntax("invalid type: string \"bevy_transform::components::transform::Transform\", expected a borrowed string")

Here are my imports:

use bevy::{
    prelude::*,
    scene::serde::{SceneSerializer, SceneDeserializer}, tasks::IoTaskPool,
};

use rmp_serde::{Serializer, Deserializer};
use serde::{Serialize, de::DeserializeSeed};

fn main() {
  App::new()
    .add_plugins(DefaultPlugins)
    .add_startup_system(setup_state);
}

fn setup_state(
  registry: Res<AppTypeRegistry>,
) {
  load_state(&registry);
}

Version: 0.9.1
Platform: Linux x86-64

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ReflectionRuntime information about typesA-ScenesSerialized ECS data stored on the diskC-BugAn unexpected or incorrect behavior

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions