|
1 | 1 | //! This example illustrates loading scenes from files.
|
| 2 | +use std::fs::File; |
| 3 | +use std::io::Write; |
2 | 4 |
|
3 | 5 | use bevy::{prelude::*, utils::Duration};
|
4 | 6 |
|
@@ -49,12 +51,18 @@ impl FromWorld for ComponentB {
|
49 | 51 | }
|
50 | 52 | }
|
51 | 53 |
|
| 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 | + |
52 | 60 | fn load_scene_system(mut commands: Commands, asset_server: Res<AssetServer>) {
|
53 | 61 | // "Spawning" a scene bundle creates a new entity and spawns new instances
|
54 | 62 | // of the given scene's entities as children of that entity.
|
55 | 63 | commands.spawn_bundle(DynamicSceneBundle {
|
56 | 64 | // 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), |
58 | 66 | ..default()
|
59 | 67 | });
|
60 | 68 |
|
@@ -98,7 +106,15 @@ fn save_scene_system(world: &mut World) {
|
98 | 106 | // Scenes can be serialized like this:
|
99 | 107 | info!("{}", scene.serialize_ron(type_registry).unwrap());
|
100 | 108 |
|
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"); |
102 | 118 | }
|
103 | 119 |
|
104 | 120 | // This is only necessary for the info message in the UI. See examples/ui/text.rs for a standalone
|
|
0 commit comments