Skip to content

Commit 3ab5ce0

Browse files
committed
update bevy_app, bevy_core and a whole lotta docs
1 parent e685d45 commit 3ab5ce0

File tree

18 files changed

+924
-1283
lines changed

18 files changed

+924
-1283
lines changed

crates/bevy_app/src/app.rs

+193-599
Large diffs are not rendered by default.

crates/bevy_app/src/lib.rs

+3-40
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ mod ci_testing;
1212

1313
pub use app::*;
1414
pub use bevy_derive::DynamicPlugin;
15+
pub use bevy_ecs::event::*;
1516
pub use plugin::*;
1617
pub use plugin_group::*;
1718
pub use schedule_runner::*;
@@ -20,45 +21,7 @@ pub use schedule_runner::*;
2021
pub mod prelude {
2122
#[doc(hidden)]
2223
pub use crate::{
23-
app::App, CoreStage, DynamicPlugin, Plugin, PluginGroup, StartupSchedule, StartupStage,
24+
app::{App, AppSet},
25+
DynamicPlugin, Plugin, PluginGroup,
2426
};
2527
}
26-
27-
use bevy_ecs::schedule::StageLabel;
28-
29-
/// The names of the default App stages
30-
///
31-
/// The relative stages are added by [`App::add_default_stages`].
32-
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
33-
pub enum CoreStage {
34-
/// Name of app stage that runs before all other app stages
35-
First,
36-
/// Name of app stage responsible for performing setup before an update. Runs before UPDATE.
37-
PreUpdate,
38-
/// Name of app stage responsible for doing most app logic. Systems should be registered here
39-
/// by default.
40-
Update,
41-
/// Name of app stage responsible for processing the results of UPDATE. Runs after UPDATE.
42-
PostUpdate,
43-
/// Name of app stage that runs after all other app stages
44-
Last,
45-
}
46-
47-
/// The label for the Startup [`Schedule`](bevy_ecs::schedule::Schedule),
48-
/// which runs once at the beginning of the app.
49-
///
50-
/// When targeting a [`Stage`](bevy_ecs::schedule::Stage) inside this Schedule,
51-
/// you need to use [`StartupStage`] instead.
52-
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
53-
pub struct StartupSchedule;
54-
55-
/// The names of the default App startup stages
56-
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
57-
pub enum StartupStage {
58-
/// Name of app stage that runs once before the startup stage
59-
PreStartup,
60-
/// Name of app stage that runs once when an app starts up
61-
Startup,
62-
/// Name of app stage that runs once after the startup stage
63-
PostStartup,
64-
}

crates/bevy_app/src/plugin.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
use crate::App;
22
use std::any::Any;
33

4-
/// A collection of Bevy App logic and configuration
4+
/// Portable [`App`] configuration.
55
///
6-
/// Plugins configure an [`App`](crate::App). When an [`App`](crate::App) registers
7-
/// a plugin, the plugin's [`Plugin::build`] function is run.
6+
/// Plugins make it easy to export (and re-import) systems, systems sets, and resources.
7+
///
8+
/// After you import a plugin to an [`App`](crate::App) using [`add_plugin`](App::add_plugin), the app will run its [`build`](Plugin::build) function.
89
pub trait Plugin: Any + Send + Sync {
9-
/// Configures the [`App`] to which this plugin is added.
10+
/// Applies the stored configuration to the given [`App`].
1011
fn build(&self, app: &mut App);
11-
/// Configures a name for the [`Plugin`]. Primarily for debugging.
12+
/// Returns the name of this plugin.
1213
fn name(&self) -> &str {
1314
std::any::type_name::<Self>()
1415
}
1516
}
1617

17-
/// Type representing an unsafe function that returns a mutable pointer to a [`Plugin`].
18-
/// Used for dynamically loading plugins. See
19-
/// `bevy_dynamic_plugin/src/loader.rs#dynamically_load_plugin`
18+
/// An alias for an unsafe function that returns a pointer to a [`Plugin`].
19+
/// Used for dynamically loading plugins,
20+
/// as shown in [this example][`bevy_dynamic_plugin/src/loader.rs#dynamically_load_plugin`].
2021
pub type CreatePlugin = unsafe fn() -> *mut dyn Plugin;

