pub struct App {
pub world: World,
pub runner: Box<dyn FnOnce(App) + Send, Global>,
pub main_schedule_label: Box<dyn ScheduleLabel, Global>,
/* private fields */
}
Expand description
A container of app logic and data.
Bundles together the necessary elements like World
and Schedule
to create
an ECS-based application. It also stores a pointer to a runner function.
The runner is responsible for managing the application’s event loop and applying the
Schedule
to the World
to drive application logic.
Examples
Here is a simple “Hello World” Bevy app:
fn main() {
App::new()
.add_systems(Update, hello_world_system)
.run();
}
fn hello_world_system() {
println!("hello world");
}
Fields§
§world: World
The main ECS World
of the App
.
This stores and provides access to all the main data of the application.
The systems of the App
will run using this World
.
If additional separate World
-Schedule
pairs are needed, you can use sub_app
s.
runner: Box<dyn FnOnce(App) + Send, Global>
The runner function is primarily responsible for managing
the application’s event loop and advancing the Schedule
.
Typically, it is not configured manually, but set by one of Bevy’s built-in plugins.
See bevy::winit::WinitPlugin
and ScheduleRunnerPlugin
.
main_schedule_label: Box<dyn ScheduleLabel, Global>
The schedule that systems are added to by default.
The schedule that runs the main loop of schedule execution.
This is initially set to Main
.
Implementations§
§impl App
impl App
pub fn new() -> App
pub fn new() -> App
Creates a new App
with some default structure to enable core engine features.
This is the preferred constructor for most use cases.
pub fn empty() -> App
pub fn empty() -> App
Creates a new empty App
with minimal default configuration.
This constructor should be used if you wish to provide custom scheduling, exit handling, cleanup, etc.
pub fn update(&mut self)
pub fn update(&mut self)
Advances the execution of the Schedule
by one cycle.
This method also updates sub apps.
See insert_sub_app
for more details.
The schedule run by this method is determined by the main_schedule_label
field.
By default this is Main
.
Panics
The active schedule of the app must be set before this method is called.
pub fn run(&mut self)
pub fn run(&mut self)
Starts the application by calling the app’s runner function.
Finalizes the App
configuration. For general usage, see the example on the item
level documentation.
run()
might not return
Calls to App::run()
might never return.
In simple and headless applications, one can expect that execution will
proceed, normally, after calling run()
but this is not the case for
windowed applications.
Windowed apps are typically driven by an event loop or message loop and some window-manager APIs expect programs to terminate when their primary window is closed and that event loop terminates – behavior of processes that do not is often platform dependent or undocumented.
By default, Bevy uses the winit
crate for window creation. See
WinitSettings::return_from_run
for further discussion of this topic and for a mechanism to require that App::run()
does return – albeit one that carries its own caveats and disclaimers.
Panics
Panics if called from Plugin::build()
, because it would prevent other plugins to properly build.
pub fn ready(&self) -> bool
pub fn ready(&self) -> bool
Check that Plugin::ready
of all plugins returns true. This is usually called by the
event loop, but can be useful for situations where you want to use App::update
pub fn finish(&mut self)
pub fn finish(&mut self)
Run Plugin::finish
for each plugin. This is usually called by the event loop once all
plugins are App::ready
, but can be useful for situations where you want to use
App::update
.
pub fn cleanup(&mut self)
pub fn cleanup(&mut self)
Run Plugin::cleanup
for each plugin. This is usually called by the event loop after
App::finish
, but can be useful for situations where you want to use App::update
.
pub fn add_state<S>(&mut self) -> &mut Appwhere
S: States,
pub fn add_state<S>(&mut self) -> &mut Appwhere S: States,
Adds State<S>
and NextState<S>
resources, OnEnter
and OnExit
schedules
for each state variant (if they don’t already exist), an instance of apply_state_transition::<S>
in
StateTransition
so that transitions happen before Update
and
a instance of run_enter_schedule::<S>
in StateTransition
with a
run_once
condition to run the on enter schedule of the
initial state.
If you would like to control how other systems run based on the current state,
you can emulate this behavior using the in_state
Condition
.
Note that you can also apply state transitions at other points in the schedule
by adding the apply_state_transition
system manually.
pub fn add_system<M>(&mut self, system: impl IntoSystemConfigs<M>) -> &mut App
👎Deprecated since 0.11.0: Please use add_systems
instead. If you didn’t change the default base set, you should use add_systems(Update, your_system).
pub fn add_system<M>(&mut self, system: impl IntoSystemConfigs<M>) -> &mut App
add_systems
instead. If you didn’t change the default base set, you should use add_systems(Update, your_system).
Adds a system to the default system set and schedule of the app’s Schedules
.
Refer to the system module documentation to see how a system can be defined.
Examples
app.add_system(my_system);
pub fn add_systems<M>(
&mut self,
schedule: impl ScheduleLabel,
systems: impl IntoSystemConfigs<M>
) -> &mut App
pub fn add_systems<M>( &mut self, schedule: impl ScheduleLabel, systems: impl IntoSystemConfigs<M> ) -> &mut App
pub fn add_startup_system<M>(
&mut self,
system: impl IntoSystemConfigs<M>
) -> &mut App
👎Deprecated since 0.11.0: Please use add_systems
instead. If you didn’t change the default base set, you should use add_systems(Startup, your_system).
pub fn add_startup_system<M>( &mut self, system: impl IntoSystemConfigs<M> ) -> &mut App
add_systems
instead. If you didn’t change the default base set, you should use add_systems(Startup, your_system).
Adds a system to Startup
.
These systems will run exactly once, at the start of the App
’s lifecycle.
To add a system that runs every frame, see add_system
.
Examples
fn my_startup_system(_commands: Commands) {
println!("My startup system");
}
App::new()
.add_systems(Startup, my_startup_system);
pub fn add_startup_systems<M>(
&mut self,
systems: impl IntoSystemConfigs<M>
) -> &mut App
👎Deprecated since 0.11.0: Please use add_systems
instead. If you didn’t change the default base set, you should use add_systems(Startup, your_system).
pub fn add_startup_systems<M>( &mut self, systems: impl IntoSystemConfigs<M> ) -> &mut App
add_systems
instead. If you didn’t change the default base set, you should use add_systems(Startup, your_system).
pub fn configure_set(
&mut self,
schedule: impl ScheduleLabel,
set: impl IntoSystemSetConfig
) -> &mut App
pub fn configure_set( &mut self, schedule: impl ScheduleLabel, set: impl IntoSystemSetConfig ) -> &mut App
Configures a system set in the default schedule, adding the set if it does not exist.
pub fn configure_sets(
&mut self,
schedule: impl ScheduleLabel,
sets: impl IntoSystemSetConfigs
) -> &mut App
pub fn configure_sets( &mut self, schedule: impl ScheduleLabel, sets: impl IntoSystemSetConfigs ) -> &mut App
Configures a collection of system sets in the default schedule, adding any sets that do not exist.
pub fn add_event<T>(&mut self) -> &mut Appwhere
T: Event,
pub fn add_event<T>(&mut self) -> &mut Appwhere T: Event,
Setup the application to manage events of type T
.
This is done by adding a Resource
of type Events::<T>
,
and inserting an update_system
into First
.
See Events
for defining events.
Examples
app.add_event::<MyEvent>();
pub fn insert_resource<R>(&mut self, resource: R) -> &mut Appwhere
R: Resource,
pub fn insert_resource<R>(&mut self, resource: R) -> &mut Appwhere R: Resource,
Inserts a Resource
to the current App
and overwrites any Resource
previously added of the same type.
A Resource
in Bevy represents globally unique data. Resource
s must be added to Bevy apps
before using them. This happens with insert_resource
.
See init_resource
for Resource
s that implement Default
or FromWorld
.
Examples
#[derive(Resource)]
struct MyCounter {
counter: usize,
}
App::new()
.insert_resource(MyCounter { counter: 0 });
pub fn insert_non_send_resource<R>(&mut self, resource: R) -> &mut Appwhere
R: 'static,
pub fn insert_non_send_resource<R>(&mut self, resource: R) -> &mut Appwhere R: 'static,
Inserts a non-send resource to the app.
You usually want to use insert_resource
,
but there are some special cases when a resource cannot be sent across threads.
Examples
struct MyCounter {
counter: usize,
}
App::new()
.insert_non_send_resource(MyCounter { counter: 0 });
pub fn init_resource<R>(&mut self) -> &mut Appwhere
R: Resource + FromWorld,
pub fn init_resource<R>(&mut self) -> &mut Appwhere R: Resource + FromWorld,
Initialize a Resource
with standard starting values by adding it to the World
.
If the Resource
already exists, nothing happens.
The Resource
must implement the FromWorld
trait.
If the Default
trait is implemented, the FromWorld
trait will use
the Default::default
method to initialize the Resource
.
Examples
#[derive(Resource)]
struct MyCounter {
counter: usize,
}
impl Default for MyCounter {
fn default() -> MyCounter {
MyCounter {
counter: 100
}
}
}
App::new()
.init_resource::<MyCounter>();
pub fn init_non_send_resource<R>(&mut self) -> &mut Appwhere
R: 'static + FromWorld,
pub fn init_non_send_resource<R>(&mut self) -> &mut Appwhere R: 'static + FromWorld,
pub fn set_runner(
&mut self,
run_fn: impl FnOnce(App) + Send + 'static
) -> &mut App
pub fn set_runner( &mut self, run_fn: impl FnOnce(App) + Send + 'static ) -> &mut App
Sets the function that will be called when the app is run.
The runner function run_fn
is called only once by App::run
. If the
presence of a main loop in the app is desired, it is the responsibility of the runner
function to provide it.
The runner function is usually not set manually, but by Bevy integrated plugins
(e.g. WinitPlugin
).
Examples
fn my_runner(mut app: App) {
loop {
println!("In main loop");
app.update();
}
}
App::new()
.set_runner(my_runner);
pub fn add_plugin<T>(&mut self, plugin: T) -> &mut Appwhere
T: Plugin,
👎Deprecated since 0.11.0: Please use add_plugins
instead.
pub fn add_plugin<T>(&mut self, plugin: T) -> &mut Appwhere T: Plugin,
add_plugins
instead.Adds a single Plugin
.
One of Bevy’s core principles is modularity. All Bevy engine features are implemented
as Plugin
s. This includes internal features like the renderer.
Bevy also provides a few sets of default Plugin
s. See add_plugins
.
Examples
App::new().add_plugin(bevy_log::LogPlugin::default());
Panics
Panics if the plugin was already added to the application.
pub fn is_plugin_added<T>(&self) -> boolwhere
T: Plugin,
pub fn is_plugin_added<T>(&self) -> boolwhere T: Plugin,
Checks if a Plugin
has already been added.
This can be used by plugins to check if a plugin they depend upon has already been added.
pub fn get_added_plugins<T>(&self) -> Vec<&T, Global>where
T: Plugin,
pub fn get_added_plugins<T>(&self) -> Vec<&T, Global>where T: Plugin,
Returns a vector of references to any plugins of type T
that have been added.
This can be used to read the settings of any already added plugins.
This vector will be length zero if no plugins of that type have been added.
If multiple copies of the same plugin are added to the App
, they will be listed in insertion order in this vector.
let default_sampler = app.get_added_plugins::<ImagePlugin>()[0].default_sampler;
pub fn add_plugins<M>(&mut self, plugins: impl Plugins<M>) -> &mut App
pub fn add_plugins<M>(&mut self, plugins: impl Plugins<M>) -> &mut App
Adds one or more Plugin
s.
One of Bevy’s core principles is modularity. All Bevy engine features are implemented
as Plugin
s. This includes internal features like the renderer.
Plugin
s can be grouped into a set by using a PluginGroup
.
There are built-in PluginGroup
s that provide core engine functionality.
The PluginGroup
s available by default are DefaultPlugins
and MinimalPlugins
.
To customize the plugins in the group (reorder, disable a plugin, add a new plugin
before / after another plugin), call build()
on the group,
which will convert it to a PluginGroupBuilder
.
You can also specify a group of Plugin
s by using a tuple over Plugin
s and
PluginGroup
s. See Plugins
for more details.
Examples
App::new()
.add_plugins(MinimalPlugins);
App::new()
.add_plugins((MinimalPlugins, LogPlugin));
Panics
Panics if one of the plugins was already added to the application.
pub fn register_type<T>(&mut self) -> &mut Appwhere
T: GetTypeRegistration,
pub fn register_type<T>(&mut self) -> &mut Appwhere T: GetTypeRegistration,
Registers the type T
in the TypeRegistry
resource,
adding reflect data as specified in the Reflect
derive:
#[derive(Reflect)]
#[reflect(Component, Serialize, Deserialize)] // will register ReflectComponent, ReflectSerialize, ReflectDeserialize
pub fn register_type_data<T, D>(&mut self) -> &mut Appwhere
T: Reflect + 'static,
D: TypeData + FromType<T>,
pub fn register_type_data<T, D>(&mut self) -> &mut Appwhere T: Reflect + 'static, D: TypeData + FromType<T>,
Adds the type data D
to type T
in the TypeRegistry
resource.
Most of the time App::register_type
can be used instead to register a type you derived Reflect
for.
However, in cases where you want to add a piece of type data that was not included in the list of #[reflect(...)]
type data in the derive,
or where the type is generic and cannot register e.g. ReflectSerialize
unconditionally without knowing the specific type parameters,
this method can be used to insert additional type data.
Example
use bevy_app::App;
use bevy_reflect::{ReflectSerialize, ReflectDeserialize};
App::new()
.register_type::<Option<String>>()
.register_type_data::<Option<String>, ReflectSerialize>()
.register_type_data::<Option<String>, ReflectDeserialize>();
pub fn sub_app_mut(&mut self, label: impl AppLabel) -> &mut App
pub fn sub_app_mut(&mut self, label: impl AppLabel) -> &mut App
pub fn get_sub_app_mut(
&mut self,
label: impl AppLabel
) -> Result<&mut App, AppLabelId>
pub fn get_sub_app_mut( &mut self, label: impl AppLabel ) -> Result<&mut App, AppLabelId>
pub fn insert_sub_app(&mut self, label: impl AppLabel, sub_app: SubApp)
pub fn insert_sub_app(&mut self, label: impl AppLabel, sub_app: SubApp)
Inserts an existing sub app into the app
pub fn remove_sub_app(&mut self, label: impl AppLabel) -> Option<SubApp>
pub fn remove_sub_app(&mut self, label: impl AppLabel) -> Option<SubApp>
Removes a sub app from the app. Returns None
if the label doesn’t exist.
pub fn get_sub_app(&self, label: impl AppLabel) -> Result<&App, impl AppLabel>
pub fn get_sub_app(&self, label: impl AppLabel) -> Result<&App, impl AppLabel>
pub fn add_schedule(
&mut self,
label: impl ScheduleLabel,
schedule: Schedule
) -> &mut App
pub fn add_schedule( &mut self, label: impl ScheduleLabel, schedule: Schedule ) -> &mut App
pub fn init_schedule(&mut self, label: impl ScheduleLabel) -> &mut App
pub fn init_schedule(&mut self, label: impl ScheduleLabel) -> &mut App
Initializes a new empty schedule
to the App
under the provided label
if it does not exists.
See App::add_schedule
to pass in a pre-constructed schedule.
pub fn get_schedule(&self, label: impl ScheduleLabel) -> Option<&Schedule>
pub fn get_schedule(&self, label: impl ScheduleLabel) -> Option<&Schedule>
Gets read-only access to the Schedule
with the provided label
if it exists.
pub fn get_schedule_mut(
&mut self,
label: impl ScheduleLabel
) -> Option<&mut Schedule>
pub fn get_schedule_mut( &mut self, label: impl ScheduleLabel ) -> Option<&mut Schedule>
Gets read-write access to a Schedule
with the provided label
if it exists.
pub fn edit_schedule(
&mut self,
label: impl ScheduleLabel,
f: impl FnOnce(&mut Schedule)
) -> &mut App
pub fn edit_schedule( &mut self, label: impl ScheduleLabel, f: impl FnOnce(&mut Schedule) ) -> &mut App
Applies the function to the Schedule
associated with label
.
Note: This will create the schedule if it does not already exist.
Trait Implementations§
§impl AddAsset for App
impl AddAsset for App
§fn add_asset<T>(&mut self) -> &mut Appwhere
T: Asset,
fn add_asset<T>(&mut self) -> &mut Appwhere T: Asset,
T
as a supported asset in the application. Read more§fn register_asset_reflect<T>(&mut self) -> &mut Appwhere
T: Asset + Reflect + FromReflect + GetTypeRegistration,
fn register_asset_reflect<T>(&mut self) -> &mut Appwhere T: Asset + Reflect + FromReflect + GetTypeRegistration,
T
using App::register_type
,
and adds ReflectAsset
type data to T
and ReflectHandle
type data to Handle<T>
in the type registry. Read more§fn add_debug_asset<T>(&mut self) -> &mut Appwhere
T: Clone + Asset,
fn add_debug_asset<T>(&mut self) -> &mut Appwhere T: Clone + Asset,
T
as a supported internal asset in the application. Read more§fn init_asset_loader<T>(&mut self) -> &mut Appwhere
T: AssetLoader + FromWorld,
fn init_asset_loader<T>(&mut self) -> &mut Appwhere T: AssetLoader + FromWorld,
T
using default values. Read more§fn init_debug_asset_loader<T>(&mut self) -> &mut Appwhere
T: AssetLoader + FromWorld,
fn init_debug_asset_loader<T>(&mut self) -> &mut Appwhere T: AssetLoader + FromWorld,
T
for internal assets using default values. Read more§fn add_asset_loader<T>(&mut self, loader: T) -> &mut Appwhere
T: AssetLoader,
fn add_asset_loader<T>(&mut self, loader: T) -> &mut Appwhere T: AssetLoader,
§fn preregister_asset_loader(&mut self, extensions: &[&str]) -> &mut App
fn preregister_asset_loader(&mut self, extensions: &[&str]) -> &mut App
§impl AddAudioSource for App
impl AddAudioSource for App
§fn add_audio_source<T>(&mut self) -> &mut Appwhere
T: Decodable + Asset,
f32: FromSample<<T as Decodable>::DecoderItem>,
fn add_audio_source<T>(&mut self) -> &mut Appwhere T: Decodable + Asset, f32: FromSample<<T as Decodable>::DecoderItem>,
§impl AddRenderCommand for App
impl AddRenderCommand for App
§fn add_render_command<P, C>(&mut self) -> &mut Appwhere
P: PhaseItem,
C: RenderCommand<P> + Send + Sync + 'static,
<C as RenderCommand<P>>::Param: ReadOnlySystemParam,
fn add_render_command<P, C>(&mut self) -> &mut Appwhere P: PhaseItem, C: RenderCommand<P> + Send + Sync + 'static, <C as RenderCommand<P>>::Param: ReadOnlySystemParam,
RenderCommand
for the specified RenderPhase
to the app.§impl RegisterDiagnostic for App
impl RegisterDiagnostic for App
§fn register_diagnostic(&mut self, diagnostic: Diagnostic) -> &mut App
fn register_diagnostic(&mut self, diagnostic: Diagnostic) -> &mut App
Register a new Diagnostic
with an App
.
Will initialize a DiagnosticsStore
if it doesn’t exist.
use bevy_app::App;
use bevy_diagnostic::{Diagnostic, DiagnosticsPlugin, DiagnosticId, RegisterDiagnostic};
const UNIQUE_DIAG_ID: DiagnosticId = DiagnosticId::from_u128(42);
App::new()
.register_diagnostic(Diagnostic::new(UNIQUE_DIAG_ID, "example", 10))
.add_plugins(DiagnosticsPlugin)
.run();
§impl RenderGraphApp for App
impl RenderGraphApp for App
§fn add_render_graph_node<T>(
&mut self,
sub_graph_name: &'static str,
node_name: &'static str
) -> &mut Appwhere
T: Node + FromWorld,
fn add_render_graph_node<T>( &mut self, sub_graph_name: &'static str, node_name: &'static str ) -> &mut Appwhere T: Node + FromWorld,
§fn add_render_graph_edges(
&mut self,
sub_graph_name: &'static str,
edges: &[&'static str]
) -> &mut App
fn add_render_graph_edges( &mut self, sub_graph_name: &'static str, edges: &[&'static str] ) -> &mut App
§fn add_render_graph_edge(
&mut self,
sub_graph_name: &'static str,
output_edge: &'static str,
input_edge: &'static str
) -> &mut App
fn add_render_graph_edge( &mut self, sub_graph_name: &'static str, output_edge: &'static str, input_edge: &'static str ) -> &mut App
fn add_render_sub_graph(&mut self, sub_graph_name: &'static str) -> &mut App
Auto Trait Implementations§
impl !RefUnwindSafe for App
impl Send for App
impl !Sync for App
impl Unpin for App
impl !UnwindSafe for App
Blanket Implementations§
§impl<T, U> AsBindGroupShaderType<U> for Twhere
U: ShaderType,
&'a T: for<'a> Into<U>,
impl<T, U> AsBindGroupShaderType<U> for Twhere U: ShaderType, &'a T: for<'a> Into<U>,
§fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
T
ShaderType
for self
. When used in AsBindGroup
derives, it is safe to assume that all images in self
exist.source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere T: Any,
§fn into_any(self: Box<T, Global>) -> Box<dyn Any, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any, Global>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T, Global>) -> Rc<dyn Any, Global>
fn into_any_rc(self: Rc<T, Global>) -> Rc<dyn Any, Global>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere T: Default,
§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Self
using data from the given World