Skip to content

Add World::try_run_schedule - #8028

Merged
alice-i-cecile merged 10 commits into
bevyengine:mainfrom
ItsDoot:feature/try-run-schedule
Mar 17, 2023
Merged

alice-i-cecile merged 10 commits into
bevyengine:mainfrom
ItsDoot:feature/try-run-schedule

Conversation

@ItsDoot

@ItsDoot ItsDoot commented Mar 10, 2023

Copy link
Copy Markdown
Contributor

Objective

Fix #8018

Solution

Added a new method World::try_run_schedule that returns a Result instead of panicking if the schedule doesn't exist.


Changelog

Added

  • World::try_run_schedule: Attempt to run a schedule and return a Result rather than panicking if the schedule doesn't exist.

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Mar 10, 2023
@alice-i-cecile

Copy link
Copy Markdown
Member

The diff / commit history looks wrong, so this is quite hard to review. Are you able to clean that up?

@ItsDoot
ItsDoot marked this pull request as draft March 10, 2023 20:03
@ItsDoot

ItsDoot commented Mar 10, 2023

Copy link
Copy Markdown
Contributor Author

Yea sorry about that @alice-i-cecile, messed up my history trying to rebase, fixing it now. 😅

@ItsDoot
ItsDoot force-pushed the feature/try-run-schedule branch from 69b1f51 to bfbffd3 Compare March 10, 2023 20:11
@ItsDoot
ItsDoot marked this pull request as ready for review March 10, 2023 20:12
Comment thread crates/bevy_ecs/src/world/error.rs Resolved
@alice-i-cecile

Copy link
Copy Markdown
Member

Code itself LGTM. Can we use this internally anywhere? I know that state transitions wanted this: do enter / exit want it too already?

@ItsDoot

ItsDoot commented Mar 10, 2023

Copy link
Copy Markdown
Contributor Author

We could certainly move towards full lazy initialization for State schedules by removing these:

for variant in S::variants() {
if self.get_schedule(OnEnter(variant.clone())).is_none() {
self.add_schedule(OnEnter(variant.clone()), Schedule::new());
}
if self.get_schedule(OnExit(variant.clone())).is_none() {
self.add_schedule(OnExit(variant), Schedule::new());
}
}

And using try_run_schedule in these places:

pub fn run_enter_schedule<S: States>(world: &mut World) {
world.run_schedule(OnEnter(world.resource::<State<S>>().0.clone()));
}

world.run_schedule(OnExit(exited.clone()));
// If the schedule doesn't exist, we don't care.
let _ = world.try_run_schedule(OnTransition {
from: exited,
to: entered.clone(),
});
world.run_schedule(OnEnter(entered));

@james7132 @alice-i-cecile Thoughts?

@alice-i-cecile

Copy link
Copy Markdown
Member

I like that much better!

Comment thread crates/bevy_app/src/app.rs Resolved
Comment thread crates/bevy_ecs/src/schedule/state.rs Resolved
Co-authored-by: James Liu <contact@jamessliu.com>
Comment thread crates/bevy_ecs/src/schedule/state.rs Resolved
@james7132 james7132 modified the milestones: 0.10.1, 0.11 Mar 16, 2023
@alice-i-cecile
alice-i-cecile added this pull request to the merge queue Mar 17, 2023
@alice-i-cecile alice-i-cecile 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 17, 2023
Merged via the queue into bevyengine:main with commit 609b099 Mar 17, 2023
ProfLander pushed a commit to Lander-Rust/bevy that referenced this pull request Mar 19, 2023
Co-authored-by: James Liu <contact@jamessliu.com>
ProfLander pushed a commit to Lander-Rust/bevy that referenced this pull request Mar 19, 2023
Co-authored-by: James Liu <contact@jamessliu.com>
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-Usability A targeted quality-of-life change that makes Bevy easier to use 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.

Panic-less version of World::run_schedule

3 participants