Skip to content
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

Use FromReflect when extracting entities in dynamic scenes #15174

Merged
merged 9 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions crates/bevy_scene/src/dynamic_scene_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy_ecs::{
reflect::{AppTypeRegistry, ReflectComponent, ReflectResource},
world::World,
};
use bevy_reflect::PartialReflect;
use bevy_reflect::{PartialReflect, ReflectFromReflect};
use bevy_utils::default;
use std::collections::BTreeMap;

Expand Down Expand Up @@ -274,11 +274,22 @@ impl<'w> DynamicSceneBuilder<'w> {
return None;
}

let component = type_registry
.get(type_id)?
let type_registration = type_registry.get(type_id)?;

let component = type_registration
.data::<ReflectComponent>()?
.reflect(original_entity)?;
entry.components.push(component.clone_value());

// Clone the via `FromReflect`. Unlike `PartialReflect::clone_value` this
yrns marked this conversation as resolved.
Show resolved Hide resolved
// retains the original type and `ReflectSerialize` type data which is needed to
// deserialize.
yrns marked this conversation as resolved.
Show resolved Hide resolved
let component = type_registration
.data::<ReflectFromReflect>()
.and_then(|fr| fr.from_reflect(component.as_partial_reflect()))
.map(PartialReflect::into_partial_reflect)
.unwrap_or_else(|| component.clone_value());

entry.components.push(component);
Some(())
};
extract_and_push();
Expand Down
18 changes: 14 additions & 4 deletions crates/bevy_scene/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
use crate::{DynamicEntity, DynamicScene};
use bevy_ecs::entity::Entity;
use bevy_reflect::serde::{TypedReflectDeserializer, TypedReflectSerializer};
use bevy_reflect::PartialReflect;
use bevy_reflect::{
serde::{ReflectDeserializer, TypeRegistrationDeserializer},
TypeRegistry,
};
use bevy_reflect::{PartialReflect, ReflectFromReflect};
use bevy_utils::HashSet;
use serde::ser::SerializeMap;
use serde::{
Expand Down Expand Up @@ -471,9 +471,19 @@ impl<'a, 'de> Visitor<'de> for SceneMapVisitor<'a> {
)));
}

entries.push(
map.next_value_seed(TypedReflectDeserializer::new(registration, self.registry))?,
);
let value =
map.next_value_seed(TypedReflectDeserializer::new(registration, self.registry))?;

// Attempt to convert using FromReflect.
let value = self
.registry
.get(registration.type_id())
.and_then(|tr| tr.data::<ReflectFromReflect>())
.and_then(|fr| fr.from_reflect(value.as_partial_reflect()))
.map(PartialReflect::into_partial_reflect)
.unwrap_or(value);

entries.push(value);
}

Ok(entries)
Expand Down