Skip to content

Commit 73ae6af

Browse files
blaindcart
andcommitted
Add inline documentation to bevy code (#1404)
For review, first iteration of bevy code documentation. I can continue submitting docs every now and then for relevant parts. Some challenges I found: * plugins example had to be commented out, as adding bevy_internal (where plugins reside) would pull in too many dependencies Co-authored-by: Mika <[email protected]> Co-authored-by: Carter Anderson <[email protected]>
1 parent 4e52484 commit 73ae6af

File tree

2 files changed

+207
-2
lines changed

2 files changed

+207
-2
lines changed

crates/bevy_app/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ ron = { version = "0.6.2", optional = true }
3232
[target.'cfg(target_arch = "wasm32")'.dependencies]
3333
wasm-bindgen = { version = "0.2" }
3434
web-sys = { version = "0.3", features = [ "Window" ] }
35+
36+
[dev-dependencies]
37+
# bevy
38+
bevy_log = { path = "../bevy_log", version = "0.5.0" }

crates/bevy_app/src/app_builder.rs

+203-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ impl AppBuilder {
4949
}
5050
}
5151

52+
/// Start the application (through main runner)
53+
///
54+
/// Runs the application main loop.
55+
///
56+
/// Usually the main loop is handled by Bevy integrated plugins (`winit`), but
57+
/// but one can also set the runner function through [`AppBuilder::set_runner`].
58+
///
59+
/// ## Example
60+
/// ```
61+
/// # use bevy_app::prelude::*;
62+
/// #
63+
/// App::build()
64+
/// // all required plugin insertions, systems, etc inserted here
65+
/// // finally, call:
66+
/// .run();
67+
/// ```
5268
pub fn run(&mut self) {
5369
let app = std::mem::take(&mut self.app);
5470
app.run();
@@ -138,6 +154,32 @@ impl AppBuilder {
138154
self
139155
}
140156

157+
/// Adds a system that runs every time `app.update()` is called by the runner
158+
///
159+
/// Systems are the main building block in the Bevy ECS app model. You can define
160+
/// normal rust functions, and call `.system()` to make them be Bevy systems.
161+
///
162+
/// System functions can have parameters, through which one can query and
163+
/// mutate Bevy ECS states.
164+
/// See [The Bevy Book](https://bevyengine.org/learn/book/introduction/) for more information.
165+
///
166+
/// Systems are run in parallel, and the execution order is not deterministic.
167+
/// If you want more fine-grained control for order, see [`AppBuilder::add_system_to_stage`].
168+
///
169+
/// For adding a system that runs only at app startup, see [`AppBuilder::add_startup_system`].
170+
///
171+
/// ## Example
172+
/// ```
173+
/// # use bevy_app::prelude::*;
174+
/// # use bevy_ecs::prelude::*;
175+
/// #
176+
/// fn my_system(_commands: Commands) {
177+
/// println!("My system, triggered once per frame");
178+
/// }
179+
///
180+
/// App::build()
181+
/// .add_system(my_system.system());
182+
/// ```
141183
pub fn add_system(&mut self, system: impl Into<SystemDescriptor>) -> &mut Self {
142184
self.add_system_to_stage(CoreStage::Update, system)
143185
}
@@ -166,6 +208,26 @@ impl AppBuilder {
166208
self
167209
}
168210

211+
/// Adds a system that is run once at application startup
212+
///
213+
/// Startup systems run exactly once BEFORE all other systems. These are generally used for
214+
/// app initialization code (ex: adding entities and resources).
215+
///
216+
/// * For adding a system that runs for every frame, see [`AppBuilder::add_system`].
217+
/// * For adding a system to specific stage, see [`AppBuilder::add_system_to_stage`].
218+
///
219+
/// ## Example
220+
/// ```
221+
/// # use bevy_app::prelude::*;
222+
/// # use bevy_ecs::prelude::*;
223+
/// #
224+
/// fn my_startup_system(_commands: Commands) {
225+
/// println!("My startup system");
226+
/// }
227+
///
228+
/// App::build()
229+
/// .add_startup_system(my_startup_system.system());
230+
/// ```
169231
pub fn add_startup_system(&mut self, system: impl Into<SystemDescriptor>) -> &mut Self {
170232
self.add_startup_system_to_stage(StartupStage::Startup, system)
171233
}
@@ -236,8 +298,24 @@ impl AppBuilder {
236298
.add_system_to_stage(CoreStage::First, Events::<T>::update_system.system())
237299
}
238300

239-
/// Inserts a resource to the current [App] and overwrites any resource previously added of the
240-
/// same type.
301+
/// Inserts a resource to the current [App] and overwrites any resource previously added of the same type.
302+
///
303+
/// A resource in Bevy represents globally unique data. Resources must be added to Bevy Apps
304+
/// before using them. This happens with [`AppBuilder::insert_resource`].
305+
///
306+
/// See also `init_resource` for resources that implement `Default` or [`FromResources`].
307+
///
308+
/// ## Example
309+
/// ```
310+
/// # use bevy_app::prelude::*;
311+
/// #
312+
/// struct MyCounter {
313+
/// counter: usize,
314+
/// }
315+
///
316+
/// App::build()
317+
/// .insert_resource(MyCounter { counter: 0 });
318+
/// ```
241319
pub fn insert_resource<T>(&mut self, resource: T) -> &mut Self
242320
where
243321
T: Component,
@@ -246,6 +324,22 @@ impl AppBuilder {
246324
self
247325
}
248326

327+
/// Inserts a non-send resource to the app
328+
///
329+
/// You usually want to use `insert_resource`, but there are some special cases when a resource must
330+
/// be non-send.
331+
///
332+
/// ## Example
333+
/// ```
334+
/// # use bevy_app::prelude::*;
335+
/// #
336+
/// struct MyCounter {
337+
/// counter: usize,
338+
/// }
339+
///
340+
/// App::build()
341+
/// .insert_non_send_resource(MyCounter { counter: 0 });
342+
/// ```
249343
pub fn insert_non_send_resource<T>(&mut self, resource: T) -> &mut Self
250344
where
251345
T: 'static,
@@ -254,6 +348,30 @@ impl AppBuilder {
254348
self
255349
}
256350

351+
/// Initialize a resource in the current [App], if it does not exist yet
352+
///
353+
/// Adds a resource that implements `Default` or [`FromResources`] trait.
354+
/// If the resource already exists, `init_resource` does nothing.
355+
///
356+
/// ## Example
357+
/// ```
358+
/// # use bevy_app::prelude::*;
359+
/// #
360+
/// struct MyCounter {
361+
/// counter: usize,
362+
/// }
363+
///
364+
/// impl Default for MyCounter {
365+
/// fn default() -> MyCounter {
366+
/// MyCounter {
367+
/// counter: 100
368+
/// }
369+
/// }
370+
/// }
371+
///
372+
/// App::build()
373+
/// .init_resource::<MyCounter>();
374+
/// ```
257375
pub fn init_resource<R>(&mut self) -> &mut Self
258376
where
259377
R: FromWorld + Send + Sync + 'static,
@@ -280,11 +398,45 @@ impl AppBuilder {
280398
self
281399
}
282400

401+
/// Sets the main runner loop function for this Bevy App
402+
///
403+
/// Usually the main loop is handled by Bevy integrated plugins ([`WinitPlugin`]), but
404+
/// in some cases one might wish to implement their own main loop.
405+
///
406+
/// This method sets the main loop function, overwriting a previous runner if any.
407+
///
408+
/// ## Example
409+
/// ```
410+
/// # use bevy_app::prelude::*;
411+
/// #
412+
/// fn my_runner(mut app: App) {
413+
/// loop {
414+
/// println!("In main loop");
415+
/// app.update();
416+
/// }
417+
/// }
418+
///
419+
/// App::build()
420+
/// .set_runner(my_runner);
421+
/// ```
283422
pub fn set_runner(&mut self, run_fn: impl Fn(App) + 'static) -> &mut Self {
284423
self.app.runner = Box::new(run_fn);
285424
self
286425
}
287426

427+
/// Adds a single plugin
428+
///
429+
/// One of Bevy's core principles is modularity. All Bevy engine features are implemented
430+
/// as plugins. This includes internal features like the renderer.
431+
///
432+
/// Bevy also provides a few sets of default plugins. See [`AppBuilder::add_plugins`].
433+
///
434+
/// ## Example
435+
/// ```
436+
/// # use bevy_app::prelude::*;
437+
/// #
438+
/// App::build().add_plugin(bevy_log::LogPlugin::default());
439+
/// ```
288440
pub fn add_plugin<T>(&mut self, plugin: T) -> &mut Self
289441
where
290442
T: Plugin,
@@ -294,13 +446,62 @@ impl AppBuilder {
294446
self
295447
}
296448

449+
/// Adds a group of plugins
450+
///
451+
/// Bevy plugins can be grouped into a set of plugins. Bevy provides
452+
/// built-in PluginGroups that provide core engine functionality.
453+
///
454+
/// The plugin groups available by default are [`DefaultPlugins`] and [`MinimalPlugins`].
455+
///
456+
/// ## Example
457+
/// ```
458+
/// # use bevy_app::{prelude::*, PluginGroupBuilder};
459+
/// #
460+
/// # // Dummy created to avoid using bevy_internal, which pulls in to many dependencies.
461+
/// # struct MinimalPlugins;
462+
/// # impl PluginGroup for MinimalPlugins {
463+
/// # fn build(&mut self, group: &mut PluginGroupBuilder){;}
464+
/// # }
465+
/// #
466+
/// App::build()
467+
/// .add_plugins(MinimalPlugins);
468+
/// ```
297469
pub fn add_plugins<T: PluginGroup>(&mut self, mut group: T) -> &mut Self {
298470
let mut plugin_group_builder = PluginGroupBuilder::default();
299471
group.build(&mut plugin_group_builder);
300472
plugin_group_builder.finish(self);
301473
self
302474
}
303475

476+
/// Adds a group of plugins with an initializer method
477+
///
478+
/// Can be used to add a group of plugins, where the group is modified
479+
/// before insertion into Bevy application. For example, you can add
480+
/// extra plugins at a specific place in the plugin group, or deactivate
481+
/// specific plugins while keeping the rest.
482+
///
483+
/// ## Example
484+
/// ```
485+
/// # use bevy_app::{prelude::*, PluginGroupBuilder};
486+
/// #
487+
/// # // Dummies created to avoid using bevy_internal which pulls in to many dependencies.
488+
/// # struct DefaultPlugins;
489+
/// # impl PluginGroup for DefaultPlugins {
490+
/// # fn build(&mut self, group: &mut PluginGroupBuilder){
491+
/// # group.add(bevy_log::LogPlugin::default());
492+
/// # }
493+
/// # }
494+
/// #
495+
/// # struct MyOwnPlugin;
496+
/// # impl Plugin for MyOwnPlugin {
497+
/// # fn build(&self, app: &mut AppBuilder){;}
498+
/// # }
499+
/// #
500+
/// App::build()
501+
/// .add_plugins_with(DefaultPlugins, |group| {
502+
/// group.add_before::<bevy_log::LogPlugin, _>(MyOwnPlugin)
503+
/// });
504+
/// ```
304505
pub fn add_plugins_with<T, F>(&mut self, mut group: T, func: F) -> &mut Self
305506
where
306507
T: PluginGroup,

0 commit comments

Comments
 (0)