#Error when attempting Bevy Docs serialization example

31 messages · Page 1 of 1 (latest)

zenith prawn
#

So I'm following the example given here:

https://github.com/bevyengine/bevy/blob/main/examples/scene/scene.rs

and I can get it to work with their code, but if I instead use the passed in world parameter instead of creating a new world as they did, I get the error

called `Result::unwrap()` on an `Err` value: Message("Type 'std::time::Instant' did not register ReflectSerialize")```

Anyone know what might be up? I have almost every single other line of my code commented out except for this scene plugin and the default bevy plugins
GitHub

A refreshingly simple data-driven game engine built in Rust - bevyengine/bevy

#

Here is the code I am using:

#[reflect(Component)] // this tells the reflect derive to also reflect component behaviors
struct ComponentA {
    pub x: f32,
    pub y: f32,
}
fn save_scene_system(scene_world: &mut World) {
    //create components here for testing
    scene_world.spawn(ComponentA { x: 3.0, y: 4.0 });
    scene_world.spawn(ComponentA { x: 321.0, y: 4.0 });

    scene_world.spawn(ComponentA { x: 432.0, y: 23.0 });

    // With our sample world ready to go, we can now create our scene:
    let scene = DynamicScene::from_world(&scene_world);

    // Scenes can be serialized like this:
    let type_registry = scene_world.resource::<AppTypeRegistry>();
    let serialized_scene = scene.serialize_ron(type_registry).unwrap();

    // Showing the scene in the console
    info!("{}", serialized_scene);

    // Writing the scene to a new file. Using a task to avoid calling the filesystem APIs in a system
    // as they are blocking
    // This can't work in WASM as there is no filesystem access
    #[cfg(not(target_arch = "wasm32"))]
    IoTaskPool::get()
        .spawn(async move {
            // Write the scene RON data to file
            File::create(NEW_SCENE_FILE_PATH)
                .and_then(|mut file| file.write(serialized_scene.as_bytes()))
                .expect("Error while writing scene to file");
        })
        .detach();
}```
trail patrol
#

Did you register the type in the app like: .register_type::<ComponentA>()?

zenith prawn
#

I think so

#

this is my plugin

trail patrol
#

What plugins are you adding to the app? I can only recreate this error message by adding two ScenePlugin.

zenith prawn
#
    info!("Starting app...");
    App::new()
        .add_plugins(
            DefaultPlugins
                //Precision for pixel art style
                .set(AssetPlugin {
                    // This tells the AssetServer to watch for changes to assets.
                    // It enables our scenes to automatically reload in game when we modify their files.
                    watch_for_changes: ChangeWatcher::with_delay(Duration::from_millis(200)),
                    ..default()
                })
                .build(),
        )
        .add_plugins((scene::ScenePlugin,))
        .run();
}```
#

this is it

trail patrol
#

DefaultPlugins already adds the ScenePlugin if the feature it on, which is is by default. Try removing the extra scene::ScenePlugin

zenith prawn
#

the extra ScenePlugin is my own plugin I added

#

with the saving/loading functionality I am using to test

trail patrol
zenith prawn
#

yeah I should probably rename it

#

it seems like the pre-existing world somehow has some entity in there which has the std::time::Instant, but idk how that works

#

bc just going off the example they gave where they create a new world within the ScenePlugin, that works

trail patrol
#

When I created a new World, like in the example, it works. I need to check if DefaultPlugins adds an entity that can't be saved. I need to look it up but there is a way to omitt certain parts of a scene. I remember reading it in the bevy news

zenith prawn
#

the omit thing would be super helpful to know about

#

surprised no one else has run into this yet

trail patrol
zenith prawn
#

extremely helpful thank you!!

trail patrol
#

I did some looking at what is inserted into the world with DefaultPlugins and it adds a few resources including Time, which has Instant values. I guessing, but not 100% sure, that this is what is throwing the error. So, use scene filtering seems like the way to go.

zenith prawn
#

I suspect you're right, I'll check back here if I get it to work, currently battling with this line

let scene = builder.allow::<ComponentA>().build();

"cannot move out of a mutable reference
move occurs because value has type bevy::prelude::DynamicSceneBuilder<'_>, which does not implement the Copy traitrustcClick for full compiler diagnostic
scene.rs(54, 47): value moved due to this method call
dynamic_scene_builder.rs(177, 18): bevy::prelude::DynamicSceneBuilder::<'w>::build takes ownership of the receiver self, which moves value"

trail patrol
#

I think it is because the functions like allow and other builder functions return &mut Self, but when build is called it wants a Self which can only be done by copying or cloning DynamicSceneBuilder. It implements neither of these traits. I feel like build should take a &mut Self like how leafwing-input-manager constructs InputMaps? @maiden stump Is my assumption correct that build() needs a change to &mut self. I can open a pr if so. (Sorry for the ping)

maiden stump
#

I'm not familiar with the details of the scene machinery, but that sounds plausible and I'd happily take a look at a PR

zenith prawn
#
    let scene = builder.build();```
#

works

trail patrol
#

While working on the pr, I saw the tests for the builder and apperently the builder is done like:

let mut builder = DynamicSceneBuilder::from_world(&scene_world);
builder.deny_all().allow::<ComponentA>();
let scene = builder.build();

That's feels a little off that you can't use .build() in the chain. It does have an example in the docs, though https://docs.rs/bevy/latest/bevy/scene/prelude/struct.DynamicSceneBuilder.html. Should this stay the same or allow for calling the builder like this?

let mut builder = DynamicSceneBuilder::from_world(&scene_world);
let scene = builder.deny_all().allow::<ComponentA>().build();
zenith prawn
#

really appreciate the help, I never would have found this stuff on my own