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

Add RunSystem #9366

Merged
merged 3 commits into from
Aug 11, 2023
Merged

Add RunSystem #9366

merged 3 commits into from
Aug 11, 2023

Conversation

Zeenobit
Copy link
Contributor

@Zeenobit Zeenobit commented Aug 5, 2023

Add a RunSystem extension trait to allow for immediate execution of systems on a World for debugging and/or testing purposes.

Objective

Fixes #6184

Initially, I made this CL as ApplyCommands. After a discussion with @cart , we decided a more generic implementation would be better to support all systems. This is the new revised CL. Sorry for the long delay! 😅

This CL allows users to do this:

use bevy::prelude::*;
use bevy::ecs::system::RunSystem;

struct T(usize);

impl Resource for T {}

fn system(In(n): In<usize>, mut commands: Commands) -> usize {
    commands.insert_resource(T(n));
    n + 1
}

let mut world = World::default();
let n = world.run_system_with(1, system);
assert_eq!(n, 2);
assert_eq!(world.resource::<T>().0, 1);

Solution

This is implemented as a trait extension and not included in any preludes to ensure it's being used consciously.
Internally, it just initializes and runs a systems, and applies any deferred parameters all "in place".
The trait has 2 functions (one of which calls the other by default):

  • run_system_with is the general implementation, which allows user to pass system input parameters
  • run_system is the ergonomic wrapper for systems with no input parameter (to avoid having the user pass () as input).

Additionally, this trait is also implemented for &mut App. I added this mainly for ergonomics (app.run_system vs. app.world.run_system). (Removed based on feedback)

Add a `RunSystem` extension trait to allow for immediate execution of systems on a `World` for debugging and/or testing purposes.
Copy link
Contributor

@killercup killercup left a comment

Choose a reason for hiding this comment

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

Looks like a neat helper for tests.

crates/bevy_ecs/src/system/system.rs Show resolved Hide resolved
Co-authored-by: Pascal Hertleif <killercup@gmail.com>
Copy link
Contributor

@hymm hymm left a comment

Choose a reason for hiding this comment

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

I like this api much better than the previous pr.

Not a huge fan of implementing on &mut App just for decreased verbosity. A little worse ergonomics for tests is fine and we're polluting the public api. But not worth blocking on.

@Zeenobit
Copy link
Contributor Author

Not a huge fan of implementing on &mut App just for decreased verbosity

I was debating that myself. Especially because compared to the previous PR, with this one you don't really need the app anymore to test systems, and in the few cases where we do want the app, using app.world.run_system vs. app.run_system isn't that different.

So I think you're right. I'll remove the implementation for &mut App.

Remove `RunSystem` impl for `&mut App`
@cart cart added this pull request to the merge queue Aug 11, 2023
Merged via the queue into bevyengine:main with commit 1e170d2 Aug 11, 2023
@Zeenobit Zeenobit deleted the run_system branch August 11, 2023 21:09
@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-ECS Entities, components, systems, and events labels Aug 25, 2023
@alice-i-cecile alice-i-cecile mentioned this pull request Aug 25, 2023
@cart cart mentioned this pull request Oct 13, 2023
43 tasks
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.

Immediate Execution of Commands on &mut World
5 participants