bevy/crates
Francesco 7b38de0a64
(De) serialize resources in scenes (#6846)
# Objective

Co-Authored-By: davier
[bricedavier@gmail.com](mailto: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:
https://github.com/bevyengine/bevy/pull/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 <mcanders1@gmail.com>
2023-03-20 21:17:02 +00:00
..
bevy_a11y chore: Release (#7920) 2023-03-06 05:13:36 +00:00
bevy_animation Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
bevy_app Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
bevy_asset Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
bevy_audio Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
bevy_core Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
bevy_core_pipeline Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
bevy_derive chore: Release (#7920) 2023-03-06 05:13:36 +00:00
bevy_diagnostic Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
bevy_dylib chore: Release (#7920) 2023-03-06 05:13:36 +00:00
bevy_dynamic_plugin chore: Release (#7920) 2023-03-06 05:13:36 +00:00
bevy_ecs Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
bevy_ecs_compile_fail_tests Update trybuild tests for Rust 1.68 (#8002) 2023-03-09 15:46:06 +00:00
bevy_encase_derive chore: Release (#7920) 2023-03-06 05:13:36 +00:00
bevy_gilrs Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
bevy_gizmos Immediate Mode Line/Gizmo Drawing (#6529) 2023-03-20 20:57:54 +00:00
bevy_gltf chore: Release (#7920) 2023-03-06 05:13:36 +00:00
bevy_hierarchy Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
bevy_input Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
bevy_internal Immediate Mode Line/Gizmo Drawing (#6529) 2023-03-20 20:57:54 +00:00
bevy_log chore: Release (#7920) 2023-03-06 05:13:36 +00:00
bevy_macro_utils chore: Release (#7920) 2023-03-06 05:13:36 +00:00
bevy_math chore: Release (#7920) 2023-03-06 05:13:36 +00:00
bevy_mikktspace chore: Release (#7920) 2023-03-06 05:13:36 +00:00
bevy_pbr Left-handed y-up cubemap coordinates (#8122) 2023-03-18 23:06:53 +00:00
bevy_ptr chore: Release (#7920) 2023-03-06 05:13:36 +00:00
bevy_reflect Fix typo in utility.rs (#7997) 2023-03-09 09:20:45 +00:00
bevy_reflect_compile_fail_tests Update trybuild tests for Rust 1.68 (#8002) 2023-03-09 15:46:06 +00:00
bevy_render Allow SPIR-V shaders to process when shader defs are present (#7772) 2023-03-19 09:26:26 +00:00
bevy_scene (De) serialize resources in scenes (#6846) 2023-03-20 21:17:02 +00:00
bevy_sprite Derive Copy and Clone for Collision (#8121) 2023-03-18 04:55:31 +00:00
bevy_tasks chore: Release (#7920) 2023-03-06 05:13:36 +00:00
bevy_text Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
bevy_time Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
bevy_transform Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
bevy_ui Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
bevy_utils chore: Release (#7920) 2023-03-06 05:13:36 +00:00
bevy_window Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
bevy_winit Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00