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.