mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 04:33:37 +00:00
Add writing of scene data to Scene example (#5949)
# Objective Alice says to make this PR: https://discord.com/channels/691052431525675048/745805740274614303/1018554340841107477 - The "scene" example in the examples folder has a TODO comment about writing the serialized data to a file. This PR implements that. ## Solution The `AssetIo` trait in the `AssetServer` only supports reading data, not writing it. So, I used `std::io::File` for the implementation. This way, every time you run the example, it will mutate the file in-place. I had thought about adding a UUID string to the example Component, so that every time you run the example, the file will be guaranteed to change (currently, it just writes the same numbers over and over). However, I didn't bother because it was beyond the scope of the TODO comment. One thing to note is that the logic for serializing the scene into RON data has changed since the existing RON file was created, and so even though the data is the same, it's rendered in a different order for whatever reason. I left the changed output to the example file, because it's presumably trivial. I can remove it and force-push if you don't want that included in here.
This commit is contained in:
parent
bb7f521f91
commit
f83a9c23f2
2 changed files with 21 additions and 2 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -7,3 +7,6 @@ Cargo.lock
|
|||
/.idea
|
||||
/.vscode
|
||||
/benches/target
|
||||
|
||||
# Generated by "examples/scene/scene.rs"
|
||||
assets/scenes/load_scene_example-new.scn.ron
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
//! This example illustrates loading scenes from files.
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
|
||||
use bevy::{prelude::*, utils::Duration};
|
||||
|
||||
|
@ -49,12 +51,18 @@ impl FromWorld for ComponentB {
|
|||
}
|
||||
}
|
||||
|
||||
// The initial scene file will be loaded below and not change when the scene is saved
|
||||
const SCENE_FILE_PATH: &str = "scenes/load_scene_example.scn.ron";
|
||||
|
||||
// The new, updated scene data will be saved here so that you can see the changes
|
||||
const NEW_SCENE_FILE_PATH: &str = "scenes/load_scene_example-new.scn.ron";
|
||||
|
||||
fn load_scene_system(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
// "Spawning" a scene bundle creates a new entity and spawns new instances
|
||||
// of the given scene's entities as children of that entity.
|
||||
commands.spawn_bundle(DynamicSceneBundle {
|
||||
// Scenes are loaded just like any other asset.
|
||||
scene: asset_server.load("scenes/load_scene_example.scn.ron"),
|
||||
scene: asset_server.load(SCENE_FILE_PATH),
|
||||
..default()
|
||||
});
|
||||
|
||||
|
@ -98,7 +106,15 @@ fn save_scene_system(world: &mut World) {
|
|||
// Scenes can be serialized like this:
|
||||
info!("{}", scene.serialize_ron(type_registry).unwrap());
|
||||
|
||||
// TODO: save scene
|
||||
// Write the scene RON data to file (leveraging From<io::Error> for ron::error::Error)
|
||||
File::create(format!("assets/{}", NEW_SCENE_FILE_PATH))
|
||||
.map_err(|err| err.into())
|
||||
.and_then(|mut file| {
|
||||
scene
|
||||
.serialize_ron(type_registry)
|
||||
.and_then(|data| file.write(data.as_bytes()).map_err(|err| err.into()))
|
||||
})
|
||||
.expect("Error while writing scene to file");
|
||||
}
|
||||
|
||||
// This is only necessary for the info message in the UI. See examples/ui/text.rs for a standalone
|
||||
|
|
Loading…
Reference in a new issue