Skip to content
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

System Inputs, Outputs, Chaining, and Registration Ergo #876

Merged
merged 7 commits into from
Nov 17, 2020

Conversation

cart
Copy link
Member

@cart cart commented Nov 16, 2020

Time for another round of ECS improvements!

  • System Registration Ergonomics: add_system() now takes a generic IntoSystem type instead of a boxed system

    // old
    App::build()
      .add_system(my_system.system())
      .add_system(my_thread_local_system.thread_local_system())
      .run()
    
    // new
    App::build()
      .add_system(my_system)
      .add_system(my_thread_local_system)
       // you can do this now, which is pretty cool
      .add_system(|| println!("hello world"))
      .run()
  • System Inputs and Outputs: The System trait now has Input and Output associated types. System<Input = X, Output = Y>

    fn count_materials_system(
      In(count_materials): In<bool>,
      materials: Res<Assets<ColorMaterial>>) -> Option<usize> {
      if count_materials {
        Some(materials.len())
      } else {
         None
      }
    }
    
    fn system(world: &mut World, resources: &mut Resources) {
      let system = count_materials_system.system();
      if let Some(material_count) = system.run(true, world, resources) {
        println!("there are {} materials", material_count);
      } 
    }
  • System Chaining. This allows us to make "result systems" with error handlers (among other things)

    fn main() {
      App::build()
        .add_plugins(DefaultPlugins)
        .add_system(result_system.chain(error_handler))
        .run();
    }
    
    fn result_system(query: Query<&Transform>) -> Result<()> {
      let transform = query.get(SOME_ENTITY)?;
      println!("found entity transform: {:?}", transform);
      Ok(())
    }
    
    fn error_handler(In(result): In<Result<()>>) {
      if let Err(err) = result {
          println!("encountered an error {:?}", err);
      }
    }

Note: only System<Input = (), Output =()> systems can be added to a schedule. Schedules don't know how to populate inputs, nor do they know what to do with outputs.

@Plecra
Copy link
Contributor

Plecra commented Nov 17, 2020

A test for the inputs and outputs should be added to the examples for the normal reasons. They're unique enough that illustrating the feature is important.

@cart cart added A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible labels Nov 17, 2020
@cart cart merged commit 3a6f6de into bevyengine:master Nov 17, 2020
@@ -115,8 +114,13 @@ where
ThreadLocalExecution::NextFlush
}

fn run(&mut self, world: &World, resources: &Resources) {
(self.func)(&mut self.state, world, resources)
unsafe fn run_unsafe(

This comment was marked as resolved.

}

// This system takes a Result<usize> input and either prints the parsed value or the error message
// Try changing the Message resource to something that isn't an integer. You should see the error message printed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message is already not an integer. It is "hello".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! I changed it to hello to test the error case, then forgot to change it back. Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants