Skip to content

Commit 6e46fa9

Browse files
committed
Make validity check enabled through a resource
1 parent 26b8f08 commit 6e46fa9

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

crates/bevy_hierarchy/src/valid_parent_check_plugin.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,32 @@ use std::marker::PhantomData;
22

33
use bevy_app::{App, CoreStage, Plugin};
44
use bevy_core::Name;
5-
use bevy_ecs::prelude::*;
5+
use bevy_ecs::{prelude::*, schedule::ShouldRun};
66
use bevy_log::warn;
77
use bevy_utils::{get_short_name, HashSet};
88

99
use 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

5280
impl<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
}

crates/bevy_internal/src/default_plugins.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ impl PluginGroup for DefaultPlugins {
3535
group.add(bevy_input::InputPlugin::default());
3636
group.add(bevy_window::WindowPlugin::default());
3737

38-
#[cfg(debug_assertions)]
39-
group.add(bevy_hierarchy::ValidParentCheckPlugin::<
40-
crate::prelude::GlobalTransform,
41-
>::default());
42-
4338
#[cfg(feature = "bevy_asset")]
4439
group.add(bevy_asset::AssetPlugin::default());
4540

@@ -55,11 +50,6 @@ impl PluginGroup for DefaultPlugins {
5550
#[cfg(feature = "bevy_render")]
5651
group.add(bevy_render::RenderPlugin::default());
5752

58-
#[cfg(all(feature = "bevy_render", debug_assertions))]
59-
group.add(bevy_hierarchy::ValidParentCheckPlugin::<
60-
crate::prelude::ComputedVisibility,
61-
>::default());
62-
6353
#[cfg(feature = "bevy_core_pipeline")]
6454
group.add(bevy_core_pipeline::CorePipelinePlugin::default());
6555

crates/bevy_render/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod spatial_bundle;
1818
pub mod texture;
1919
pub mod view;
2020

21+
use bevy_hierarchy::ValidParentCheckPlugin;
2122
pub use extract_param::Extract;
2223

2324
pub mod prelude {
@@ -34,6 +35,7 @@ pub mod prelude {
3435
}
3536

3637
pub use once_cell;
38+
use prelude::ComputedVisibility;
3739

3840
use crate::{
3941
camera::CameraPlugin,
@@ -315,7 +317,8 @@ impl Plugin for RenderPlugin {
315317
});
316318
}
317319

318-
app.add_plugin(WindowRenderPlugin)
320+
app.add_plugin(ValidParentCheckPlugin::<ComputedVisibility>::default())
321+
.add_plugin(WindowRenderPlugin)
319322
.add_plugin(CameraPlugin)
320323
.add_plugin(ViewPlugin)
321324
.add_plugin(MeshPlugin)

crates/bevy_transform/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub mod prelude {
1414

1515
use bevy_app::prelude::*;
1616
use bevy_ecs::prelude::*;
17+
use bevy_hierarchy::ValidParentCheckPlugin;
1718
use prelude::{GlobalTransform, Transform};
1819

1920
/// A [`Bundle`] of the [`Transform`] and [`GlobalTransform`]
@@ -91,6 +92,7 @@ impl Plugin for TransformPlugin {
9192
fn build(&self, app: &mut App) {
9293
app.register_type::<Transform>()
9394
.register_type::<GlobalTransform>()
95+
.add_plugin(ValidParentCheckPlugin::<GlobalTransform>::default())
9496
// add transform systems to startup so the first update is "correct"
9597
.add_startup_system_to_stage(
9698
StartupStage::PostStartup,

0 commit comments

Comments
 (0)