Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Command to trigger observers with statically known components #14775

Open
ItsDoot opened this issue Aug 16, 2024 · 0 comments
Open

Add a Command to trigger observers with statically known components #14775

ItsDoot opened this issue Aug 16, 2024 · 0 comments
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!

Comments

@ItsDoot
Copy link
Contributor

ItsDoot commented Aug 16, 2024

What problem does this solve or what need does it fill?

Triggering Observers with specific components currently requires you to know the component's ComponentId, even if the component type is statically known. This generally results in needing to store all necessary ComponentIds in a Resource and grabbing them as needed from it.

What solution would you like?

impl Commands {
    // I would expect the Bundle's component IDs to be chained with the ones provided with the TriggerTargets
    pub fn trigger_targets_with<B: Bundle>(&mut self, event: impl Event, targets: impl TriggerTargets);
}

What alternative(s) have you considered?

My current situation:

fn system(commands: Commands, actions: Res<ActionIds>) {
    let entity = Entity::PLACEHOLDER;
    commands.trigger_targets(ActionFinished(actions.drink), TargetedAction(entity, actions.drink));
}

#[derive(Resource)]
pub struct ActionIds {
    drink: ComponentId,
    idle: ComponentId,
}

impl FromWorld for ActionIds {
    fn from_world(world: &mut World) -> Self {
        Self {
            drink: world.init_component::<Drink>(),
            idle: world.init_component::<Idle>(),
        }
    }
}

struct TargetedAction(Entity, ComponentId);

impl TriggerTargets for TargetedAction {
    fn components(&self) -> impl ExactSizeIterator<Item = ComponentId> {
        std::iter::once(self.1)
    }
    fn entities(&self) -> impl ExactSizeIterator<Item = Entity> {
        std::iter::once(self.0)
    }
}

Additional context

Originally brought up on discord.

@ItsDoot ItsDoot added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it! D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes labels Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!
Projects
None yet
Development

No branches or pull requests

1 participant