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

Docs for ".entry()" in EntityCommands is confusing / incomplete #16379

Open
viridia opened this issue Nov 13, 2024 · 1 comment
Open

Docs for ".entry()" in EntityCommands is confusing / incomplete #16379

viridia opened this issue Nov 13, 2024 · 1 comment
Labels
A-ECS Entities, components, systems, and events C-Docs An addition or correction to our documentation D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it! X-Uncontroversial This work is generally agreed upon

Comments

@viridia
Copy link
Contributor

viridia commented Nov 13, 2024

How can Bevy's documentation be improved?

This relates to the new .entry() API for manipulating components. Specifically, the documentation doesn't make clear how to do a "get or insert" type of operation, which is what I think most users want. Specifically, the code example for .entry() looks like this:

    /// fn level_up_system(mut commands: Commands, player: Res<PlayerEntity>) {
    ///     commands
    ///         .entity(player.entity)
    ///         .entry::<Level>()
    ///         // Modify the component if it exists
    ///         .and_modify(|mut lvl| lvl.0 += 1)
    ///         // Otherwise insert a default value
    ///         .or_insert(Level(0));
    /// }

This example seems to imply that if you want to modify a component in place, or create a new component if it does not already exist, then you have to specify the component values twice: once for the "already exists" case, and once for the "create new" case. In this example the component only has one field, but for something like Node this would be onerous. My mistaken understanding caused me to avoid using this API for a long time - thinking it wasn't that useful - until someone on Discord informed me about the .or_default() option.

My suggestion is that the docs for .entry() should include an example which is less artificial, and more like a typical use case: "get the component if it exists, create it if it doesn't, and then let me specify the parameters one time."

@viridia viridia added C-Docs An addition or correction to our documentation S-Needs-Triage This issue needs to be labelled labels Nov 13, 2024
@alice-i-cecile alice-i-cecile added D-Trivial Nice and easy! A great choice to get started with Bevy A-ECS Entities, components, systems, and events S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it! D-Straightforward Simple bug fixes and API improvements, docs, test and examples X-Uncontroversial This work is generally agreed upon and removed S-Needs-Triage This issue needs to be labelled D-Trivial Nice and easy! A great choice to get started with Bevy labels Nov 13, 2024
@alice-i-cecile
Copy link
Member

Note that the order in which you call .and_modify vs .or_default / or_insert matters! If you want to insert and then modify something, initialize it first. If you want to only modify the value if it exists, do the reverse.

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-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it! X-Uncontroversial This work is generally agreed upon
Projects
None yet
Development

No branches or pull requests

2 participants