use bevy::{input::keyboard::KeyboardInput, prelude::*, type_registry::TypeRegistry}; fn main() { App::build() .add_default_plugins() // Registering components informs Bevy that they exist. This allows them to be used when loading scenes // This step is only required if you want to load your components from scene files. // Unregistered components can still be used in your code, but they won't be serialized / deserialized. // In the future registering components will also make them usable from the Bevy editor. // The core Bevy plugins already register their components, so you only need this step for custom components. .register_component::() .register_component::() .add_startup_system(save_scene_system) .add_startup_system(load_scene_system) .run(); } // Registered components must implement the `Properties` and `FromResources` traits. // The `Properties` trait enables serialization, deserialization, dynamic property access, and change detection. // `Properties` enable a bunch of cool behaviors, so its worth checking out the dedicated `properties.rs` example. // The `FromResources` trait determines how your component is constructed. // For simple use cases you can just implement the `Default` trait (which automatically implements FromResources) // The simplest registered component just needs these two derives: #[derive(Properties, Default)] struct ComponentA { pub x: f32, pub y: f32, } // Some components have fields that cannot (or should not) be written to scene files. These can be ignored with // the #[property(ignore)] attribute. This is also generally where the `FromResources` trait comes into play. // This gives you access to your App's current ECS `Resources` when you construct your component. #[derive(Properties)] struct ComponentB { pub value: String, #[property(ignore)] pub event_reader: EventReader, } impl FromResources for ComponentB { fn from_resources(resources: &Resources) -> Self { let event_reader = resources.get_event_reader::(); ComponentB { event_reader, value: "Default Value".to_string(), } } } fn save_scene_system(world: &mut World, resources: &mut Resources) { // Scenes can be created from any ECS World. world .build() .build_entity() .add(ComponentA { x: 1.0, y: 2.0 }) .add(ComponentB { value: "hello".to_string(), ..ComponentB::from_resources(resources) }) .build_entity() .add(ComponentA { x: 3.0, y: 4.0 }); // The component registry resource contains information about all registered components. This is used to construct scenes. let type_registry = resources.get::().unwrap(); let scene = Scene::from_world(world, &type_registry.component.read().unwrap()); // Scenes can be serialized like this: println!( "{}", scene .serialize_ron(&type_registry.property.read().unwrap()) .unwrap() ); // TODO: save scene } fn load_scene_system(world: &mut World, resources: &mut Resources) { let asset_server = resources.get::().unwrap(); let mut scenes = resources.get_mut::>().unwrap(); // Scenes are loaded just like any other asset. let scene_handle: Handle = asset_server .load_sync(&mut scenes, "assets/scene/load_scene_example.scn") .unwrap(); let scene = scenes.get(&scene_handle).unwrap(); // Scenes can be added to any ECS World. Adding scenes also uses the component registry. let type_registry = resources.get::().unwrap(); scene .add_to_world(world, resources, &type_registry.component.read().unwrap()) .unwrap(); }