#Bundle components conflicts

3 messages · Page 1 of 1 (latest)

primal summit
#

Hi everyone!
I'm trying to figure out how to spawn enemies in my game so far this is my early idea:

use crate::prelude::*;

pub(super) fn plugin(app: &mut App) {
    app.add_systems(Startup, place_enemy_debug);
}

#[derive(Bundle)]
pub struct EnemyBundle {
    pub speed: Speed,
    pub health: Health,
    pub sprite_bundle: SpriteBundle,
    pub rigid_body: RigidBody,
    pub collider: Collider,
    pub controler: KinematicCharacterController,
    pub enemy_marker: Enemy,
}

impl Default for EnemyBundle {
    fn default() -> Self {
        Self {
            health: Health(100.0),
            speed: Speed(400.0),
            sprite_bundle: SpriteBundle {
                transform: Transform::from_xyz(0.0, 200.0, 0.0),
                sprite: Sprite {
                    custom_size: Some(Vec2::new(50.0, 60.0)),
                    ..Default::default()
                },
                ..default()
            },
            rigid_body: RigidBody::KinematicPositionBased,
            collider: Collider::ball(10.0 / 2.0),
            controler: KinematicCharacterController::default(),
            enemy_marker: Enemy,
        }
    }
}

fn spawn_default_enemy(commands: &mut Commands) {
    commands.spawn(EnemyBundle {
        ..Default::default()
    });
}

fn place_enemy_debug(mut commands: Commands) {
    spawn_default_enemy(&mut commands);
}

My goal is to create a basic enemy blueprint that I can customize in different functions like big_enemy, default_enemy, etc.

However, when I try to add a TransformBundle to the EnemyBundle, the game crashes, saying there are duplicate components. This made me question whether my approach is correct. If I wanted to add multiple components that include Transform, how should I go about it?

I came across this design document and I'm wondering if it's a better approach. I initially avoided it because the Bevy docs mention that RunSystemOnce isn't efficient and is more for testing or diagnostics.

kind raven
#

You cannot have multiple of the same component on one entity, but if you want you can override the transform which you already set by doing something like this:

    commands.spawn(EnemyBundle {
        ..Default::default()
    }).insert(someTransform);