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

Replace multiple calls to add_system with add_systems #8001

Merged
merged 12 commits into from
Mar 10, 2023

Conversation

JoJoJet
Copy link
Member

@JoJoJet JoJoJet commented Mar 9, 2023

Objective

When multiple systems are added to an app or schedule, prefer using add_systems. This makes the code less repetitive, and usually results in nicer formatting.

Example

// Before:
schedule.add_system(ab).add_system(cd).add_system(ce);

// After:
schedule.add_systems((ab, cd, ce));

@JoJoJet JoJoJet added A-ECS Entities, components, systems, and events C-Code-Quality A section of code that is hard to understand or change labels Mar 9, 2023
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.

Generally in favor of these changes. Most things are easier to read and understand. Had some comments around a few places where chain could have been used.

When we start applying different restrictions to multiple systems like with

.add_system((
    system1.in_set(Prepare),
    system2.in_set(Prepare),
    system3.in_set(Queue),
));

I think there are stylistic choices where it isn't clear what the best way to write things is. The above could also be written as:

.add_systems((system1, system2).in_set(Prepare))
.add_system(system3.in_set(Queue))

Probably not worth debating at this point, since we have some open issues around limitations of add_systems that could make this point moot.

));

schedule.configure_sets((
PreStartup.before(PreStartupFlush),
Copy link
Contributor

Choose a reason for hiding this comment

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

can this use chain?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not really. These sets aren't being ordered with respect to each other, they're ordered with respect to *Flush sets.

Copy link
Contributor

Choose a reason for hiding this comment

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

schedule.configure_sets((
    PreStartup,
    PreStartupFlush,
    Startup,
    StartupFlush,
    PostStartup,
    PostStartupFlush
).chain());

Isn't this the same?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ooh I like that

crates/bevy_ecs/src/system/mod.rs Outdated Show resolved Hide resolved
crates/bevy_render/src/lib.rs Outdated Show resolved Hide resolved
.add_startup_system(setup_camera_fog)
.add_startup_system(setup_terrain_scene)
.add_startup_system(setup_instructions)
.add_startup_systems((setup_camera_fog, setup_terrain_scene, setup_instructions))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.add_startup_systems((setup_camera_fog, setup_terrain_scene, setup_instructions))
.add_systems((
setup_camera_fog,
setup_terrain_scene,
setup_instructions
).on_startup())

We've removed most of the other add_startup_system calls.

Copy link
Member Author

Choose a reason for hiding this comment

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

My main issue with this syntax is that rustfmt makes it look horrible. It'll become something like

add_systems(
    (
        setup_camera_fog,
        setup_terrain_scene,
        setup_instructions,
    )
        .on_startup(),
)

It's not exactly a dealbreaker, but for the time being I prefer it the way it is now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Once we get recursive SystemConfigs working, we should be able to make this look really nice:

add_systems((
    (setup_camera_fog, setup_terrain_scene, setup_instructions).on_startup(),
    toggle_system,
))

Copy link
Member

@james7132 james7132 left a comment

Choose a reason for hiding this comment

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

This in theory requires a lot more codegen than the original add_system chain. Wonder if this has any measurable impact on cargo build --timings.

@james7132 james7132 added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Mar 10, 2023
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Mar 10, 2023
Merged via the queue into bevyengine:main with commit fd1af7c Mar 10, 2023
@JoJoJet JoJoJet deleted the prefer-add-systems branch March 10, 2023 18:37
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Mar 10, 2023
Shfty pushed a commit to shfty-rust/bevy that referenced this pull request Mar 19, 2023
Shfty pushed a commit to shfty-rust/bevy that referenced this pull request Mar 19, 2023
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-Code-Quality A section of code that is hard to understand or change S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants