bevyengine / bevyengine/bevy

Add a Command to trigger observers with statically known components

Open
#14,775 0 comments 1 reaction 0 assignees View on GitHub
A-ECS C-Usability D-Modest S-Ready-For-Implementation
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

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

Triggering `Observer`s 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 `ComponentId`s in a `Resource` and grabbing them as needed from it.

## What solution would you like?

```rust
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(&mut self, event: impl Event, targets: impl TriggerTargets);
}
```

## What alternative(s) have you considered?

My current situation:

```rust
fn system(commands: Commands, actions: Res) {
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::(),
idle: world.init_component::(),
}
}
}

struct TargetedAction(Entity, ComponentId);

impl TriggerTargets for TargetedAction {
fn components(&self) -> impl ExactSizeIterator {
std::iter::once(self.1)
}
fn entities(&self) -> impl ExactSizeIterator {
std::iter::once(self.0)
}
}
```

## Additional context

Originally brought up [on discord](https://discord.com/channels/691052431525675048/749335865876021248/1273836049696817163).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.