Skip to content

Schedule enter and exit hooks#23738

Open
MushineLament wants to merge 5 commits intobevyengine:mainfrom
MushineLament:schedulehook
Open

Schedule enter and exit hooks#23738
MushineLament wants to merge 5 commits intobevyengine:mainfrom
MushineLament:schedulehook

Conversation

@MushineLament
Copy link
Copy Markdown
Contributor

Objective

Fixes #23191

Solution

#23191 (comment)

Testing

I've done some simple tests to ensure the hooks work as expected.
However, I haven't conducted any performance tests, and I don't have much experience in that area.

The documentation still needs improvement, and some other features also need to be refined.

@MushineLament
Copy link
Copy Markdown
Contributor Author

Hi @alice-i-cecile , here's a proposal about schedule hooks. I think this feature can support dynamic systems for schedules. I hope this proposal can be included in the 0.19 milestone. I was wondering if you might be interested.

@kfc35 kfc35 added C-Feature A new feature, making something new possible A-ECS Entities, components, systems, and events D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 9, 2026
@github-project-automation github-project-automation bot moved this to Needs SME Triage in ECS Apr 9, 2026
Copy link
Copy Markdown
Contributor

@andriyDev andriyDev left a comment

Choose a reason for hiding this comment

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

I don't really care for this change. Hooking on to the schedule execution seems like it would make things slow for the common case of not using this feature.

Even ignoring that, I think this feature needs more motivation. The issue talks about making schedules more dynamic - but this PR won't help with that since the hook can't access the schedule, because the schedule is removed from the world to run it. If you want to mutate schedules, just change them in a system outside the schedule. So for example, in a system in Startup.

self.try_schedule_scope(label, |world, sched| sched.run(world))
self.try_schedule_scope(label.intern(), |world, sched| {
world.try_resource_scope::<ScheduleHooks, ()>(|world, mut hooks| {
hooks.run_enter(world, label.intern());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not just trigger an observer instead? Then there's no need to make a whole custom "hooking" thing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I haven't paid attention to observers for a long time. After completing the rough design, I will give it a try.

@alice-i-cecile
Copy link
Copy Markdown
Member

Hi @alice-i-cecile , here's a proposal about schedule hooks. I think this feature can support dynamic systems for schedules. I hope this proposal can be included in the 0.19 milestone. I was wondering if you might be interested.

Definitely not at this stage.

Overall the idea has some promise as a way to simplify our state handling code, but I'm still not convinced of the need (or that this is a correct design. I can't dedicate the time and energy to review this properly right now, but if you can convince others that this is useful I will prioritize it more heavily.

@alice-i-cecile alice-i-cecile added S-Needs-Goal This should have a C-Goal and should not continue until it has one and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 10, 2026
@alice-i-cecile alice-i-cecile moved this from Needs SME Triage to SME Triaged in ECS Apr 10, 2026
@MushineLament
Copy link
Copy Markdown
Contributor Author

I don't really care for this change. Hooking on to the schedule execution seems like it would make things slow for the common case of not using this feature.

Even ignoring that, I think this feature needs more motivation. The issue talks about making schedules more dynamic - but this PR won't help with that since the hook can't access the schedule, because the schedule is removed from the world to run it. If you want to mutate schedules, just change them in a system outside the schedule. So for example, in a system in Startup.

Performance issues still need optimization. My original goal was to implement two things: transferring a CommandQueue to another schedule for deferred processing, and modifying the system set of the current schedule. I know that schedule modifications can be done in other stages, but that requires the target schedule to have a system that handles and stores the changed data (which would be repetitive if every project had to do it). That brings us back to the first requirement. Deferred merging of CommandQueue across schedules (see #23145) — i.e., putting the queue into a specific ScheduleLabel and merging it — is complicated.

Executing certain behaviors on schedule entry and exit is an extended idea. As far as I know, within a schedule, there is no way to guarantee which system runs first, and the Commands queue runs last. However, this extended idea is closely related to the two previous requirements.

Therefore, my goal is to bridge the gaps between different schedules and prepare the execution for the next schedule.

@MushineLament
Copy link
Copy Markdown
Contributor Author

@andriyDev You're right, observers are more suitable. I've adjusted the implementation to better meet the requirements.

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 D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Goal This should have a C-Goal and should not continue until it has one

Projects

Status: SME Triaged

Development

Successfully merging this pull request may close these issues.

Add a hook to the schedule

4 participants