Skip to content
Discussion options

You must be logged in to vote

I came across the same issues myself. I've solved this, plus the issue of code duplication, using events.

pub struct DamageEvent {
    pub damage: f32,
    pub target: Entity,
}

pub fn process_damage_events(
    mut events: EventReader<DamageEvent>,
    mut health_query: Query<&mut Health>,
) {
    for &DamageEvent { damage, target } in events.iter() {
        // If this entity has a Health component, damage it.
        if let Ok(mut health) = health_query.get_mut(target) {
            health -= damage;
        }
    }
}

fn contact_damage(
    mut events: EventWriter<DamageEvent>,
    query: Query<(&ContactDamage, &CollidingEntities)>,
) {
    for (contact_damage, collisions) in query.iter

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@meowitzher
Comment options

@tim-blackbird
Comment options

@meowitzher
Comment options

Answer selected by meowitzher
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants