|
2 | 2 | use std::fs::File;
|
3 | 3 | use std::io::Write;
|
4 | 4 |
|
5 |
| -use bevy::{prelude::*, utils::Duration}; |
| 5 | +use bevy::{prelude::*, tasks::IoTaskPool, utils::Duration}; |
6 | 6 |
|
7 | 7 | fn main() {
|
8 | 8 | App::new()
|
@@ -104,17 +104,23 @@ fn save_scene_system(world: &mut World) {
|
104 | 104 | let scene = DynamicScene::from_world(&scene_world, type_registry);
|
105 | 105 |
|
106 | 106 | // Scenes can be serialized like this:
|
107 |
| - info!("{}", scene.serialize_ron(type_registry).unwrap()); |
108 |
| - |
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())) |
| 107 | + let serialized_scene = scene.serialize_ron(type_registry).unwrap(); |
| 108 | + |
| 109 | + // Showing the scene in the console |
| 110 | + info!("{}", serialized_scene); |
| 111 | + |
| 112 | + // Writing the scene to a new file. Using a task to avoid calling the filesystem APIs in a system |
| 113 | + // as they are blocking |
| 114 | + // This can't work in WASM as there is no filesystem access |
| 115 | + #[cfg(not(target_arch = "wasm32"))] |
| 116 | + IoTaskPool::get() |
| 117 | + .spawn(async move { |
| 118 | + // Write the scene RON data to file |
| 119 | + File::create(format!("assets/{}", NEW_SCENE_FILE_PATH)) |
| 120 | + .and_then(|mut file| file.write(serialized_scene.as_bytes())) |
| 121 | + .expect("Error while writing scene to file"); |
116 | 122 | })
|
117 |
| - .expect("Error while writing scene to file"); |
| 123 | + .detach(); |
118 | 124 | }
|
119 | 125 |
|
120 | 126 | // This is only necessary for the info message in the UI. See examples/ui/text.rs for a standalone
|
|
0 commit comments