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

Support .run_if() on observers #14195

Open
forbjok opened this issue Jul 6, 2024 · 4 comments
Open

Support .run_if() on observers #14195

forbjok opened this issue Jul 6, 2024 · 4 comments
Labels
A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible D-Complex Quite challenging from either a design or technical perspective. Ask for help!

Comments

@forbjok
Copy link
Contributor

forbjok commented Jul 6, 2024

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.

@forbjok forbjok added C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels Jul 6, 2024
@iiYese iiYese added A-ECS Entities, components, systems, and events D-Complex Quite challenging from either a design or technical perspective. Ask for help! and removed S-Needs-Triage This issue needs to be labelled labels Jul 6, 2024
@iiYese
Copy link
Contributor

iiYese commented Jul 6, 2024

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
Copy link
Contributor Author

forbjok commented Jul 6, 2024

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
Copy link
Contributor

benfrankel commented Jul 8, 2024

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
Copy link
Contributor

benfrankel commented Jul 8, 2024

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: #12660 (comment).

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-Complex Quite challenging from either a design or technical perspective. Ask for help!
Projects
None yet
Development

No branches or pull requests

3 participants