Skip to content

Commit

Permalink
Fixed some build errors, fixed example
Browse files Browse the repository at this point in the history
  • Loading branch information
Trashtalk217 committed Aug 28, 2023
1 parent aa15370 commit 47c75fc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
5 changes: 2 additions & 3 deletions crates/bevy_app/src/system_registry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::App;
use bevy_ecs::{
prelude::*,
};
use bevy_ecs::prelude::*;
use bevy_ecs::system::{SystemId, SystemRegistryError};

impl App {
/// Runs the supplied system on the [`World`] a single time.
Expand Down
9 changes: 8 additions & 1 deletion crates/bevy_ecs/src/system/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
self as bevy_ecs,
bundle::Bundle,
entity::{Entities, Entity},
system::{IntoSystem, RunSystem},
system::{IntoSystem, RunSystem, RunSystemById, SystemId},
world::{FromWorld, World},
};
use bevy_ecs_macros::SystemParam;
Expand Down Expand Up @@ -533,6 +533,13 @@ impl<'w, 's> Commands<'w, 's> {
self.queue.push(RunSystem::new(system));
}

/// Runs the system corresponding to the given [`SystemId`].
///
/// Calls [`SystemRegistry::run_system_by_id`](crate::SystemRegistry::run_system_by_id).
pub fn run_system_by_id(&mut self, id: SystemId) {
self.queue.push(RunSystemById::new(id));
}

/// Pushes a generic [`Command`] to the command queue.
///
/// `command` can be a built-in command, custom struct that implements [`Command`] or a closure
Expand Down
13 changes: 7 additions & 6 deletions crates/bevy_ecs/src/system/system_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl SystemRegistry {
///
/// Repeatedly registering a system will have no effect.
#[inline]
fn register<M, S: IntoSystem<(), (), M> + 'static>(&mut self, system: S) -> SystemId {
pub fn register<M, S: IntoSystem<(), (), M> + 'static>(&mut self, system: S) -> SystemId {
let id = self.last_id + 1;
self.last_id = id;
self.systems
Expand All @@ -112,10 +112,10 @@ impl SystemRegistry {
/// System state will not be reused between runs, so [`Local`](crate::system::Local) variables are not preserved between runs.
/// To preserve [`Local`](crate::system::Local) variables between runs, it's possible to register and run the system by id manually.
pub fn run<M, S: IntoSystem<(), (), M> + 'static>(&mut self, world: &mut World, system: S) {
let id = self.register(system);
self.run_by_id(world, id)
.expect("System was registered before running");
self.remove(id);
let mut boxed_system: BoxedSystem = Box::new(IntoSystem::into_system(system));
boxed_system.initialize(world);
boxed_system.run((), world);
boxed_system.apply_deferred(world);
}

/// Run the system by its [`SystemId`]
Expand Down Expand Up @@ -224,10 +224,11 @@ impl<M: Send + Sync + 'static, S: IntoSystem<(), (), M> + Send + Sync + 'static>
/// The [`Command`] type for [`SystemRegistry::run_by_id`].
#[derive(Debug, Clone)]
pub struct RunSystemById {
pub system_id: SystemId,
system_id: SystemId,
}

impl RunSystemById {
/// Creates a new [`Command`] struct, which can be added to [`Commands`](crate::system::Commands)
pub fn new(system_id: SystemId) -> Self {
Self { system_id }
}
Expand Down
8 changes: 3 additions & 5 deletions examples/ecs/one_shot_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ use bevy::{

fn main() {
App::new()
.add_startup_system(count_entities)
.add_startup_system(setup)
.add_systems(Startup, (count_entities, setup))
// One shot systems are interchangeable with ordinarily scheduled systems.
// Change detection, Local and NonSend all work as expected.
.add_system(count_entities.in_base_set(CoreSet::PostUpdate))
.add_systems(PostUpdate, count_entities)
// One-shot systems can be used to build complex abstractions
// to match the needs of your design.
// Here, we model a very simple component-linked callback architecture.
.add_system(evaluate_callbacks)
.add_systems(Update, evaluate_callbacks)
.run();
}

Expand Down

0 comments on commit 47c75fc

Please sign in to comment.