@@ -2,12 +2,32 @@ use std::marker::PhantomData;
22
33use bevy_app:: { App , CoreStage , Plugin } ;
44use bevy_core:: Name ;
5- use bevy_ecs:: prelude:: * ;
5+ use bevy_ecs:: { prelude:: * , schedule :: ShouldRun } ;
66use bevy_log:: warn;
77use bevy_utils:: { get_short_name, HashSet } ;
88
99use crate :: Parent ;
1010
11+ /// When enabled, runs [`check_hierarchy_component_has_valid_parent<T>`].
12+ ///
13+ /// This resource is added by [`ValidParentCheckPlugin<T>`].
14+ /// It is enabled on debug builds and disabled in release builds by default,
15+ /// you can update this resource at runtime to change the default behavior.
16+ #[ derive( Resource ) ]
17+ pub struct ReportHierarchyIssue < T > {
18+ /// Whether to run [`check_hierarchy_component_has_valid_parent<T>`].
19+ pub enabled : bool ,
20+ _comp : PhantomData < fn ( T ) > ,
21+ }
22+ impl < T > Default for ReportHierarchyIssue < T > {
23+ fn default ( ) -> Self {
24+ Self {
25+ enabled : cfg ! ( debug_assertions) ,
26+ _comp : PhantomData ,
27+ }
28+ }
29+ }
30+
1131/// System to print a warning for each `Entity` with a `T` component
1232/// which parent hasn't a `T` component.
1333///
@@ -38,6 +58,14 @@ pub fn check_hierarchy_component_has_valid_parent<T: Component>(
3858 }
3959}
4060
61+ /// Run criteria that only allows running when [`ReportHierarchyIssue<T>`] is enabled.
62+ pub fn on_hierarchy_reports_enabled < T > ( report : Res < ReportHierarchyIssue < T > > ) -> ShouldRun
63+ where
64+ T : Component ,
65+ {
66+ report. enabled . into ( )
67+ }
68+
4169/// Print a warning for each `Entity` with a `T` component
4270/// which parent hasn't a `T` component.
4371///
@@ -51,9 +79,11 @@ impl<T: Component> Default for ValidParentCheckPlugin<T> {
5179
5280impl < T : Component > Plugin for ValidParentCheckPlugin < T > {
5381 fn build ( & self , app : & mut App ) {
54- app. add_system_to_stage (
55- CoreStage :: Last ,
56- check_hierarchy_component_has_valid_parent :: < T > ,
57- ) ;
82+ app. init_resource :: < ReportHierarchyIssue < T > > ( )
83+ . add_system_to_stage (
84+ CoreStage :: Last ,
85+ check_hierarchy_component_has_valid_parent :: < T >
86+ . with_run_criteria ( on_hierarchy_reports_enabled :: < T > ) ,
87+ ) ;
5888 }
5989}
0 commit comments