Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Timing regarding hooks and observers #19018

Unanswered
gwafotapa asked this question in Q&A
May 1, 2025 · 1 comments · 7 replies
Discussion options

I'm curious about the order things run when both a hook and an observer are scheduled to run, assuming both run commands and that the command queue is not empty. I found out the order was:

hook, observer, hook's commands, observer's commands, command queue

I thought since hooks are designed to maintain invariants, their commands would either be pushed to the end of the queue (and their use discouraged in that case) or that they would execute right after the hook itself but the observer runs in between. Is this correct ?

You must be logged in to vote

Replies: 1 comment · 7 replies

Comment options

Since you have mutable world (DeferredWorld) access in hooks, you usually directly modify entities, components and resources, without using commands (which are deferred). If you are using commands in a hook and want to apply them before the observers are running, you may need to manually flush the command queue on the world.

EDIT: Also, notice that when we're talking about removing or de-spawning, the order of hooks & observers is not the same, observers run before the hooks.

You must be logged in to vote
7 replies
@gwafotapa
Comment options

No I was expecting the commands from the hooks to run before the observers themselves as in :
hooks, commands from hooks, observers, commands from observers

@Henauxg
Comment options

Henauxg May 7, 2025
Collaborator

Oh, I don't quite understand what you meant with pushed to the end of the queue then.
If you're interested, I found a past discussion on Discord about hooks & observers and why they are structured that way (with no immediate commands/mut world access). In short: allowing structural changes would cause many possibly undefined behaviors and would ensue a sizable performance loss/complexity to try and prevent/fix these.

@gwafotapa
Comment options

When a hook runs, the command queue might not be empty. When a command is encountered while running the hook, the standard behavior would have been to add that command at the end of the queue. Instead the choice has been made to make these commands jump to the front of the queue. That makes sense to me as you want all the hook logic to apply immediately. But I would have gone one step further and have these commands run before any observer as well. I was curious why this is not the case.

I'm not sure why but I can't access your link.

@Henauxg
Comment options

Henauxg May 7, 2025
Collaborator

Understood. The link leads to the Bevy Discord server, you can find an invitation in the Bevy Community page, you might not have a Discord account ?

@gwafotapa
Comment options

I was missing the invitation. Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.