#Why adding Text2dBundle next to Bundle with Mesh panics in Bevy 0.14?

4 messages · Page 1 of 1 (latest)

queen kayak
#
pub fn create_industry(
    commands: &mut Commands,
    meshes: &mut ResMut<Assets<Mesh>>,
    materials: &mut ResMut<Assets<ColorMaterial>>,
    industry: &IndustryTypes,
    amount: u32,
    god: Entity,
) {
    commands.spawn((
        MaterialMesh2dBundle {
            mesh: Mesh2dHandle(meshes.add(Annulus::new(15.0, 30.0))),
            material: materials.add(Color::hsl(1.0, 0.95, 0.7)),
            transform: Transform::from_xyz(0.0, 0.0, 0.0),
            ..default()
        },
        Text2dBundle {
            text: Text::from_section("Copper Mine", TextStyle::default()),
            ..default()
        },
    ));
Encountered a panic when applying buffers for system `civilizace::generate::antropocene::industry_created`!
2024-07-05T22:00:43.198320Z  WARN bevy_ecs::world::command_queue: CommandQueue has un-applied commands being dropped.
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!
queen kayak
#

Hmmm.. I guess I got it.. I should move text as child..

commands
        .spawn((MaterialMesh2dBundle {
            mesh: Mesh2dHandle(meshes.add(Annulus::new(15.0, 30.0))),
            material: materials.add(Color::hsl(1.0, 0.95, 0.7)),
            transform: Transform::from_xyz(0.0, 0.0, 0.0),
            ..default()
        },))
        .with_children(|parent| {
            parent.spawn(Text2dBundle {
                text: Text::from_section("Copper Mine", TextStyle::default()),
                ..default()
            });
        });
#

but why?

sullen fossil
#

In your first code block, you're attempting to spawn a single entity. A single entity can't be both a 2d mesh and a 2d text.

Bundles are not allowed to have duplicate components. (BundleA, BundleB) is a way of composing a new bundle from the components of the members of that tuple.

MaterialMesh2dBundle and Text2dBundle both contain a variety of components that they need in order to work properly. That includes a set of components that both bundles have that need to be unique: Transform, GlobalTransform, Visibility, InheritedVisibility, ViewVisibility.

You would see the same sort of error message if you tried to do: commands.spawn((Transform::default(), Transform::default())), because multiple of the same component can't make sense. That's effectively what you're doing in the first code block.