#Querying Entities Without Multiple Components in Bevy ECS

13 messages · Page 1 of 1 (latest)

broken flower
#

How can I configure a Bevy query to select entities that neither have Root nor FrameCompNode simultaneously, but still consider those with only one of these components?

#
Query<
        ...,
        (
            With<CompNode>,
            Without<Root>, // Need to exclude only entities with both Root and FrameCompNode
            Without<Selected>,
            Without<Locked>,
        ),
    >

Logs

INFO: [log_entity_components] Entity (5v1) has the following components:
 - dyn_comp_bundles::components::nodes::CompNode
 - dyn_comp_bundles::components::nodes::FrameCompNode
 - bevy_transform::components::transform::Transform
 - bevy_transform::components::global_transform::GlobalTransform
 - dyn_comp_bundles::components::mixins::SizeMixin
 - dyn_comp_bundles::components::mixins::CornerRadiiMixin
 - dyn_comp_bundles::components::mixins::VisibilityMixin
 - dyn_comp_bundles::components::mixins::BlendModeMixin
 - dyn_comp_bundles::components::mixins::OpacityMixin
 - dyn_comp_bundles::components::mixins::StyleChildrenMixin
 - bevy_hierarchy::components::parent::Parent
 - bevy_hierarchy::components::children::Children
 - dyn_comp_bundles::components::mixins::HierarchyLevel
 - dyn_comp_bundles::components::mixins::PathMixin
 - dyn_comp_svg_builder::svg::svg_bundle::SvgBundleVariant
index.js:428 INFO: [log_entity_components] Entity (7v1) has the following components:
 - dyn_comp_bundles::components::nodes::CompNode
 - bevy_transform::components::transform::Transform
 - bevy_transform::components::global_transform::GlobalTransform
 - dyn_comp_bundles::components::mixins::SizeMixin
 - dyn_comp_bundles::components::mixins::CornerRadiiMixin
 - dyn_comp_bundles::components::mixins::VisibilityMixin
 - dyn_comp_bundles::components::mixins::BlendModeMixin
 - dyn_comp_bundles::components::mixins::OpacityMixin
 - dyn_comp_bundles::components::mixins::StyleChildrenMixin
 - dyn_comp_bundles::components::nodes::RectangleCompNode
 - bevy_hierarchy::components::parent::Parent
 - dyn_comp_bundles::components::mixins::HierarchyLevel
 - dyn_comp_bundles::components::mixins::PathMixin
 - dyn_comp_svg_builder::svg::svg_bundle::SvgBundleVariant
#

Currently, adding Without<FrameCompNode> to the tuple (which should be AND?) excludes all entities with FrameCompNode, which isn't the intended behavior. How can I adjust this?

Query<
        ...,
        (
            With<CompNode>,
            Without<Root>, 
            Without<FrameCompNode>, // Now no FrameCompNode is queried anymore no matter whether it has Root or no Root component
            Without<Selected>,
            Without<Locked>,
        ),
    >

Logs

INFO: [log_entity_components] Entity (7v1) has the following components:
 - dyn_comp_bundles::components::nodes::CompNode
 - bevy_transform::components::transform::Transform
 - bevy_transform::components::global_transform::GlobalTransform
 - dyn_comp_bundles::components::mixins::SizeMixin
 - dyn_comp_bundles::components::mixins::CornerRadiiMixin
 - dyn_comp_bundles::components::mixins::VisibilityMixin
 - dyn_comp_bundles::components::mixins::BlendModeMixin
 - dyn_comp_bundles::components::mixins::OpacityMixin
 - dyn_comp_bundles::components::mixins::StyleChildrenMixin
 - dyn_comp_bundles::components::nodes::RectangleCompNode
 - bevy_hierarchy::components::parent::Parent
 - dyn_comp_bundles::components::mixins::HierarchyLevel
 - dyn_comp_bundles::components::mixins::PathMixin
 - dyn_comp_svg_builder::svg::svg_bundle::SvgBundleVariant
cunning shuttle
misty whale
#

i actually think AND is what you need here.

Query<..., (Without<Root>, Without<FrameCompNode>)> means only entities where both filters apply are filtered out

#

AND in filters is a tuple of filters

cunning shuttle
#

Wouldn't it be something like Or<(With<Root>, Without<FrameCompNode>), (Without<Root>, With<FrameCompNode>)>?

misty whale
cunning shuttle
#

I think the Or query should achieve that 🤔

broken flower
#

an and should've solved it but it didn't work for me as you can see in my second example,
where the 5v1 entity isn't listed in the logs anymore although it is not Root Node

echo zenith
broken flower
#

Yeah it works.. Thanks for the hint 🙂
But an and in some way would be ofc more streamlined I guess

 Or<(
                (Without<Root>, With<FrameCompNode>),
                (With<Root>, Without<FrameCompNode>),
                (Without<Root>, Without<FrameCompNode>),
            )>, // Exclude root frame nodes
echo zenith