From 42e990861cc28f921bf4253b277c87bed83a163a Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Mon, 8 Jan 2024 22:34:32 +0000 Subject: [PATCH] Remove apply_deferred example (#11142) # Objective Re this comment: https://github.com/bevyengine/bevy/pull/11141#issuecomment-1872455313 Since https://github.com/bevyengine/bevy/pull/9822, Bevy automatically inserts `apply_deferred` between systems with dependencies where needed, so manually inserted `apply_deferred` doesn't to anything useful, and in current state this example does more harm than good. ## Solution The example can be modified with removal of automatic `apply_deferred` insertion, but that would immediately upgrade this example from beginner level, to upper intermediate. Most users don't need to disable automatic sync point insertion, and remaining few who do probably already know how it works. CC @hymm --- Cargo.toml | 5 - examples/README.md | 1 - examples/ecs/apply_deferred.rs | 181 --------------------------------- 3 files changed, 187 deletions(-) delete mode 100644 examples/ecs/apply_deferred.rs diff --git a/Cargo.toml b/Cargo.toml index b7c54b5a7e..70677e717e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1283,11 +1283,6 @@ description = "Full guide to Bevy's ECS" category = "ECS (Entity Component System)" wasm = false -[[example]] -name = "apply_deferred" -path = "examples/ecs/apply_deferred.rs" -doc-scrape-examples = true - [package.metadata.example.apply_deferred] name = "Apply System Buffers" description = "Show how to use `apply_deferred` system" diff --git a/examples/README.md b/examples/README.md index d9e4db4c7b..ca47cf4647 100644 --- a/examples/README.md +++ b/examples/README.md @@ -219,7 +219,6 @@ Example | Description Example | Description --- | --- -[Apply System Buffers](../examples/ecs/apply_deferred.rs) | Show how to use `apply_deferred` system [Component Change Detection](../examples/ecs/component_change_detection.rs) | Change detection on components [Custom Query Parameters](../examples/ecs/custom_query_param.rs) | Groups commonly used compound queries and query filters into a single type [ECS Guide](../examples/ecs/ecs_guide.rs) | Full guide to Bevy's ECS diff --git a/examples/ecs/apply_deferred.rs b/examples/ecs/apply_deferred.rs deleted file mode 100644 index 3e8b45ff45..0000000000 --- a/examples/ecs/apply_deferred.rs +++ /dev/null @@ -1,181 +0,0 @@ -//! This example illustrates how to use the `apply_deferred` system -//! to flush commands added by systems that have already run, -//! but have not had their buffers applied yet. -//! -//! This is useful when you don't want to wait until the next time Bevy -//! automatically flushes commands (by default, after all systems have run in any -//! particular schedule) but want to flush commands immediately. -//! -//! It is important that systems are ordered correctly with respect to -//! `apply_deferred`, to avoid surprising non-deterministic system execution order. - -use bevy::prelude::*; - -fn main() { - App::new() - .add_plugins(DefaultPlugins) - .init_resource::() - .add_systems(Startup, setup) - .add_systems( - Update, - ( - ( - despawn_old_and_spawn_new_fruits, - // We encourage adding apply_deferred to a custom set - // to improve diagnostics. This is optional, but useful when debugging! - apply_deferred.in_set(CustomFlush), - count_apple, - ) - .chain(), - count_orange, - bevy::window::close_on_esc, - ), - ) - .run(); -} - -#[derive(Resource)] -struct Timers { - repeating: Timer, -} - -impl Default for Timers { - fn default() -> Self { - Self { - repeating: Timer::from_seconds(0.5, TimerMode::Repeating), - } - } -} - -#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)] -struct CustomFlush; - -#[derive(Component)] -struct Apple; - -#[derive(Component)] -struct Orange; - -#[derive(Component)] -struct AppleCount; - -#[derive(Component)] -struct OrangeCount; - -// Setup the counters in the UI. -fn setup(mut commands: Commands) { - commands.spawn(Camera2dBundle::default()); - - commands - .spawn(NodeBundle { - style: Style { - width: Val::Percent(100.0), - height: Val::Percent(100.0), - align_items: AlignItems::Center, - justify_content: JustifyContent::Center, - flex_direction: FlexDirection::Column, - ..default() - }, - ..default() - }) - .with_children(|parent| { - parent.spawn(( - TextBundle::from_section( - "Apple: nothing counted yet".to_string(), - TextStyle { - font_size: 80.0, - color: Color::ORANGE, - ..default() - }, - ), - AppleCount, - )); - parent.spawn(( - TextBundle::from_section( - "Orange: nothing counted yet".to_string(), - TextStyle { - font_size: 80.0, - color: Color::ORANGE, - ..default() - }, - ), - OrangeCount, - )); - }); -} - -// Every tick, before the CustomFlush we added, we despawn any Apple and Orange -// we have previously spawned, if any. Then we tick the timer, and if the timer -// has finished during this tick, we spawn a new Apple and a new Orange. -// -// The commands that we have added here will normally be flushed by Bevy -// after all systems in the schedule have run, but because we have ordered -// this system to run before `apply_deferred.in_set(CustomFlush)`, -// these commands added here will be flushed during our custom flush. -fn despawn_old_and_spawn_new_fruits( - mut commands: Commands, - time: Res