bevy
bevy copied to clipboard
window::Cursor does not support ..default() initialization pattern
Bevy version
0.10.0
What you did
use bevy::{
prelude::*,
window::{Cursor, CursorGrabMode, PresentMode, WindowMode},
};
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Title".to_string(),
present_mode: PresentMode::AutoNoVsync,
mode: WindowMode::BorderlessFullscreen,
cursor: Cursor {
visible: false,
grab_mode: CursorGrabMode::Confined,
..default()
},
..default()
}),
..default()
}))
}
.run();
}
What went wrong
This section errors because Cursor.physical_position is private even though everything else is public. This makes the ..default()
initialization pattern not work because it can't set the physical_position field.
cursor: Cursor {
visible: false,
grab_mode: CursorGrabMode::Confined,
..default()
},
Additional information
Workaround:
cursor: {
let mut cursor = Cursor::default();
cursor.visible = false;
cursor.grab_mode = CursorGrabMode::Confined;
cursor
},
This workaround is quite compact and just fine honestly, though the current behavior is a bit unexpected considering how many other struct initializations work for Bevy users.
Checking the blame for the relevant line shows this field was intentionally made private to fix another issue, so it does not seem to be a trivial fix unfortunately.
- https://github.com/bevyengine/bevy/pull/7297