Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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,
(
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();
}
@mcpar-land

This comment has been minimized.

Copy link
Owner Author

@mcpar-land mcpar-land commented Sep 7, 2020

Unable to figure out why this crashes.

thread '<unnamed>' panicked at 'called `Option::unwrap()` on a `None` value', C:\Users\username\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ui-0.1.3\src\flex\mod.rs:86:66
@mcpar-land

This comment has been minimized.

Copy link
Owner Author

@mcpar-land mcpar-land commented Sep 7, 2020

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