diff --git a/crates/bevy_ecs/src/reflect/component.rs b/crates/bevy_ecs/src/reflect/component.rs index 03b3601773..94b36d0953 100644 --- a/crates/bevy_ecs/src/reflect/component.rs +++ b/crates/bevy_ecs/src/reflect/component.rs @@ -211,6 +211,26 @@ impl ReflectComponent { pub fn new(fns: ReflectComponentFns) -> Self { Self(fns) } + + /// The underlying function pointers implementing methods on `ReflectComponent`. + /// + /// This is useful when you want to keep track locally of an individual + /// function pointer. + /// + /// Calling [`TypeRegistry::get`] followed by + /// [`TypeRegistration::data::`] can be costly if done several + /// times per frame. Consider cloning [`ReflectComponent`] and keeping it + /// between frames, cloning a `ReflectComponent` is very cheap. + /// + /// If you only need a subset of the methods on `ReflectComponent`, + /// use `fn_pointers` to get the underlying [`ReflectComponentFns`] + /// and copy the subset of function pointers you care about. + /// + /// [`TypeRegistration::data::`]: bevy_reflect::TypeRegistration::data + /// [`TypeRegistry::get`]: bevy_reflect::TypeRegistry::get + pub fn fn_pointers(&self) -> &ReflectComponentFns { + &self.0 + } } impl FromType for ReflectComponent { diff --git a/crates/bevy_ecs/src/reflect/resource.rs b/crates/bevy_ecs/src/reflect/resource.rs index 64dc8c1017..924eb54b09 100644 --- a/crates/bevy_ecs/src/reflect/resource.rs +++ b/crates/bevy_ecs/src/reflect/resource.rs @@ -142,6 +142,26 @@ impl ReflectResource { pub fn new(&self, fns: ReflectResourceFns) -> Self { Self(fns) } + + /// The underlying function pointers implementing methods on `ReflectResource`. + /// + /// This is useful when you want to keep track locally of an individual + /// function pointer. + /// + /// Calling [`TypeRegistry::get`] followed by + /// [`TypeRegistration::data::`] can be costly if done several + /// times per frame. Consider cloning [`ReflectResource`] and keeping it + /// between frames, cloning a `ReflectResource` is very cheap. + /// + /// If you only need a subset of the methods on `ReflectResource`, + /// use `fn_pointers` to get the underlying [`ReflectResourceFns`] + /// and copy the subset of function pointers you care about. + /// + /// [`TypeRegistration::data::`]: bevy_reflect::TypeRegistration::data + /// [`TypeRegistry::get`]: bevy_reflect::TypeRegistry::get + pub fn fn_pointers(&self) -> &ReflectResourceFns { + &self.0 + } } impl FromType for ReflectResource {