Skip to content

Commit f83a9c2

Browse files
committed
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.
1 parent bb7f521 commit f83a9c2

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ Cargo.lock
77
/.idea
88
/.vscode
99
/benches/target
10+
11+
# Generated by "examples/scene/scene.rs"
12+
assets/scenes/load_scene_example-new.scn.ron

examples/scene/scene.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//! This example illustrates loading scenes from files.
2+
use std::fs::File;
3+
use std::io::Write;
24

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

@@ -49,12 +51,18 @@ impl FromWorld for ComponentB {
4951
}
5052
}
5153

54+
// The initial scene file will be loaded below and not change when the scene is saved
55+
const SCENE_FILE_PATH: &str = "scenes/load_scene_example.scn.ron";
56+
57+
// The new, updated scene data will be saved here so that you can see the changes
58+
const NEW_SCENE_FILE_PATH: &str = "scenes/load_scene_example-new.scn.ron";
59+
5260
fn load_scene_system(mut commands: Commands, asset_server: Res<AssetServer>) {
5361
// "Spawning" a scene bundle creates a new entity and spawns new instances
5462
// of the given scene's entities as children of that entity.
5563
commands.spawn_bundle(DynamicSceneBundle {
5664
// Scenes are loaded just like any other asset.
57-
scene: asset_server.load("scenes/load_scene_example.scn.ron"),
65+
scene: asset_server.load(SCENE_FILE_PATH),
5866
..default()
5967
});
6068

@@ -98,7 +106,15 @@ fn save_scene_system(world: &mut World) {
98106
// Scenes can be serialized like this:
99107
info!("{}", scene.serialize_ron(type_registry).unwrap());
100108

101-
// TODO: save scene
109+
// Write the scene RON data to file (leveraging From<io::Error> for ron::error::Error)
110+
File::create(format!("assets/{}", NEW_SCENE_FILE_PATH))
111+
.map_err(|err| err.into())
112+
.and_then(|mut file| {
113+
scene
114+
.serialize_ron(type_registry)
115+
.and_then(|data| file.write(data.as_bytes()).map_err(|err| err.into()))
116+
})
117+
.expect("Error while writing scene to file");
102118
}
103119

104120
// This is only necessary for the info message in the UI. See examples/ui/text.rs for a standalone

0 commit comments

Comments
 (0)