bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
36.25k stars 3.58k forks source link

Support .run_if() on observers #14195

Open forbjok opened 4 months ago

forbjok commented 4 months ago

What problem does this solve or what need does it fill?

Just like with systems, sometimes observers are only relevant for a specific state. It would be much more convenient to be able to do this on observers using .run_if() or something similar, than having to manually check the state in the function code.

What solution would you like?

To be able to use .run_if() on observers to limit when they will run, just like on regular systems.

iiYese commented 4 months ago

Just forwarding my thoughts from discord

On run conditions for observers specifically they don't make sense & add a lot of complexity. This came up before in #ecs-dev. Run conditions are for things in schedules. IMO this is a problem with how bevys systems are designed. They always run regardless of if their params match. In flecs what determines if an observer runs is the observers query (queries in flecs aren't limited to iterating entities & matching overlapping archetypes). Like right now if a system asks for Res<Foo> where Foo doesn't exist it panics. If the behavior was instead that the system doesn't run then observers checking for state would just be InState<Menu>. Bevy's type system limitations are also showing here because doing this requires more const generics which would be too limiting anyway cause it would have zero dynamic capabilities.

forbjok commented 4 months ago

Like right now if a system asks for Res where Foo doesn't exist it panics.

This is one thing that makes the lack of support for run conditions on observers even more annoying. I just ran into this as well, where when I added the manual state check to the observer function, triggering it would cause a crash because a resource that would only exist in the specific state that observer was relevant in, correctly and as expected did not exist. To work around that I also had to wrap it in an option and use a let-else statement to unwrap it or return.

benfrankel commented 4 months ago

My take:

Run conditions are good for composition. You write a run condition once and then you can apply it to whatever system you want. But not to observers atm.

So you couldn't use a run condition like in_state(Foo), you'd have to add Res<State<Foo>> as an argument to your observer and check in there. For more complex run conditions, especially run conditions provided by 3rd-party libraries, this can become very annoying.

I suspect that end-users of observers will keep meeting the lack of run condition support with disappointment.

benfrankel commented 4 months ago

I do think that systems not running if one of their required resources doesn't exist would be a good thing btw, I've brought it up before: https://github.com/bevyengine/bevy/issues/12660#issuecomment-2016641765.