You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue is about the discussion of a potential new query type OneOf.
I am willing to create the pull request for this feature if it's desired, I'm creating an issue first to gauge whether or not this would be suitable for hecs.
Motivation
I can't find a built-in way to query one of a set of types, the closest is the Or query type but this has certain limitations as discussed below. The motivation for OneOf would be to implement different behaviour when the player interacts with some entity, i.e. clicking an Enemy/Item/Corpse/etc.
Example
The query type OneOf3<A, B, C> will match any entity that contains either A, B or C, but only up to the first match.
You can then use code similar on the result:
match item {One(a) => {},Two(b) => {},Three(c) => {},}
edit: An alternative implementation would be to implement #[derive(Query)] for enum types as well, allowing users to define their own enum queries like:
Lots of boilerplate to set up the query with the repeated or and brackets
Not very easy to concisely match on the result
The query engine will continue checking for B and C even if A is present, i.e. it doesn't stop when you've already matched
Using a lot of options, like (Option<A>, Option<B>, Option<C>)
Problems:
Code using the query result will end up having a lot of if-else chains to check every option
3 Nones is a valid result, meaning you're going to end up iterating over every entity unless you constrain it with a more complex query eg. With<(Option<A>, Option<B>, Option<C>), Or<A, Or<B, C>>
Some other workaround, like inserting an Attack component when an entity is clicked
Problems:
Doesn't work for every use-case
Performance penalty due to changing archetype
More architecturally complex than just performing the logic you want directly
I apologize in advance if there's built-in a way to do OneOf. Once again, I am willing to contribute this feature if it's within the scope of hecs. Thank you for your time
The text was updated successfully, but these errors were encountered:
Hello, hope you are doing well.
This issue is about the discussion of a potential new query type OneOf.
I am willing to create the pull request for this feature if it's desired, I'm creating an issue first to gauge whether or not this would be suitable for hecs.
Motivation
I can't find a built-in way to query one of a set of types, the closest is the Or query type but this has certain limitations as discussed below. The motivation for OneOf would be to implement different behaviour when the player interacts with some entity, i.e. clicking an Enemy/Item/Corpse/etc.
Example
The query type
OneOf3<A, B, C>
will match any entity that contains either A, B or C, but only up to the first match.You can then use code similar on the result:
edit: An alternative implementation would be to implement
#[derive(Query)]
for enum types as well, allowing users to define their own enum queries like:I think this approach might be superior.
Alternatives that already exist (to my knowledge)
Layering
Or
s, likeOr<A, Or<B, C>>
Problems:
Using a lot of options, like
(Option<A>, Option<B>, Option<C>)
Problems:
None
s is a valid result, meaning you're going to end up iterating over every entity unless you constrain it with a more complex query eg.With<(Option<A>, Option<B>, Option<C>), Or<A, Or<B, C>>
Some other workaround, like inserting an Attack component when an entity is clicked
Problems:
I apologize in advance if there's built-in a way to do OneOf. Once again, I am willing to contribute this feature if it's within the scope of hecs. Thank you for your time
The text was updated successfully, but these errors were encountered: