-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Directly run system given world #2427
Changes from all commits
1a2cc63
8e99532
2496410
fc16b63
9b3a5fa
5e2c89e
34496eb
178840d
b24128a
e420351
440740c
da2f447
aa07241
d5835c1
faf36a7
f379b37
82c0443
c0a1801
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,38 @@ pub trait ExclusiveSystem: Send + Sync + 'static { | |
|
||
fn id(&self) -> SystemId; | ||
|
||
/// Runs the exclusive system in the world. | ||
/// | ||
/// Use [`run_direct`] instead if you are manually running a system outside of a schedule | ||
|
||
fn run(&mut self, world: &mut World); | ||
|
||
/// Runs the exclusive system directly on the world, correctly initializing its state first | ||
/// | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For both methods, it may be useful to describe how to persistently store and then access a system, rather than initializing it every frame. This reduces work by enabling query caching and also allows I'm just not sure that the API docs are the right place to put that; it feels like content that might be better suited to the book. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I suppose that the way to do this is to probably store the system, run The new method is much more convenient, but will probably perform worse for repeated invocations. Hmm. Maybe we also demonstrate how to store and run a stage in the same section of the book? |
||
/// # Example | ||
/// | ||
/// ```rust | ||
/// use bevy_ecs::prelude::*; | ||
/// | ||
/// struct Counter(u8); | ||
/// let mut world = World::new(); | ||
/// | ||
/// fn count_up(world: &mut World){ | ||
/// let mut counter = world.get_resource_mut::<Counter>().unwrap(); | ||
/// counter.0 += 1; | ||
/// } | ||
/// | ||
/// world.insert_resource::<Counter>(Counter(0)); | ||
/// count_up.exclusive_system().run_direct(&mut world); | ||
/// let counter = world.get_resource::<Counter>().unwrap(); | ||
/// assert_eq!(counter.0, 1); | ||
///``` | ||
fn run_direct(&mut self, world: &mut World) { | ||
self.initialize(world); | ||
self.run(world); | ||
} | ||
|
||
/// Initialize the World, so that the system can safely run | ||
fn initialize(&mut self, world: &mut World); | ||
|
||
fn check_change_tick(&mut self, change_tick: u32); | ||
|
@@ -48,6 +78,11 @@ impl ExclusiveSystem for ExclusiveSystemFn { | |
world.last_change_tick = saved_last_tick; | ||
} | ||
|
||
fn run_direct(&mut self, world: &mut World) { | ||
self.initialize(world); | ||
self.run(world); | ||
} | ||
|
||
fn initialize(&mut self, _: &mut World) {} | ||
|
||
fn check_change_tick(&mut self, change_tick: u32) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this imply that run has an additional safety requirement that
self
is initialised for this world?If that is so, then it should be
unsafe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not unsafe in the Rust sense; it merely panics with an opaque error message.