Skip to content

Commit

Permalink
bevy_reflect: Fix DynamicScene not respecting component registratio…
Browse files Browse the repository at this point in the history
…ns during serialization (bevyengine#6288)

# Objective

When running the scene example, you might notice we end up printing out the following:
```ron
// ...
{
  "scene::ComponentB": (
    value: "hello",
    _time_since_startup: (
      secs: 0,
      nanos: 0,
    ),
  ),
},
// ...
```

We should not be printing out `_time_since_startup` as the field is marked with `#[reflect(skip_serializing)]`:

```rust
#[derive(Component, Reflect)]
#[reflect(Component)]
struct ComponentB {
  pub value: String,
  #[reflect(skip_serializing)]
  pub _time_since_startup: Duration,
}
```

This is because when we create the `DynamicScene`, we end up calling `Reflect::clone_value`:

https://github.com/bevyengine/bevy/blob/82126697ee4f635cf6b22e0b9f25e5aca95fda4a/crates/bevy_scene/src/dynamic_scene_builder.rs#L114-L114

This results in non-Value types being cloned into Dynamic types, which means the `TypeId` returned from `reflected_value.type_id()` is not the same as the original component's. 

And this meant we were not able to locate the correct `TypeRegistration`.

## Solution

Use `TypeInfo::type_id()` instead of calling `Any::type_id()` on the value directly.

---

## Changelog

* Fix a bug introduced in `0.9.0-dev` where scenes disregarded component's type registrations
  • Loading branch information
MrGVSV authored and Pietrek14 committed Dec 17, 2022
1 parent dfcb3c0 commit c1cc9c1
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions crates/bevy_reflect/src/serde/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl<'a> Serialize for StructSerializer<'a> {

let serialization_data = self
.registry
.get(self.struct_value.type_id())
.get(type_info.type_id())
.and_then(|registration| registration.data::<SerializationData>());
let ignored_len = serialization_data.map(|data| data.len()).unwrap_or(0);
let mut state = serializer.serialize_struct(
Expand Down Expand Up @@ -255,7 +255,7 @@ impl<'a> Serialize for TupleStructSerializer<'a> {

let serialization_data = self
.registry
.get(self.tuple_struct.type_id())
.get(type_info.type_id())
.and_then(|registration| registration.data::<SerializationData>());
let ignored_len = serialization_data.map(|data| data.len()).unwrap_or(0);
let mut state = serializer.serialize_tuple_struct(
Expand Down

0 comments on commit c1cc9c1

Please sign in to comment.