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 an example demonstrating how to send and receive events in the same system #11574

Merged
merged 22 commits into from
Jan 29, 2024

Conversation

alice-i-cecile
Copy link
Member

Objective

  • Sending and receiving events of the same type in the same system is a reasonably common need, generally due to event filtering.
  • However, actually doing so is non-trivial, as the borrow checker simultaneous hates mutable and immutable access.

Solution

  • Demonstrate two sensible patterns for doing so.
  • Update the ManualEventReader docs to be more clear and link to this example.

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Examples An addition or correction to our examples labels Jan 28, 2024
Copy link
Contributor

The generated examples/README.md is out of sync with the example metadata in Cargo.toml or the example readme template. Please run cargo run -p build-templated-pages -- update examples to update it, and commit the file change.

1 similar comment
Copy link
Contributor

The generated examples/README.md is out of sync with the example metadata in Cargo.toml or the example readme template. Please run cargo run -p build-templated-pages -- update examples to update it, and commit the file change.

Co-authored-by: Joona Aalto <jondolf.dev@gmail.com>
Copy link
Member

@ickk ickk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useful example, no issues.

My comments are mostly nit-picky (take it or leave it) and maybe there's a slight bias to cut some of the asides to keep the example focused on teaching just the one thing well

crates/bevy_ecs/src/event.rs Outdated Show resolved Hide resolved
examples/ecs/send_and_receive_events.rs Outdated Show resolved Hide resolved
examples/ecs/send_and_receive_events.rs Outdated Show resolved Hide resolved
examples/ecs/send_and_receive_events.rs Outdated Show resolved Hide resolved
examples/ecs/send_and_receive_events.rs Outdated Show resolved Hide resolved
examples/ecs/send_and_receive_events.rs Outdated Show resolved Hide resolved
examples/ecs/send_and_receive_events.rs Outdated Show resolved Hide resolved
alice-i-cecile and others added 5 commits January 28, 2024 15:57
Co-authored-by: ickk <git@ickk.io>
Co-authored-by: Joona Aalto <jondolf.dev@gmail.com>
@alice-i-cecile
Copy link
Member Author

Amended, thanks a ton for the helpful reviews :)

alice-i-cecile and others added 2 commits January 28, 2024 16:09
Co-authored-by: Joona Aalto <jondolf.dev@gmail.com>
Co-authored-by: Joona Aalto <jondolf.dev@gmail.com>
Copy link
Contributor

@Jondolf Jondolf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable names in the doc example seem to be the last place with "emit" instead of "send" 😅 Nice and useful example!

Co-authored-by: Joona Aalto <jondolf.dev@gmail.com>
@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Jan 28, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jan 29, 2024
Merged via the queue into bevyengine:main with commit 149a313 Jan 29, 2024
26 of 27 checks passed
github-merge-queue bot pushed a commit that referenced this pull request Jan 30, 2024
# Objective

- Example `send_and_receive_events` added in #11574 panics
```
thread 'Compute Task Pool (3)' panicked at bevy/crates/bevy_ecs/src/system/system_param.rs:570:17:
Resource requested by send_and_receive_events::read_and_write_different_event_types does not exist: bevy_ecs::event::Events<send_and_receive_events::A>
Encountered a panic in system `send_and_receive_events::read_and_write_different_event_types`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!
```

## Solution

- Register the events used in the system
- Don't use logger as it's not setup with `MinimalPlugins`, just print

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: Kanabenki <lucien.menassol@gmail.com>
tjamaan pushed a commit to tjamaan/bevy that referenced this pull request Feb 6, 2024
…me system (bevyengine#11574)

# Objective

- Sending and receiving events of the same type in the same system is a
reasonably common need, generally due to event filtering.
- However, actually doing so is non-trivial, as the borrow checker
simultaneous hates mutable and immutable access.

## Solution

- Demonstrate two sensible patterns for doing so.
- Update the `ManualEventReader` docs to be more clear and link to this
example.

---------

Co-authored-by: Alice Cecile <alice.i.cecil@gmail.com>
Co-authored-by: Joona Aalto <jondolf.dev@gmail.com>
Co-authored-by: ickk <git@ickk.io>
tjamaan pushed a commit to tjamaan/bevy that referenced this pull request Feb 6, 2024
# Objective

- Example `send_and_receive_events` added in bevyengine#11574 panics
```
thread 'Compute Task Pool (3)' panicked at bevy/crates/bevy_ecs/src/system/system_param.rs:570:17:
Resource requested by send_and_receive_events::read_and_write_different_event_types does not exist: bevy_ecs::event::Events<send_and_receive_events::A>
Encountered a panic in system `send_and_receive_events::read_and_write_different_event_types`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!
```

## Solution

- Register the events used in the system
- Don't use logger as it's not setup with `MinimalPlugins`, just print

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: Kanabenki <lucien.menassol@gmail.com>
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-Examples An addition or correction to our examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants