bevy_app

Struct SubApp

source
pub struct SubApp {
    pub update_schedule: Option<InternedScheduleLabel>,
    /* private fields */
}
Expand description

A secondary application with its own World. These can run independently of each other.

These are useful for situations where certain processes (e.g. a render thread) need to be kept separate from the main application.

§Example


#[derive(Resource, Default)]
struct Val(pub i32);

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, AppLabel)]
struct ExampleApp;

// Create an app with a certain resource.
let mut app = App::new();
app.insert_resource(Val(10));

// Create a sub-app with the same resource and a single schedule.
let mut sub_app = SubApp::new();
sub_app.insert_resource(Val(100));

// Setup an extract function to copy the resource's value in the main world.
sub_app.set_extract(|main_world, sub_world| {
    sub_world.resource_mut::<Val>().0 = main_world.resource::<Val>().0;
});

// Schedule a system that will verify extraction is working.
sub_app.add_systems(Main, |counter: Res<Val>| {
    // The value will be copied during extraction, so we should see 10 instead of 100.
    assert_eq!(counter.0, 10);
});

// Add the sub-app to the main app.
app.insert_sub_app(ExampleApp, sub_app);

// Update the application once (using the default runner).
app.run();

Fields§

§update_schedule: Option<InternedScheduleLabel>

The schedule that will be run by update.

Implementations§

source§

impl SubApp

source

pub fn new() -> Self

Returns a default, empty SubApp.

source

pub fn world(&self) -> &World

Returns a reference to the World.

source

pub fn world_mut(&mut self) -> &mut World

Returns a mutable reference to the World.

source

pub fn run_default_schedule(&mut self)

Runs the default schedule.

Does not clear internal trackers used for change detection.

source

pub fn update(&mut self)

Runs the default schedule and updates internal component trackers.

source

pub fn extract(&mut self, world: &mut World)

Extracts data from world into the app’s world using the registered extract method.

Note: There is no default extract method. Calling extract does nothing if set_extract has not been called.

source

pub fn set_extract<F>(&mut self, extract: F) -> &mut Self
where F: Fn(&mut World, &mut World) + Send + 'static,

Sets the method that will be called by extract.

The first argument is the World to extract data from, the second argument is the app World.

source

pub fn take_extract( &mut self, ) -> Option<Box<dyn Fn(&mut World, &mut World) + Send>>

Take the function that will be called by extract out of the app, if any was set, and replace it with None.

If you use Bevy, bevy_render will set a default extract function used to extract data from the main world into the render world as part of the Extract phase. In that case, you cannot replace it with your own function. Instead, take the Bevy default function with this, and install your own instead which calls the Bevy default.

let default_fn = app.take_extract();
app.set_extract(move |main, render| {
    // Do pre-extract custom logic
    // [...]

    // Call Bevy's default, which executes the Extract phase
    if let Some(f) = default_fn.as_ref() {
        f(main, render);
    }

    // Do post-extract custom logic
    // [...]
});
source

pub fn insert_resource<R: Resource>(&mut self, resource: R) -> &mut Self

source

pub fn init_resource<R: Resource + FromWorld>(&mut self) -> &mut Self

source

pub fn add_systems<M>( &mut self, schedule: impl ScheduleLabel, systems: impl IntoSystemConfigs<M>, ) -> &mut Self

source

pub fn register_system<I, O, M>( &mut self, system: impl IntoSystem<I, O, M> + 'static, ) -> SystemId<I, O>
where I: SystemInput + 'static, O: 'static,

source

pub fn configure_sets( &mut self, schedule: impl ScheduleLabel, sets: impl IntoSystemSetConfigs, ) -> &mut Self

source

pub fn add_schedule(&mut self, schedule: Schedule) -> &mut Self

source

pub fn init_schedule(&mut self, label: impl ScheduleLabel) -> &mut Self

source

pub fn get_schedule(&self, label: impl ScheduleLabel) -> Option<&Schedule>

source

pub fn get_schedule_mut( &mut self, label: impl ScheduleLabel, ) -> Option<&mut Schedule>

source

pub fn edit_schedule( &mut self, label: impl ScheduleLabel, f: impl FnMut(&mut Schedule), ) -> &mut Self

source

pub fn configure_schedules( &mut self, schedule_build_settings: ScheduleBuildSettings, ) -> &mut Self

source

pub fn allow_ambiguous_component<T: Component>(&mut self) -> &mut Self

source

pub fn allow_ambiguous_resource<T: Resource>(&mut self) -> &mut Self

source

pub fn ignore_ambiguity<M1, M2, S1, S2>( &mut self, schedule: impl ScheduleLabel, a: S1, b: S2, ) -> &mut Self
where S1: IntoSystemSet<M1>, S2: IntoSystemSet<M2>,

source

pub fn add_event<T>(&mut self) -> &mut Self
where T: Event,

source

pub fn add_plugins<M>(&mut self, plugins: impl Plugins<M>) -> &mut Self

source

pub fn is_plugin_added<T>(&self) -> bool
where T: Plugin,

source

pub fn get_added_plugins<T>(&self) -> Vec<&T>
where T: Plugin,

source

pub fn plugins_state(&mut self) -> PluginsState

Return the state of plugins.

source

pub fn finish(&mut self)

Runs Plugin::finish for each plugin.

source

pub fn cleanup(&mut self)

Runs Plugin::cleanup for each plugin.

source

pub fn register_type<T: GetTypeRegistration>(&mut self) -> &mut Self

source

pub fn register_type_data<T: Reflect + TypePath, D: TypeData + FromType<T>>( &mut self, ) -> &mut Self

Trait Implementations§

source§

impl Debug for SubApp

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for SubApp

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !Freeze for SubApp

§

impl !RefUnwindSafe for SubApp

§

impl Send for SubApp

§

impl !Sync for SubApp

§

impl Unpin for SubApp

§

impl !UnwindSafe for SubApp

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> Downcast for T
where T: Any,

source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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.
source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> FromWorld for T
where T: Default,

source§

fn from_world(_world: &mut World) -> T

Creates Self using default().

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> ConditionalSend for T
where T: Send,