Skip to content

Commit

Permalink
Add writing of scene data to Scene example (bevyengine#5949)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
wanderrful authored and james7132 committed Oct 28, 2022
1 parent c099568 commit aff8d5c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
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

0 comments on commit aff8d5c

Please sign in to comment.