crates/bevy_app/src/plugin_group.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@ use crate::{App, Plugin};
22
use bevy_utils::{tracing::debug, HashMap};
33
use std::any::TypeId;
44

5-
/// Combines multiple [`Plugin`]s into a single unit.
5+
/// Combines multiple [`Plugin`] instances into one.
66
pub trait PluginGroup {
7-
/// Configures the [`Plugin`]s that are to be added.
7+
/// Builds all the plugins in the group.
88
fn build(&mut self, group: &mut PluginGroupBuilder);
99
}
1010

11+
/// A [`Plugin`] that is part of a [`PluginGroup`].
1112
struct PluginEntry {
1213
plugin: Box<dyn Plugin>,
1314
enabled: bool,
1415
}
1516

1617
/// Facilitates the creation and configuration of a [`PluginGroup`].
17-
/// Provides a build ordering to ensure that [`Plugin`]s which produce/require a resource
18-
/// are built before/after dependent/depending [`Plugin`]s.
18+
/// Defines a [`Plugin`] build order to enable plugins being dependent on other plugins.
1919
#[derive(Default)]
2020
pub struct PluginGroupBuilder {
2121
plugins: HashMap<TypeId, PluginEntry>,
2222
order: Vec<TypeId>,
2323
}
2424

2525
impl PluginGroupBuilder {
26-
/// Appends a [`Plugin`] to the [`PluginGroupBuilder`].
26+
/// Appends the [`Plugin`] to the end of the current set.
2727
pub fn add<T: Plugin>(&mut self, plugin: T) -> &mut Self {
2828
self.order.push(TypeId::of::<T>());
2929
self.plugins.insert(
@@ -36,7 +36,7 @@ impl PluginGroupBuilder {
3636
self
3737
}
3838

39-
/// Configures a [`Plugin`] to be built before another plugin.
39+
/// Configures the [`Plugin`] to be built before the `Target` plugin.
4040
pub fn add_before<Target: Plugin, T: Plugin>(&mut self, plugin: T) -> &mut Self {
4141
let target_index = self
4242
.order
@@ -61,7 +61,7 @@ impl PluginGroupBuilder {
6161
self
6262
}
6363

64-
/// Configures a [`Plugin`] to be built after another plugin.
64+
/// Configures the [`Plugin`] to be built after the `Target` plugin.
6565
pub fn add_after<Target: Plugin, T: Plugin>(&mut self, plugin: T) -> &mut Self {
6666
let target_index = self
6767
.order
@@ -86,10 +86,10 @@ impl PluginGroupBuilder {
8686
self
8787
}
8888

89-
/// Enables a [`Plugin`]
89+
/// Enables the [`Plugin`].
9090
///
91-
/// [`Plugin`]s within a [`PluginGroup`] are enabled by default. This function is used to
92-
/// opt back in to a [`Plugin`] after [disabling](Self::disable) it.
91+
/// Plugins within a [`PluginGroup`] are enabled by default. This function will
92+
/// re-activate a plugin if [`disable`](Self::disable) was called earlier.
9393
pub fn enable<T: Plugin>(&mut self) -> &mut Self {
9494
let mut plugin_entry = self
9595
.plugins
@@ -99,7 +99,7 @@ impl PluginGroupBuilder {
9999
self
100100
}
101101

102-
/// Disables a [`Plugin`], preventing it from being added to the `App` with the rest of the [`PluginGroup`].
102+
/// Disables the [`Plugin`], stopping it from being built by an [`App`] with the rest of the [`PluginGroup`].
103103
pub fn disable<T: Plugin>(&mut self) -> &mut Self {
104104
let mut plugin_entry = self
105105
.plugins
@@ -109,7 +109,8 @@ impl PluginGroupBuilder {
109109
self
110110
}
111111

112-
/// Consumes the [`PluginGroupBuilder`] and [builds](Plugin::build) the contained [`Plugin`]s.
112+
/// Consumes the [`PluginGroupBuilder`] and runs the [`build`](Plugin::build) function of each
113+
/// contained [`Plugin`] in the specified order.
113114
pub fn finish(self, app: &mut App) {
114115
for ty in self.order.iter() {
115116
if let Some(entry) = self.plugins.get(ty) {

crates/bevy_app/src/schedule_runner.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ use std::{cell::RefCell, rc::Rc};
1010
#[cfg(target_arch = "wasm32")]
1111
use wasm_bindgen::{prelude::*, JsCast};
1212

13-
/// Determines the method used to run an [App]'s [`Schedule`](bevy_ecs::schedule::Schedule).
13+
/// Determines the method used to run an [`App`] schedule.
1414
#[derive(Copy, Clone, Debug)]
1515
pub enum RunMode {
16-
/// Indicates that the [`App`]'s schedule should run repeatedly.
16+
/// The schedule should be run repeatedly.
1717
Loop {
18-
/// Minimum duration to wait after a schedule has completed before repeating.
18+
/// Minimum duration to wait after an update has completed before running another.
1919
/// A value of [`None`] will not wait.
2020
wait: Option<Duration>,
2121
},
22-
/// Indicates that the [`App`]'s schedule should run only once.
22+
/// The schedule should be run a single time.
2323
Once,
2424
}
2525

@@ -54,8 +54,7 @@ impl ScheduleRunnerSettings {
5454
}
5555
}
5656

57-
/// Configures an `App` to run its [`Schedule`](bevy_ecs::schedule::Schedule) according to a given
58-
/// [`RunMode`]
57+
/// Configures an [`App`] to run its schedule according to a given [`RunMode`].
5958
#[derive(Default)]
6059
pub struct ScheduleRunnerPlugin;
6160

crates/bevy_core/src/lib.rs

+78-12
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,78 @@ pub mod prelude {
1616
//! The Bevy Core Prelude.
1717
#[doc(hidden)]
1818
pub use crate::{
19-
DefaultTaskPoolOptions, FixedTime, FixedTimestep, FixedTimestepState, Name, Time, Timer,
19+
CoreSet, DefaultTaskPoolOptions, FixedTime, FixedTimestepState, Name, Time, Timer,
2020
};
2121
}
2222

23-
use bevy_app::prelude::*;
23+
use bevy_app::{prelude::*, AppSystem};
2424
use bevy_ecs::{
2525
entity::Entity,
26-
schedule::{IntoSystemDescriptor, SystemLabel},
26+
schedule::{apply_buffers, chain, IntoScheduledSet, IntoScheduledSystem, SystemLabel},
2727
};
2828
use bevy_utils::HashSet;
29+
2930
use std::ops::Range;
3031

31-
/// Adds core functionality to Apps.
32+
/// [`Plugin`] that adds a standard system execution sequence.
3233
#[derive(Default)]
3334
pub struct CorePlugin;
3435

35-
/// A `SystemLabel` enum for ordering systems relative to core Bevy systems.
36+
/// Systems sets comprising the standard execution sequence.
37+
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]
38+
pub enum CoreSet {
39+
/// Systems that run before the systems in the other core sets (but after [`Time`] is updated).
40+
First,
41+
/// Systems that run with the fixed timestep. Intended for most gameplay systems (e.g. physics).
42+
///
43+
/// **Note:** Fixed timestep does not mean fixed *interval*. This set can repeat several times in a single frame. Use [`FixedTime`] instead of [`Time`] to ensure correct behavior.
44+
FixedUpdate,
45+
/// Systems that run before systems in [`Update`](CoreSet::Update).
46+
/// Intended for systems that need to perform setup for systems in [`Update`](CoreSet::Update).
47+
PreUpdate,
48+
/// Systems that run each frame.
49+
///
50+
/// **Note:** By default, systems and sets with no parent set specified are added here.
51+
Update,
52+
/// Systems that run after systems in [`Update`](CoreSet::Update).
53+
/// Intended for systems that need to process the results of systems in [`Update`](CoreSet::Update).
54+
PostUpdate,
55+
/// Systems that run after the systems in the other core sets.
56+
Last,
57+
}
58+
59+
/// Systems comprising the standard execution sequence.
60+
#[doc(hidden)]
3661
#[derive(Debug, PartialEq, Eq, Clone, Hash, SystemLabel)]
3762
pub enum CoreSystem {
38-
/// Advances time. Any system that interacts with the [`Time`] resource should run after this.
63+
/// Advances [`Time`]. First thing that runs in a frame.
3964
Time,
65+
/// Advances [`FixedTime`] and runs the systems under [`FixedUpdate`](CoreSet::FixedUpdate).
66+
FixedUpdate,
67+
/// Calls [`apply_buffers`] after the systems under [`First`](CoreSet::First).
68+
ApplyFirst,
69+
/// Calls [`apply_buffers`] after the systems under [`FixedUpdate`](CoreSet::FixedUpdate).
70+
ApplyFixedUpdate,
71+
/// Calls [`apply_buffers`] after the systems under [`PreUpdate`](CoreSet::PreUpdate).
72+
ApplyPreUpdate,
73+
/// Calls [`apply_buffers`] after the systems under [`Update`](CoreSet::Update).
74+
ApplyUpdate,
75+
/// Calls [`apply_buffers`] after the systems under [`PostUpdate`](CoreSet::PostUpdate).
76+
ApplyPostUpdate,
77+
/// Calls [`apply_buffers`] after the systems under [`Last`](CoreSet::Last).
78+
ApplyLast,
79+
}
80+
81+
/// Internal system sets needed to bypass limitations with [`apply_buffers`].
82+
#[doc(hidden)]
83+
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]
84+
pub(crate) enum CoreInternalSet {
85+
/// Encompasses [`CoreSet::FixedUpdate`] and [`CoreSystem::ApplyFixedUpdate`].
86+
FixedUpdate,
4087
}
4188

4289
impl Plugin for CorePlugin {
4390
fn build(&self, app: &mut App) {
44-
// Setup the default bevy task pools
4591
app.world
4692
.get_resource::<DefaultTaskPoolOptions>()
4793
.cloned()
@@ -57,11 +103,31 @@ impl Plugin for CorePlugin {
57103
.register_type::<Name>()
58104
.register_type::<Range<f32>>()
59105
.register_type::<Timer>()
60-
// time system is added to the "AtStart" set to ensure it runs before other systems
61-
// in CoreStage::First
62-
.add_system_to_stage(
63-
CoreStage::First,
64-
time_system.at_start().label(CoreSystem::Time),
106+
.add_many(
107+
chain![
108+
update_time.named(CoreSystem::Time),
109+
CoreSet::First,
110+
apply_buffers.named(CoreSystem::ApplyFirst),
111+
fixed_update.named(CoreSystem::FixedUpdate),
112+
CoreSet::PreUpdate,
113+
apply_buffers.named(CoreSystem::ApplyPreUpdate),
114+
CoreSet::Update,
115+
apply_buffers.named(CoreSystem::ApplyUpdate),
116+
CoreSet::PostUpdate,
117+
apply_buffers.named(CoreSystem::ApplyPostUpdate),
118+
CoreSet::Last,
119+
apply_buffers.named(CoreSystem::ApplyLast),
120+
]
121+
.to(AppSet::Update)
122+
.after(AppSet::UpdateEvents)
123+
.before(AppSystem::ClearTrackers),
124+
)
125+
.add_many(
126+
chain![
127+
CoreSet::FixedUpdate,
128+
apply_buffers.named(CoreSystem::ApplyFixedUpdate),
129+
]
130+
.to(CoreInternalSet::FixedUpdate),
65131
);
66132

67133
register_rust_types(app);

0 commit comments

Comments
 (0)