-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
(De) serialize resources in scenes #6846
Conversation
refactor: apply review suggestions feat: add example in scene.rs for resource de/serialization
# Conflicts: # crates/bevy_scene/src/dynamic_scene.rs
…SceneBuilder doc blocks
refactor: ReflectArraySerializer -> SceneMapSerializer, ReflectDeserializer -> SceneMapDeserializer, ReflectVisitor -> SceneMapVisitor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few remaining comments, but overall LGTM!
I'm not sure writing a scene to a world should always write the resources. Unlike entities, this would overwrite existing resources. This seems like a desirable behaviour sometimes but not always. |
Theoretically this should only overwrite the resources selected by the user in their passed |
Yes, but that would mean having multiple registries depending on what you would want to do. It's easier to think with one registry per scene. I am not sure having the same behavior for entities and resources makes sense, writing the entities only create new ones, writing resources replace them. Having that being implicit doesn't feel nice, I may be wrong. A reason to not want to rewrite them would be for a reusable scene that can need a few resources on its first spawn, but can reuse them after |
Yeah this is what I think this is really needed for. For scenes that are more like save files, this really helps restore the full world state (well, more or less). But I think this type of scene will generally be treated much differently than a transient one (e.g. networking) or ones that are written to a world multiple times (e.g. prefabs). So in these cases, we can expect them to rely on different filters. And this can be made a lot nicer with something like #6793 where we have an actual filter type rather than multiple registries. So imo this PR would be really nice to have. |
Sorry for the late answer. I understand this might not always be the desired behaviour, but I think it's an acceptable default behaviour. The reason for this is that resources represent globally unique data in a Bevy app, therefore it seems natural to me that deserialization implies overwriting the current values with the loaded ones. The only other ideas that I can think of would be either manually handling deserializing a resource, or having some sort of scene-scoped resource.
As @MrGVSV stated, using a specific |
# Conflicts: # crates/bevy_scene/src/dynamic_scene_builder.rs
…source feat: specific test for DynamicSceneBuilder extract_resource
I resolved the conflicts, added a test in the dynamic_scene_builder.rs to test the extract_method in detail and removed the world argument in favor of self.original_world to be consistent with the extract_entities method. The PR should be ready, I believe. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
I agree with François, implicitly adding resources seems like a potential footgun. But with the current bevy tools, there is simply no way of not doing it this way.
The ability to load resources from scenes is necessary first before being capable of developing tools to filter resources when loading them, if we wait the tools before adding loading, then the snake bites the tail of the chicken's egg and we will wait forever.
# Conflicts: # crates/bevy_scene/src/serde.rs
# Conflicts: # crates/bevy_scene/src/dynamic_scene_builder.rs # crates/bevy_scene/src/serde.rs
…effect if attempted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation is great and I agree with the sentiment expressed here: this is a good first step into the space. I strongly suspect we will need more involved and flexible resource workflows in our scenes (ex: we need to support inline assets, which can't just overwrite the current asset state), but this is a reasonable first step.
Objective
Co-Authored-By: davier bricedavier@gmail.com
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:
An example taken from the tests:
For this, a
resources
fields has been added on theDynamicScene
and theDynamicSceneBuilder
structs. The latter now also has a method namedextract_resources
to properly extract the existing resources registered in the local type registry, in a similar way toextract_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 theresources
fully optional so that old scenes can be loaded without issue.TODOs
I want to make theSince 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.resources
key optional, as specified in the Migration Guide, so that old scenes will be compatible with this change.