Skip to content

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

Closed

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

Activity

  1. added
    C-BugAn unexpected or incorrect behavior
    A-ReflectionRuntime information about types
    A-ScenesComposing and serializing ECS objects
    on Dec 8, 2022
  2. moved this from Open to In Progress in Reflection (Old)on Dec 8, 2022
  3. MrGVSV commented on Dec 8, 2022

    @MrGVSV
    Member

    This seems to also affect reflection's deserialization as well. I have a fix and will put up a PR shortly!

  4. added 2 commits that reference this issue on Jan 4, 2023
    6bbc9eb
    717def2
  5. moved this from In Progress to Done in Reflection (Old)on Jan 4, 2023
  6. added a commit that references this issue on Jan 22, 2023
    0e89423
  7. added a commit that references this issue on Feb 1, 2023
    af1bb0f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ReflectionRuntime information about typesA-ScenesComposing and serializing ECS objectsC-BugAn unexpected or incorrect behavior

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions