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

Make the Commands::add docs more representative of actual use cases #4737

Closed
alice-i-cecile opened this issue May 13, 2022 · 2 comments
Closed
Labels
A-ECS Entities, components, systems, and events C-Docs An addition or correction to our documentation D-Trivial Nice and easy! A great choice to get started with Bevy

Comments

@alice-i-cecile
Copy link
Member

How can Bevy's documentation be improved?

The current docs show a contrived example: in real use cases this would be commands.insert_bundle.

Replace this example with two examples:

  1. A custom command.
  2. Using a closure that operates on &mut World to perform a simple task.
@alice-i-cecile alice-i-cecile added C-Docs An addition or correction to our documentation D-Trivial Nice and easy! A great choice to get started with Bevy A-ECS Entities, components, systems, and events labels May 13, 2022
@tigregalis
Copy link
Contributor

What do you mean by the second example of using a closure?

Here's my attempt at the first. This can probably be cut down, maybe removing the need for an app and hiding some of the implementation, but I'm not too familiar with the API, so was hoping for some guidance (then I'll submit a PR). The key thing I wanted to demonstrate was the ability to concurrently submit data, then write to the world using that data at the end of the stage.

use bevy::{prelude::*, utils::HashMap, ecs::system::Command};

/// A custom `Command` for sending (deferred) vote counts concurrently from multiple `System`s
#[derive(Debug)]
struct SendVoteCount {
    candidate: String,
    count: usize,
}

/// A `Resource` which will aggregate the vote counts when `Commands` are flushed at the end of the `Stage`
#[derive(Default)]
struct Votes(HashMap<String, usize>);

impl Command for SendVoteCount {
    fn write(self, world: &mut World) {
        let mut counts = world.get_resource_or_insert_with(Votes::default);
        counts.0.entry(self.candidate).and_modify(|e| *e += self.count).or_insert(self.count);
    }
}

fn count_votes_melbourne(mut commands: Commands) {
    commands.add(SendVoteCount {
        candidate: "Albo".into(),
        count: 9001,
    });
    commands.add(SendVoteCount {
        candidate: "Scomo".into(),
        count: 4242,
    });
}

fn count_votes_brisbane(mut commands: Commands) {
    commands.add(SendVoteCount {
        candidate: "Clive".into(),
        count: 3,
    });
    commands.add(SendVoteCount {
        candidate: "Scomo".into(),
        count: 1997,
    });
    commands.add(SendVoteCount {
        candidate: "Albo".into(),
        count: 203,
    });
}

fn display_count(votes: Res<Votes>) {
    println!("{:?}", votes.0);
}

fn main() {
    App::new()
        .add_system(count_votes_melbourne)
        .add_system(count_votes_brisbane)
        .add_system_to_stage(CoreStage::PostUpdate, display_count)
        .run();
}

@alice-i-cecile
Copy link
Member Author

First example looks great.

With respect to using a closure, Command now has a blanket implementation for all functions that take &mut World as their only argument.

You can use an anonymous function (closure) + Commands::add to conveniently perform arbitrary logic on the world in a one-off fashion.

@bors bors bot closed this as completed in de92054 Jul 2, 2022
inodentry pushed a commit to IyesGames/bevy that referenced this issue Aug 8, 2022
# Objective

- Improve Command(s) docs
- Fixes bevyengine#4737 

## Solution

- Update and improve documentation.

---
- "list" -> "queue" in `Commands` doc (this better represents reality)
- expand `Command` doc
- update/improve `Commands::add` doc, as specified in linked issue

Let me know if you want any changes!
james7132 pushed a commit to james7132/bevy that referenced this issue Oct 28, 2022
# Objective

- Improve Command(s) docs
- Fixes bevyengine#4737 

## Solution

- Update and improve documentation.

---
- "list" -> "queue" in `Commands` doc (this better represents reality)
- expand `Command` doc
- update/improve `Commands::add` doc, as specified in linked issue

Let me know if you want any changes!
ItsDoot pushed a commit to ItsDoot/bevy that referenced this issue Feb 1, 2023
# Objective

- Improve Command(s) docs
- Fixes bevyengine#4737 

## Solution

- Update and improve documentation.

---
- "list" -> "queue" in `Commands` doc (this better represents reality)
- expand `Command` doc
- update/improve `Commands::add` doc, as specified in linked issue

Let me know if you want any changes!
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-Docs An addition or correction to our documentation D-Trivial Nice and easy! A great choice to get started with Bevy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants