@@ -49,6 +49,22 @@ impl AppBuilder {
49
49
}
50
50
}
51
51
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
+ /// ```
52
68
pub fn run ( & mut self ) {
53
69
let app = std:: mem:: take ( & mut self . app ) ;
54
70
app. run ( ) ;
@@ -138,6 +154,32 @@ impl AppBuilder {
138
154
self
139
155
}
140
156
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
+ /// ```
141
183
pub fn add_system ( & mut self , system : impl Into < SystemDescriptor > ) -> & mut Self {
142
184
self . add_system_to_stage ( CoreStage :: Update , system)
143
185
}
@@ -166,6 +208,26 @@ impl AppBuilder {
166
208
self
167
209
}
168
210
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
+ /// ```
169
231
pub fn add_startup_system ( & mut self , system : impl Into < SystemDescriptor > ) -> & mut Self {
170
232
self . add_startup_system_to_stage ( StartupStage :: Startup , system)
171
233
}
@@ -236,8 +298,24 @@ impl AppBuilder {
236
298
. add_system_to_stage ( CoreStage :: First , Events :: < T > :: update_system. system ( ) )
237
299
}
238
300
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
+ /// ```
241
319
pub fn insert_resource < T > ( & mut self , resource : T ) -> & mut Self
242
320
where
243
321
T : Component ,
@@ -246,6 +324,22 @@ impl AppBuilder {
246
324
self
247
325
}
248
326
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
+ /// ```
249
343
pub fn insert_non_send_resource < T > ( & mut self , resource : T ) -> & mut Self
250
344
where
251
345
T : ' static ,
@@ -254,6 +348,30 @@ impl AppBuilder {
254
348
self
255
349
}
256
350
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
+ /// ```
257
375
pub fn init_resource < R > ( & mut self ) -> & mut Self
258
376
where
259
377
R : FromWorld + Send + Sync + ' static ,
@@ -280,11 +398,45 @@ impl AppBuilder {
280
398
self
281
399
}
282
400
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
+ /// ```
283
422
pub fn set_runner ( & mut self , run_fn : impl Fn ( App ) + ' static ) -> & mut Self {
284
423
self . app . runner = Box :: new ( run_fn) ;
285
424
self
286
425
}
287
426
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
+ /// ```
288
440
pub fn add_plugin < T > ( & mut self , plugin : T ) -> & mut Self
289
441
where
290
442
T : Plugin ,
@@ -294,13 +446,62 @@ impl AppBuilder {
294
446
self
295
447
}
296
448
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
+ /// ```
297
469
pub fn add_plugins < T : PluginGroup > ( & mut self , mut group : T ) -> & mut Self {
298
470
let mut plugin_group_builder = PluginGroupBuilder :: default ( ) ;
299
471
group. build ( & mut plugin_group_builder) ;
300
472
plugin_group_builder. finish ( self ) ;
301
473
self
302
474
}
303
475
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
+ /// ```
304
505
pub fn add_plugins_with < T , F > ( & mut self , mut group : T , func : F ) -> & mut Self
305
506
where
306
507
T : PluginGroup ,
0 commit comments