-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reflect and (de)serialize resources in scenes #3580
Conversation
This is looking very good. That was easier than I expected, and should be incredibly useful. If you can comfortably add a method / pattern to access resource / non-resource archetypes in a less fragile way that would be lovely, but I can live without it. I'll work on compiling a list of resources to reflect now. |
Co-authored-by: Alice Cecile <[email protected]>
Reflect and Serialize
Just Reflect
Nothing
|
Overall, it looks like Serialize is likely not appropriate for resources that are about working with assets or physical hardware (or when dealing with |
It's possible to use
Reflection and serialization are closely related, I'm not sure we can have the first without the second with the current derive macro (as discussed on discord, but I'm repeating it here for others). I'll investigate. Thank you @alice-i-cecile for the research! I'll start by reflecting the simple "config"-only resources, but I might leave the ones with actual content to their own PRs. |
I did a pass over every resource, and selected those that only contain configuration (i.e. no state), and that can be changed after startup: I have not yet reflected Reflecting I'm not sure these are useful to reflect: These resources are only read during startup: These resources contain state, so I think they deserve discussion on what part should or should not be reflected in their own PRs: |
IMO they can just depend on it unconditionally, just like |
Currently this PR will put every reflectable resource into the scene, right? I don't think that this is a very good policy, as there are a lot of resources like What do you think about splitting this PR into one for |
You are talking about using For your example of
Both are implemented, I just need some time to rebase, and write some examples and tests. I can split the PR in two if we don't have a consensus on the second part 🙂 (I'll probably be to busy to do it until the end of march though) |
Agreed.
IMO yes.
I think this is a good idea regardless; the functionality is related but distinct, and optimizing for merge-ability is a good call. |
@Davier if you have the chance, can you split out the reflection changes from the "add resources to scenes"? The former is uncontroversial, but the latter needs more consideration. |
let reflect_resource = type_registry.get_type_data::<ReflectResource>(type_id)?;
let resource: &dyn Reflect = reflect_resource.reflect_resource(world)?;
reflect_resource.insert_resource(world, some_resource_value /* dyn Reflect */);
reflect_resource.remove_resource(world); you can do let reflect_from_ptr = type_registry.get_type_data::<ReflectFromPtr>(type_id)?;
let component_id = world.get_id(type_id)?;
let resource: Ptr<'_> = world.get_resource_by_id(component_id);
let resource: &dyn Reflect = unsafe { reflect_from_ptr.as_reflect(resource) }; // SAFE: resource is of the type that the `ReflectFromPtr` was made for
world.insert_resource_by_id(component_id, some_resource_value /* OwnedPtr */);
world.remove_resource_by_id(component_id); |
Very cool, but I think there's a lot of value in having a fully safe API for a relatively common task. |
Closing in favor of #5175 as it is much less controversial. We can tackle "should resources be stored in scenes" when we tackle more comprehensive design reviews of how scenes and prefabs work. |
# Objective We don't have reflection for resources. ## Solution Introduce reflection for resources. Continues #3580 (by @Davier), related to #3576. --- ## Changelog ### Added * Reflection on a resource type (by adding `ReflectResource`): ```rust #[derive(Reflect)] #[reflect(Resource)] struct MyResourse; ``` ### Changed * Rename `ReflectComponent::add_component` into `ReflectComponent::insert_component` for consistency. ## Migration Guide * Rename `ReflectComponent::add_component` into `ReflectComponent::insert_component`.
# Objective We don't have reflection for resources. ## Solution Introduce reflection for resources. Continues bevyengine#3580 (by @Davier), related to bevyengine#3576. --- ## Changelog ### Added * Reflection on a resource type (by adding `ReflectResource`): ```rust #[derive(Reflect)] #[reflect(Resource)] struct MyResourse; ``` ### Changed * Rename `ReflectComponent::add_component` into `ReflectComponent::insert_component` for consistency. ## Migration Guide * Rename `ReflectComponent::add_component` into `ReflectComponent::insert_component`.
# Objective We don't have reflection for resources. ## Solution Introduce reflection for resources. Continues bevyengine#3580 (by @Davier), related to bevyengine#3576. --- ## Changelog ### Added * Reflection on a resource type (by adding `ReflectResource`): ```rust #[derive(Reflect)] #[reflect(Resource)] struct MyResourse; ``` ### Changed * Rename `ReflectComponent::add_component` into `ReflectComponent::insert_component` for consistency. ## Migration Guide * Rename `ReflectComponent::add_component` into `ReflectComponent::insert_component`.
# Objective We don't have reflection for resources. ## Solution Introduce reflection for resources. Continues bevyengine#3580 (by @Davier), related to bevyengine#3576. --- ## Changelog ### Added * Reflection on a resource type (by adding `ReflectResource`): ```rust #[derive(Reflect)] #[reflect(Resource)] struct MyResourse; ``` ### Changed * Rename `ReflectComponent::add_component` into `ReflectComponent::insert_component` for consistency. ## Migration Guide * Rename `ReflectComponent::add_component` into `ReflectComponent::insert_component`.
# Objective Co-Authored-By: davier [[email protected]](mailto:[email protected]) Fixes #3576. Adds a `resources` field in scene serialization data to allow de/serializing resources that have reflection enabled. ## Solution Most of this code is taken from a previous closed PR: #3580. Most of the credit goes to @Davier , what I did was mostly getting it to work on the latest main branch of Bevy, along with adding a few asserts in the currently existing tests to be sure everything is working properly. This PR changes the scene format to include resources in this way: ``` ( resources: { // List of resources here, keyed by resource type name. }, entities: [ // Previous scene format here ], ) ``` An example taken from the tests: ``` ( resources: { "bevy_scene::serde::tests::MyResource": ( foo: 123, ), }, entities: { // Previous scene format here }, ) ``` For this, a `resources` fields has been added on the `DynamicScene` and the `DynamicSceneBuilder` structs. The latter now also has a method named `extract_resources` to properly extract the existing resources registered in the local type registry, in a similar way to `extract_entities`. --- ## Changelog Added: Reflect resources registered in the type registry used by dynamic scenes will now be properly de/serialized in scene data. ## Migration Guide Since the scene format has been changed, the user may not be able to use scenes saved prior to this PR due to the `resources` scene field being missing. ~~To preserve backwards compatibility, I will try to make the `resources` fully optional so that old scenes can be loaded without issue.~~ ## TODOs - [x] I may have to update a few doc blocks still referring to dynamic scenes as mere container of entities, since they now include resources as well. - [x] ~~I want to make the `resources` key optional, as specified in the Migration Guide, so that old scenes will be compatible with this change.~~ Since this would only be trivial for ron format, I think it might be better to consider it in a separate PR/discussion to figure out if it could be done for binary serialization too. - [x] I suppose it might be a good idea to add a resources in the scene example so that users will quickly notice they can serialize resources just like entities. --------- Co-authored-by: Carter Anderson <[email protected]>
Objective
Fixes #3576.
Enables editors/inspectors to show and edit resources.
Solution
ReflectResource
)DynamicScene
s and implement their (de)serializationScene
s andDynamicScene
s to actually import/export reflected resources from the worldRemaining work
Notes
I ignored non send resources as I don't think they should be loaded from scenes, but it may make sense to reflect them so that they are accessible to editors/inspectors.