-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Add runtime system insertion #2507
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
Closed
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
cfd590c
Add ScheduleCommands
Alainx277 e5ffab9
Fix formatting
Alainx277 0f47181
Remove warning in impl_system_param_tuple
Alainx277 037ade3
Impl IntoSystemDescriptor for SystemDescriptor
Alainx277 35df178
Store SystemDescriptor in InsertSystem
Alainx277 be00194
Rename ScheduleCommand to SchedulerCommand
Alainx277 78bcd91
Use world to store scheduler commands
Alainx277 1c355bb
Improve system insertion test
Alainx277 7ec8468
Add doc string for insert_system
Alainx277 48c293c
Ignore existing scheduler commands on World
Alainx277 9511e21
Simplify scheduler command application
Alainx277 8d4f86b
Improve doctest for insert_system
Alainx277 8f76330
Add dynamic schedule example
Alainx277 b67050b
Fix formatting
Alainx277 98cb617
Satisfy clippy
Alainx277 b896bcd
Reuse material in dynamic_schedule example
Alainx277 c3d1be2
Improve insert_system example
Alainx277 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
use super::{Schedule, StageLabel, SystemDescriptor}; | ||
use crate::system::Command; | ||
use crate::world::World; | ||
|
||
#[derive(Default)] | ||
pub struct SchedulerCommandQueue { | ||
items: Vec<Box<dyn SchedulerCommand>>, | ||
} | ||
|
||
impl SchedulerCommandQueue { | ||
pub fn push<C>(&mut self, command: C) | ||
where | ||
C: SchedulerCommand, | ||
{ | ||
self.items.push(Box::new(command)); | ||
} | ||
|
||
pub fn apply(&mut self, schedule: &mut Schedule, offset: usize) { | ||
for command in self.items.drain(offset..) { | ||
command.write(schedule); | ||
} | ||
} | ||
|
||
pub fn len(&self) -> usize { | ||
self.items.len() | ||
} | ||
|
||
pub fn is_empty(&self) -> bool { | ||
self.items.is_empty() | ||
} | ||
} | ||
|
||
/// A [`Schedule`] mutation. | ||
pub trait SchedulerCommand: Send + Sync + 'static { | ||
fn write(self: Box<Self>, schedule: &mut Schedule); | ||
} | ||
|
||
impl<T> Command for T | ||
where | ||
T: SchedulerCommand, | ||
{ | ||
fn write(self, world: &mut World) { | ||
world.scheduler_commands.push(self); | ||
} | ||
} | ||
|
||
pub struct InsertSystem<S> | ||
where | ||
S: StageLabel, | ||
{ | ||
pub system: SystemDescriptor, | ||
pub stage_label: S, | ||
} | ||
|
||
impl<S> SchedulerCommand for InsertSystem<S> | ||
where | ||
S: StageLabel, | ||
{ | ||
fn write(self: Box<Self>, schedule: &mut Schedule) { | ||
schedule.add_system_to_stage(self.stage_label, self.system); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{ | ||
prelude::ResMut, | ||
schedule::{ | ||
InsertSystem, IntoSystemDescriptor, Schedule, SchedulerCommandQueue, SystemStage, | ||
}, | ||
system::Commands, | ||
world::World, | ||
}; | ||
|
||
#[test] | ||
fn insert_system() { | ||
fn sample_system(mut _commands: Commands) {} | ||
let mut schedule = Schedule::default(); | ||
schedule.add_stage("test", SystemStage::parallel()); | ||
let mut queue = SchedulerCommandQueue::default(); | ||
queue.push(InsertSystem { | ||
system: sample_system.into_descriptor(), | ||
stage_label: "test", | ||
}); | ||
queue.apply(&mut schedule, 0); | ||
|
||
let stage = schedule.get_stage::<SystemStage>(&"test").unwrap(); | ||
assert_eq!(stage.parallel_systems().len(), 1); | ||
} | ||
|
||
#[derive(Debug, Default, PartialEq)] | ||
struct TestResource(Option<()>); | ||
|
||
#[test] | ||
fn insert_system_from_system() { | ||
fn sample_system(mut commands: Commands) { | ||
commands.insert_system( | ||
|mut res: ResMut<TestResource>| { | ||
res.0 = Some(()); | ||
}, | ||
"test", | ||
); | ||
} | ||
|
||
let mut world = World::default(); | ||
world.insert_resource(TestResource(None)); | ||
|
||
let mut schedule = Schedule::default(); | ||
schedule.add_stage("test", SystemStage::parallel()); | ||
schedule.add_system_to_stage("test", sample_system); | ||
schedule.run_once(&mut world); | ||
|
||
let stage = schedule.get_stage::<SystemStage>(&"test").unwrap(); | ||
assert_eq!(stage.parallel_systems().len(), 2); | ||
|
||
schedule.run_once(&mut world); | ||
assert_eq!( | ||
world.get_resource::<TestResource>(), | ||
Some(&TestResource(Some(()))) | ||
); | ||
} | ||
|
||
#[test] | ||
fn insert_with_nested_schedule() { | ||
fn sample_system(mut commands: Commands) { | ||
commands.insert_system(|| {}, "test"); | ||
} | ||
|
||
let mut world = World::default(); | ||
|
||
let nested_schedule = Schedule::default(); | ||
let mut schedule = Schedule::default(); | ||
schedule.add_stage("test", SystemStage::parallel()); | ||
schedule.add_system_to_stage("test", sample_system); | ||
schedule.add_stage("nested", nested_schedule); | ||
schedule.run_once(&mut world); | ||
|
||
let stage = schedule.get_stage::<SystemStage>(&"test").unwrap(); | ||
assert_eq!(stage.parallel_systems().len(), 2); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use bevy::{core::FixedTimestep, prelude::*}; | ||
use rand::Rng; | ||
|
||
#[derive(Default)] | ||
struct BevyMaterial(Option<Handle<ColorMaterial>>); | ||
|
||
fn main() { | ||
App::build() | ||
.add_plugins(DefaultPlugins) | ||
.add_stage_after( | ||
CoreStage::Update, | ||
"slow", | ||
SystemStage::parallel().with_run_criteria(FixedTimestep::step(1.0)), | ||
) | ||
.add_startup_system(setup) | ||
.add_system(dynamic.with_run_criteria(FixedTimestep::step(1.0))) | ||
.init_resource::<BevyMaterial>() | ||
.run(); | ||
} | ||
|
||
fn setup( | ||
mut commands: Commands, | ||
asset_server: Res<AssetServer>, | ||
mut materials: ResMut<Assets<ColorMaterial>>, | ||
mut bevy_material: ResMut<BevyMaterial>, | ||
) { | ||
commands.spawn_bundle(OrthographicCameraBundle::new_2d()); | ||
|
||
let texture_handle = asset_server.load("branding/icon.png"); | ||
let material = materials.add(texture_handle.into()); | ||
bevy_material.0 = Some(material); | ||
} | ||
|
||
fn dynamic(mut commands: Commands, mut system_counter: Local<u64>) { | ||
let count = *system_counter; | ||
*system_counter += 1; | ||
let closure = move |mut commands: Commands, bevy_material: Res<BevyMaterial>| { | ||
info!("Hello from system {}", count); | ||
|
||
let mut rng = rand::thread_rng(); | ||
|
||
commands.spawn_bundle(SpriteBundle { | ||
material: bevy_material.0.clone().unwrap(), | ||
transform: Transform::from_xyz( | ||
rng.gen_range(-400f32..400f32), | ||
rng.gen_range(-400f32..400f32), | ||
0.0, | ||
), | ||
..Default::default() | ||
}); | ||
}; | ||
commands.insert_system(closure, "slow"); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.