Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add writing of scene data to Scene example #5949

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ Cargo.lock
/.idea
/.vscode
/benches/target

# Generated by "examples/scene/scene.rs"
assets/scenes/load_scene_example-new.scn.ron
20 changes: 18 additions & 2 deletions examples/scene/scene.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! This example illustrates loading scenes from files.
use std::fs::File;
use std::io::Write;

use bevy::{prelude::*, utils::Duration};

Expand Down Expand Up @@ -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()
});

Expand Down Expand Up @@ -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
Expand Down