bevy_ecs::reflect

Trait ReflectCommandExt

Source
pub trait ReflectCommandExt {
    // Required methods
    fn insert_reflect(
        &mut self,
        component: Box<dyn PartialReflect>,
    ) -> &mut Self;
    fn insert_reflect_with_registry<T: Resource + AsRef<TypeRegistry>>(
        &mut self,
        component: Box<dyn PartialReflect>,
    ) -> &mut Self;
    fn remove_reflect(
        &mut self,
        component_type_name: impl Into<Cow<'static, str>>,
    ) -> &mut Self;
    fn remove_reflect_with_registry<T: Resource + AsRef<TypeRegistry>>(
        &mut self,
        component_type_name: impl Into<Cow<'static, str>>,
    ) -> &mut Self;
}
Available on crate feature bevy_reflect only.
Expand description

An extension trait for EntityCommands for reflection related functions

Required Methods§

Source

fn insert_reflect(&mut self, component: Box<dyn PartialReflect>) -> &mut Self

Adds the given boxed reflect component or bundle to the entity using the reflection data in AppTypeRegistry.

This will overwrite any previous component(s) of the same type.

§Panics
§Note

Prefer to use the typed EntityCommands::insert if possible. Adding a reflected component is much slower.

§Example
// Note that you need to register the component type in the AppTypeRegistry prior to using
// reflection. You can use the helpers on the App with `app.register_type::<ComponentA>()`
// or write to the TypeRegistry directly to register all your components

// A resource that can hold any component that implements reflect as a boxed reflect component
#[derive(Resource)]
struct Prefab {
    data: Box<dyn Reflect>,
}
#[derive(Component, Reflect, Default)]
#[reflect(Component)]
struct ComponentA(u32);

#[derive(Component, Reflect, Default)]
#[reflect(Component)]
struct ComponentB(String);

#[derive(Bundle, Reflect, Default)]
#[reflect(Bundle)]
struct BundleA {
    a: ComponentA,
    b: ComponentB,
}

fn insert_reflect_component(
    mut commands: Commands,
    mut prefab: ResMut<Prefab>
    ) {
    // Create a set of new boxed reflect components to use
    let boxed_reflect_component_a: Box<dyn Reflect> = Box::new(ComponentA(916));
    let boxed_reflect_component_b: Box<dyn Reflect>  = Box::new(ComponentB("NineSixteen".to_string()));
    let boxed_reflect_bundle_a: Box<dyn Reflect> = Box::new(BundleA {
        a: ComponentA(24),
        b: ComponentB("Twenty-Four".to_string()),
    });

    // You can overwrite the component in the resource with either ComponentA or ComponentB
    prefab.data = boxed_reflect_component_a;
    prefab.data = boxed_reflect_component_b;

    // Or even with BundleA
    prefab.data = boxed_reflect_bundle_a;

    // No matter which component or bundle is in the resource and without knowing the exact type, you can
    // use the insert_reflect entity command to insert that component/bundle into an entity.
    commands
        .spawn_empty()
        .insert_reflect(prefab.data.clone_value());
}
Source

fn insert_reflect_with_registry<T: Resource + AsRef<TypeRegistry>>( &mut self, component: Box<dyn PartialReflect>, ) -> &mut Self

Same as insert_reflect, but using the T resource as type registry instead of AppTypeRegistry.

§Panics
§Note
  • The given Resource is removed from the World before the command is applied.
Source

fn remove_reflect( &mut self, component_type_name: impl Into<Cow<'static, str>>, ) -> &mut Self

Removes from the entity the component or bundle with the given type name registered in AppTypeRegistry.

If the type is a bundle, it will remove any components in that bundle regardless if the entity contains all the components.

Does nothing if the type is a component and the entity does not have a component of the same type, if the type is a bundle and the entity does not contain any of the components in the bundle, if AppTypeRegistry does not contain the reflection data for the given component, or if the entity does not exist.

§Note

Prefer to use the typed EntityCommands::remove if possible. Removing a reflected component is much slower.

§Example
// Note that you need to register the component/bundle type in the AppTypeRegistry prior to using
// reflection. You can use the helpers on the App with `app.register_type::<ComponentA>()`
// or write to the TypeRegistry directly to register all your components and bundles


// A resource that can hold any component or bundle that implements reflect as a boxed reflect
#[derive(Resource)]
struct Prefab{
    entity: Entity,
    data: Box<dyn Reflect>,
}
#[derive(Component, Reflect, Default)]
#[reflect(Component)]
struct ComponentA(u32);
#[derive(Component, Reflect, Default)]
#[reflect(Component)]
struct ComponentB(String);
#[derive(Bundle, Reflect, Default)]
#[reflect(Bundle)]
struct BundleA {
    a: ComponentA,
    b: ComponentB,
}

fn remove_reflect_component(
    mut commands: Commands,
    prefab: Res<Prefab>
    ) {
    // Prefab can hold any boxed reflect component or bundle. In this case either
    // ComponentA, ComponentB, or BundleA. No matter which component or bundle is in the resource though,
    // we can attempt to remove any component (or set of components in the case of a bundle)
    // of that same type from an entity.
    commands.entity(prefab.entity)
        .remove_reflect(prefab.data.reflect_type_path().to_owned());
}
Source

fn remove_reflect_with_registry<T: Resource + AsRef<TypeRegistry>>( &mut self, component_type_name: impl Into<Cow<'static, str>>, ) -> &mut Self

Same as remove_reflect, but using the T resource as type registry instead of AppTypeRegistry.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§