mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 15:14:50 +00:00
feat: remove_component for ReflectComponent (#2682)
# Objective
While implementing a plugin for my rollback networking library, I needed to load/save parts of the world. For this, I made a WorldSnapshot that works quite like the current DynamicScene. Using a TypeRegistry to register component types I want to save/load and then using ReflectComponents methods to add or apply components of the given types.
However, I noticed there is no method to remove components from entities through the ReflectComponent.
## Solution
I added a `remove_component` field to the `ReflectComponent` struct, as well as a `pub fn remove_component(&self, world: &mut World, entity: Entity)` to call that function in `remove_component`. This follows exactly the same pattern all other methods/fields in this struct look like.
This is an example how it could be used (at least how I would use it):
6c003f86f1/src/world_snapshot.rs (L133)
This commit is contained in:
parent
9d453530fa
commit
98a08cf495
1 changed files with 8 additions and 0 deletions
|
@ -10,6 +10,7 @@ use bevy_reflect::{impl_reflect_value, FromType, Reflect, ReflectDeserialize};
|
|||
pub struct ReflectComponent {
|
||||
add_component: fn(&mut World, Entity, &dyn Reflect),
|
||||
apply_component: fn(&mut World, Entity, &dyn Reflect),
|
||||
remove_component: fn(&mut World, Entity),
|
||||
reflect_component: fn(&World, Entity) -> Option<&dyn Reflect>,
|
||||
reflect_component_mut: unsafe fn(&World, Entity) -> Option<ReflectMut>,
|
||||
copy_component: fn(&World, &mut World, Entity, Entity),
|
||||
|
@ -24,6 +25,10 @@ impl ReflectComponent {
|
|||
(self.apply_component)(world, entity, component);
|
||||
}
|
||||
|
||||
pub fn remove_component(&self, world: &mut World, entity: Entity) {
|
||||
(self.remove_component)(world, entity);
|
||||
}
|
||||
|
||||
pub fn reflect_component<'a>(
|
||||
&self,
|
||||
world: &'a World,
|
||||
|
@ -83,6 +88,9 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
|
|||
let mut component = world.get_mut::<C>(entity).unwrap();
|
||||
component.apply(reflected_component);
|
||||
},
|
||||
remove_component: |world, entity| {
|
||||
world.entity_mut(entity).remove::<C>();
|
||||
},
|
||||
copy_component: |source_world, destination_world, source_entity, destination_entity| {
|
||||
let source_component = source_world.get::<C>(source_entity).unwrap();
|
||||
let mut destination_component = C::from_world(destination_world);
|
||||
|
|
Loading…
Reference in a new issue