-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expect<&T>
for "safe" queries
#11958
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
Comments
Hmm, I'm not totally sold.
This hasn't been my experience. Increasingly, on more complex Bevy projects I've found that I've been programming really defensively, and eliminating panics wherever possible, typically logging a warning or error. To detect surprising archetype issues, I'll write my own simple logging systems that will be feature flagged, which go and check and log for these sorts of problems. Given that this is just sugar for a controversial pattern, I think it's likely a better fit for a 3rd party crate (but do let us know if you need any upstream changes to support it). |
Sure, I could try to implement this as a crate. I'd also be open to Edit: Granted, then the issue would be "what to return?" If it's an error, it'd impose a |
Just wanted to say that even if this doesn't end up being implemented, you can do it yourself on your side (in your game):
|
You may actually be able to get away with a |
Minimal implementation of this: In my opinion, the feature is too small to warrant a crate. While I understand the advantage of defensive programming, I also don't really see the harm in providing this as a tool to be used as needed. Users would have to opt into the feature consciously. We could also turn this from a panic into an error without having to worry about constructing/inserting a missing component. fn matches_component_set(
_state: &T::State,
set_contains_id: &impl Fn(ComponentId) -> bool,
) -> bool {
if set_contains_id(state) {
return true;
}
error!(...); // <--- Graceful error
false
} |
Yep,
The cost isn't in users using it: it's about the cost to learn and explore Bevy, and the ongoing maintenance costs. As an ECS SME, I'm opposed: I think this can and should be done externally. |
Perhaps the better option would be to include this as a part of a broader |
Related to #1481 ? |
What problem does this solve or what need does it fill?
Since I've started using Bevy, a problem I consistently run into is that systems which iterate over a query can silently "skip" over entities which unintentionally, or erroneously do not match the query. This often leads to subtle bugs that are hard to debug.
For example, consider the system below:
In such a scenario, this system expects all entities with
A
to also have aB
, which is kinda enforced by using anFooBundle
. However, ifA
orB
is removed for whatever reason, this system would now skip over the invalid entities; when in reality I would like a panic or an error to catch the invalid entity as early as possible.One workaround I've found to solve this issue is to use
Option<T>
:Another way to achieve the same result is to split the query:
What solution would you like?
I would like a more ergonomic implementation of the workaround I've mentioned above.
I imagine this could take the form of
Expect<&T>
, such that:Internally, this could behave like an
Option<&T>
query, but instead of its output being anOption<&T>
, it would be&T
, and it would panic if internally it isNone
.What alternative(s) have you considered?
Workaround described above.
If there are already ways to achieve what I want, I'm unaware of them!
Additional context
I think we could further extend this terminology to make query
get
more ergonomic as well.Instead of:
We could have:
I would also be open to implementing this with a PR; but I wanted to raise the idea to community before I start implementing.
The text was updated successfully, but these errors were encountered: