-
Here is my minimal code: use bevy::prelude::*;
use bevy_mod_picking::prelude::*;
fn main() {
let mut app = App::new();
app.add_plugins(DefaultPlugins);
app.add_systems(Startup, setup);
app.add_plugins(DefaultPickingPlugins);
app.run();
}
#[derive(Component)]
struct Cube {
// The private member
click_counter: u32,
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// Cube
commands.spawn((
PbrBundle {
mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
material: materials.add(Color::srgb_u8(124, 144, 255)),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default()
},
Cube { click_counter: 0 },
On::<Pointer<Click>>::target_component_mut::<Cube>(|event, cube| {
// Why do I have access to a private member here?
cube.click_counter += 1;
println!("Clicked on {:?} times {}", event.target(), cube.click_counter);
}),
));
// Light
commands.spawn(PointLightBundle {
point_light: PointLight {
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
// Camera
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
} I should not have access to this member as it is private. I don't know if it is due to the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
from https://doc.rust-lang.org/reference/visibility-and-privacy.html
private is not private to the current module, you should put it in another if you want it to fail compilation |
Beta Was this translation helpful? Give feedback.
-
Literally speaking, if you replace
with mod Private {
use bevy::prelude::*;
#[derive(Component)]
pub struct Cube {
// The private member
click_counter: u32,
}
}
use Private::Cube; you would get errors like:
|
Beta Was this translation helpful? Give feedback.
-
I just lean something on rust, didn't know the visibility was at the module level. thank you ! |
Beta Was this translation helpful? Give feedback.
from https://doc.rust-lang.org/reference/visibility-and-privacy.html
private is not private to the current module, you should put it in another if you want it to fail compilation