Created
September 7, 2020 16:47
-
-
Save mcpar-land/1a1f0ff648766b22a016b7de4b139ee6 to your computer and use it in GitHub Desktop.
Bevy Ui Crash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
( | |
GlobalChild, | |
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() | |
}, | |
), | |
); | |
}); | |
// the code below does NOT crash, but the node does not appear. | |
// commands | |
// .spawn(UiCameraComponents::default()) | |
// .spawn(NodeComponents::default()) | |
// .spawn_as_entity( | |
// global_child.0, | |
// ( | |
// GlobalChild, | |
// 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() | |
// }, | |
// ), | |
// ); | |
} | |
/// 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 NodeComponents))>, | |
) { | |
let global_child_entity = global_child_ref.0; | |
let mut global_child_one = | |
global_child_query.entity(global_child_entity).unwrap(); | |
let (_, (_, mut global_child_node)) = global_child_one.get().unwrap(); | |
global_child_node.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(); | |
} |
This variant causes a different error.
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<With<GlobalChild, (Entity, &mut NodeComponents)>>,
) {
let global_child_entity = global_child_ref.0;
let mut global_child_one =
global_child_query.entity(global_child_entity).unwrap();
let (_, mut global_child_node) = global_child_one.get().unwrap();
global_child_node.style.position = Rect {
top: Val::Px(30.0),
left: Val::Px(30.0),
..Default::default()
};
}
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: CannotReadArchetype', src\main.rs:70:56
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unable to figure out why this crashes.