#Issue with Mutable Access in Bevy's ECS

5 messages · Page 1 of 1 (latest)

astral magnet
#

Hello everyone,
I'm encountering a runtime error in my Bevy project related to mutable access of a component (DimensionMixin). The issue seems to arise when using &mut DimensionMixin in a query within my update_paint_dimension_based_on_parent_node system. Below is the code for reference:

pub fn update_paint_dimension_based_on_parent_node(
    mut commands: Commands,
    node_children_query: Query<
        (Entity, &DimensionMixin, &Children),
        (With<Node>, Changed<DimensionMixin>),
    >,
    // mut paint_with_dimension_query: Query<(Entity, &Parent, &mut DimensionMixin), With<Paint>>,
    paint_without_dimension_query: Query<(Entity, &Parent), (With<Paint>, Without<DimensionMixin>)>,
) {
    for (node_entity, dimension, children) in node_children_query.iter() {
        // Update existing DimensionMixin for children with Paint and DimensionMixin
        // for (paint_entity, parent, mut dimension_mixin) in paint_with_dimension_query.iter_mut() {
        //     if children.contains(&paint_entity) && parent.get() == node_entity {
        //         dimension_mixin.width = dimension.width;
        //         dimension_mixin.height = dimension.height;
        //     }
        // }

        // Add DimensionMixin for children with Paint but without DimensionMixin
        for (paint_entity, parent) in paint_without_dimension_query.iter() {
            if children.contains(&paint_entity) && parent.get() == node_entity {
                commands.entity(paint_entity).insert(DimensionMixin {
                    width: dimension.width,
                    height: dimension.height,
                });
            }
        }
    }
}
#

The error I'm getting is Uncaught RuntimeError: unreachable. This occurs when the system runs, and it seems to be linked to the mutable query for DimensionMixin. As if I comment out the Query referencing &mut DimensionMixin it does not panic.
I suspect it might be a conflict with ECS's rules on mutable references, but I'm not entirely sure.

Has anyone faced a similar issue or can provide insights into what might be going wrong here?
Thanks 🙂

slow walrus
#

Yeah, a normal problem in this area. In rust you can only have one single mutable reference or multiple immutable references. Bevy wants to assert that no problems can happen in this aspect, so it warns you about this conflict. With<Node> and With<Paint>, as far as bevy is concerned, can conflict

#

One way of solving this problem is adding somewhere in the two queries
Without<Paint/Node>
That will solve this error

bright anvil