rust - How to insert a Wireframe component into an entities component - Stack Overflow

admin2025-04-16  3

Using bevy 0.15

I'm trying to add a wireframe visualization to selected entities in Bevy. My system attempts to modify a _wireframe field in the Controllable component when an entity is selected, but I'm getting compilation errors due to reference mutability issues. Here's the problematic code:

struct Controllable {
    //root: SceneRoot,
    _mesh: MeshMaterial3d<StandardMaterial>,
    _collider: Collider,
    _transform: Transform,
    _rapier_pickable: RapierPickable,
    _wireframe: Option<Wireframe>,
}

spawning the entity:

let mut click_observer = Observer::new(on_click_controllable);
    let mut entity = commands
        .spawn((
            SceneRoot(asset_server.load(GltfAssetLabel::Scene(0).from_asset("guy.glb"))),
            Controllable {
                _mesh: MeshMaterial3d(debug_material.clone()),
                _collider: Collider::cuboid(2.5, 0.01, 2.5),
                _transform: Transform::from_xyz(0.0, 0.0, 3.0),
                _rapier_pickable: RapierPickable,
                _wireframe: None,
            },
        ))
        .id();
    commands.entity(entity).observe(
        move |trigger: Trigger<Pointer<Click>>, mut commands: Commands| {
            println!("before: {:?}", entity);
            if let Some(mut entity_commands) = commands.get_entity(entity) {
                entity_commands.insert(Selected);
                //insert selected components so that we can query for other
                //components of the entity when needed
                entity_commands.log_components();
            } else {
                // Handle the case where the entity no longer exists
                println!("Entity no longer exists!");
            };
        },
    );

Query:

fn wireframe_entities(mut query: Query<(&Selected, &Controllable)>, commands: Commands) {
    for e in query.iter() {
        if let (_, mut controllable) = e {
            controllable._wireframe = Some(Wireframe);
        }
    }
}

This runs but does not show the wireframe. The Selected component shows up on click, and the query also executes on the entity.

I think I am missing something with the overall architecture and I'm trying to do something I am not supposed to. How do I make a wireframe show up when I click on an entity. This should not be that hard??

I've tried to add it directly to the entity using EntityCommands but nothing shows up, I thibnk because I need to add it to Controllable because that is where the mesh is:

fn on_click_controllable(click: Trigger<Pointer<Click>>, mut commands: Commands) {
    //let id = click.entity().id();
    commands.entity(click.entity())
        .insert(Wireframe);

    println!("{} was clicked", click.entity());
}

I've tried getting the EntityEntryCommands but it only inserts the default Controllable and I can't pass in Wireframe.

Using bevy 0.15

I'm trying to add a wireframe visualization to selected entities in Bevy. My system attempts to modify a _wireframe field in the Controllable component when an entity is selected, but I'm getting compilation errors due to reference mutability issues. Here's the problematic code:

struct Controllable {
    //root: SceneRoot,
    _mesh: MeshMaterial3d<StandardMaterial>,
    _collider: Collider,
    _transform: Transform,
    _rapier_pickable: RapierPickable,
    _wireframe: Option<Wireframe>,
}

spawning the entity:

let mut click_observer = Observer::new(on_click_controllable);
    let mut entity = commands
        .spawn((
            SceneRoot(asset_server.load(GltfAssetLabel::Scene(0).from_asset("guy.glb"))),
            Controllable {
                _mesh: MeshMaterial3d(debug_material.clone()),
                _collider: Collider::cuboid(2.5, 0.01, 2.5),
                _transform: Transform::from_xyz(0.0, 0.0, 3.0),
                _rapier_pickable: RapierPickable,
                _wireframe: None,
            },
        ))
        .id();
    commands.entity(entity).observe(
        move |trigger: Trigger<Pointer<Click>>, mut commands: Commands| {
            println!("before: {:?}", entity);
            if let Some(mut entity_commands) = commands.get_entity(entity) {
                entity_commands.insert(Selected);
                //insert selected components so that we can query for other
                //components of the entity when needed
                entity_commands.log_components();
            } else {
                // Handle the case where the entity no longer exists
                println!("Entity no longer exists!");
            };
        },
    );

Query:

fn wireframe_entities(mut query: Query<(&Selected, &Controllable)>, commands: Commands) {
    for e in query.iter() {
        if let (_, mut controllable) = e {
            controllable._wireframe = Some(Wireframe);
        }
    }
}

This runs but does not show the wireframe. The Selected component shows up on click, and the query also executes on the entity.

I think I am missing something with the overall architecture and I'm trying to do something I am not supposed to. How do I make a wireframe show up when I click on an entity. This should not be that hard??

I've tried to add it directly to the entity using EntityCommands but nothing shows up, I thibnk because I need to add it to Controllable because that is where the mesh is:

fn on_click_controllable(click: Trigger<Pointer<Click>>, mut commands: Commands) {
    //let id = click.entity().id();
    commands.entity(click.entity())
        .insert(Wireframe);

    println!("{} was clicked", click.entity());
}

I've tried getting the EntityEntryCommands but it only inserts the default Controllable and I can't pass in Wireframe.

Share Improve this question edited Feb 2 at 20:07 Thomas asked Feb 2 at 20:02 ThomasThomas 437 bronze badges 4
  • This doesn't compile for several reasons, Controllable isn't a component, cannot assign to `controllable._wireframe`, which is behind a `&` reference, …, please add a minimal reproducible example. – cafce25 Commented Feb 2 at 21:24
  • What you've shown doesn't really make sense: if Controllable is a component then your entity does not have the components from it - queries won't be able to find those. If Controllable is a bundle then querying for it and modifying it shouldn't even compile. – kmdreko Commented Feb 3 at 1:04
  • How is Controllable not a component? Isn't that the reason I added #[derive(Component)]? @cafce25 – Thomas Commented Feb 3 at 3:19
  • @Thomas I don't see #[derive(Component)] anywhere in your question, are we supposed to guess what your actual code is? Or use a crystal ball? – cafce25 Commented Feb 3 at 8:08
Add a comment  | 

1 Answer 1

Reset to default 0

I am a new bevy user too, but I think I might have found the problem, the reference to the controller has to be mutable in the query. Here is the updated code:

fn wireframe_entities(mut query: Query<(&Selected, &mut Controllable)>, commands: Commands) {                                       //^^^
    for e in query.iter() {
        if let (_, mut controllable) = e {
            controllable._wireframe = Some(Wireframe);
        }
    }
}

I believe this will allow the reference to be mutable and fix the error.

转载请注明原文地址:http://www.anycun.com/QandA/1744788459a87635.html