Skip to content

Instantly share code, notes, and snippets.

@TheNeikos
Forked from mcpar-land/main.rs
Last active September 7, 2020 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheNeikos/b2a039135126061063c0393a5061fdd1 to your computer and use it in GitHub Desktop.
Save TheNeikos/b2a039135126061063c0393a5061fdd1 to your computer and use it in GitHub Desktop.
Bevy Ui Crash
use bevy::prelude::*;
pub struct GlobalChildRef(pub Entity);
pub struct GlobalChild;
fn setup(
mut commands: Commands,
mut materials: ResMut<Assets<ColorMaterial>>,
global_child: ResMut<GlobalChildRef>,
) {
// the code below crashes.
commands
.spawn(UiCameraComponents::default())
.spawn(NodeComponents::default())
.with_children(|parent| {
parent
.spawn_as_entity(
global_child.0,
NodeComponents {
style: Style {
size: Size::new(Val::Px(40.0), Val::Px(40.0)),
position: Rect {
top: Val::Px(100.0),
left: Val::Px(100.0),
..Default::default()
},
..Default::default()
},
material: materials.add(ColorMaterial::color(Color::RED)),
..Default::default()
},
)
.with(GlobalChild);
});
}
/// Move the child entity from 100, 100 to 30, 30
fn move_child_entity_around(
global_child_ref: Res<GlobalChildRef>,
global_child_query: Query<(Entity, &GlobalChild, &mut Style)>,
) {
let global_child_entity = global_child_ref.0;
let mut global_child_style = global_child_query.get_mut::<Style>(global_child_entity).unwrap();
global_child_style.position = Rect {
top: Val::Px(30.0),
left: Val::Px(30.0),
..Default::default()
};
}
fn main() {
App::build()
.add_resource(GlobalChildRef(Entity::new()))
.add_default_plugins()
.add_startup_system(setup.system())
.add_system(move_child_entity_around.system())
.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment