From 1d8ab55d06deff9433cd0ba9370ad040ffa88081 Mon Sep 17 00:00:00 2001 From: Nicola Papale Date: Sat, 18 Jun 2022 13:25:34 +0200 Subject: [PATCH] Skip iterating over scene entities on default hook --- crates/bevy_scene/src/hook.rs | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/crates/bevy_scene/src/hook.rs b/crates/bevy_scene/src/hook.rs index 3047ab6704548..7e1c73be3d88b 100644 --- a/crates/bevy_scene/src/hook.rs +++ b/crates/bevy_scene/src/hook.rs @@ -13,7 +13,7 @@ use bevy_ecs::{ use crate::{SceneInstance, SceneSpawner}; /// Marker Component for scenes that were hooked. -#[derive(Component)] +#[derive(Component, Debug)] #[non_exhaustive] pub struct SceneHooked; @@ -57,14 +57,9 @@ pub struct SceneHooked; /// }); /// } /// ``` -#[derive(Component)] +#[derive(Component, Default)] pub struct SceneHook { - hook: Box, -} -impl Default for SceneHook { - fn default() -> Self { - Self { hook: Box::new(()) } - } + hook: Option>, } impl From for SceneHook { fn from(hook: T) -> Self { @@ -79,8 +74,9 @@ impl SceneHook { /// that strictly speaking, you might as well pass a closure. Please check /// the [`Hook`] trait documentation for details. pub fn new(hook: T) -> Self { - let hook = Box::new(hook); - Self { hook } + Self { + hook: Some(Box::new(hook)), + } } /// Same as [`Self::new`] but with type bounds to make it easier to @@ -155,9 +151,11 @@ pub fn run_hooks( ) { for (entity, instance, hooked) in unloaded_instances.iter() { if let Some(entities) = scene_manager.iter_instance_entities(**instance) { - for entity_ref in entities.filter_map(|e| world.get_entity(e)) { - let mut cmd = cmds.entity(entity_ref.id()); - hooked.hook.hook_entity(&entity_ref, &mut cmd); + if let Some(hook) = hooked.hook.as_deref() { + for entity_ref in entities.filter_map(|e| world.get_entity(e)) { + let mut cmd = cmds.entity(entity_ref.id()); + hook.hook_entity(&entity_ref, &mut cmd); + } } cmds.entity(entity).insert(SceneHooked); } @@ -168,7 +166,3 @@ impl Hook for F (self)(entity_ref, commands); } } -// This is useful for the `Default` implementation of `SceneBundle` -impl Hook for () { - fn hook_entity(&self, _: &EntityRef, _: &mut EntityCommands) {} -}