-
Hello, i try to have pbrbundles or scenebundles show/hide on keypress. When i set the initial visibility on setup, it works. But not when i try to react to a keypress. im using the following plugins on 0.10.1: bevy_kira_audio, bevy_mod_picking, bevy_rapier3d/bevy_fps_controller, It doesnt matter if i try to change the visibility of the simple cube or of a gltf scene, it doesnt change. I can see on the console that the keyboard_input function works as it should, writing the correct scene names and such. My guess would be that i am not really mutating the visibility components as i think i am or that i still need to call some kind of update function in bevy? Also, every example seems to stem from the old struct-based Visibility with .is_visible, which doesnt seem to exist anymore in 0.10.x What am i doing wrong?: #[derive(Component)]
pub struct SceneName(String);
...
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
...
commands.spawn((
PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::WHITE.into()),
visibility: Visibility::Hidden,
..Default::default()
},
PickableBundle::default(),
RaycastPickTarget::default(),
OnPointer::<Click>::run_callback(move_hit_object),
));
let pistol_model = asset_server.load("models/simple_pistol2.glb#Scene0");
commands.spawn((
SceneBundle {
scene: pistol_model.into(),
transform: Transform::from_translation(Vec3{x: 0.0, y: 0.0, z: -2.0}),
visibility: Visibility::Hidden,
..default()
},
SceneName(String::from("pistol")),
MovedScene
));
commands.spawn((
SceneBundle {
scene: asset_server.load("models/untitled.glb#Scene0"),
transform: Transform::from_translation(Vec3 { x: 0.0, y: 0.0, z: -2.0 }),
visibility: Visibility::Hidden,
..default()
},
SceneName(String::from("wrench")),
MovedScene
));
...
}
input.rs:
pub fn keyboard_input(
keys: Res<Input<KeyCode>>,
entities: Query<Entity, With<SceneName>>,
scene_names: Query<&SceneName>,
mut visibilities: Query<&Visibility>,
) {
for entity in entities.iter() {
let scene_name = scene_names.get(entity).unwrap();
let mut visibility = visibilities.get_mut(entity).unwrap();
if keys.just_pressed(KeyCode::Key1) {
println!("Pistol");
if scene_name.0 == "pistol" {
println!("scene name is pistol: {}", scene_name.0);
visibility = &Visible;
}
else {
println!("scene name: {}", scene_name.0);
visibility = &Visible;
}
}
if keys.just_pressed(KeyCode::Key2) {
println!("Wrench");
if scene_name.0 == "wrench" {
println!("scene name is wrench: {}", scene_name.0);
visibility = &Visible;
}
else {
println!("scene name: {}", scene_name.0);
visibility = &Visible;
}
}
}
}
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I think you're right, the thing isn't getting mutated. This: Should be more like this: Then when you assign I'd expect something like this: I probably got some stuff wrong, didn't read the above code too closely, so hopefully this helps. |
Beta Was this translation helpful? Give feedback.
I think you're right, the thing isn't getting mutated.
This:
mut visibilities: Query<&Visibility>,
Should be more like this:
mut visibilities: Query<&mut Visibility>,
Then when you assign I'd expect something like this:
*visibility = Visibility::Inherited; // or Visibile depending on how you use it
I probably got some stuff wrong, didn't read the above code too closely, so hopefully this helps.