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 BRP method to mutate a component #16940

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

purplg
Copy link
Contributor

@purplg purplg commented Dec 23, 2024

Objective

Add a method to mutate components with BRP.

Currently the only way to modify a component on an entity with BRP is to insert a new one with the new values. This isn't ideal for several reasons, one reason being that the client has to know what all the fields are of the component and stay in sync with the server.

Solution

Add a new BRP method called bevy/mutate_component to mutate a single field in a component on an entity.

Testing

Tested on a simple scene on all Transform, Name, and a custom component.


Showcase

Example JSON-RPC request to change the Name of an entity to "New name!"

{
    "jsonrpc": "2.0",
    "id": 0,
    "method": "bevy/mutate_component",
    "params": {
        "entity": 4294967308,
        "component": "bevy_ecs::name::Name",
        "path": ".name",
        "value": "New name!"
    }
}

Or setting the X translation to 10.0 on a Transform:

{
    "jsonrpc": "2.0",
    "id": 0,
    "method": "bevy/mutate_component",
    "params": {
        "entity": 4294967308,
        "component": "bevy_transform::components::transform::Transform",
        "path": ".translation.x",
        "value": 10.0
    }
}

Clip of my Emacs BRP package using this method:

output.mp4

@@ -48,6 +48,9 @@ pub const BRP_REPARENT_METHOD: &str = "bevy/reparent";
/// The method path for a `bevy/list` request.
pub const BRP_LIST_METHOD: &str = "bevy/list";

/// The method path for a `bevy/reparent` request.
pub const BRP_MUTATE_COMPONENT_METHOD: &str = "bevy/mutate_component";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what the convention is for multiple words in a method name is. Should the method be named differently? I'm trying to consider future methods for modifying other things like Resource's.

I had a few other ideas for methods:

  • bevy/modify/component
  • bevy/mutate/component
  • bevy/component_mut this feels too Rusty in this context

@purplg purplg changed the title Add mutate_component BRP method Add BRP method to mutate a component Dec 23, 2024
@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-Dev-Tools Tools used to debug Bevy applications. S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Dec 24, 2024
@purplg purplg force-pushed the brp_component_mutate_method branch from 20785c1 to 9e738a6 Compare January 4, 2025 16:55
@purplg purplg force-pushed the brp_component_mutate_method branch from 9e738a6 to 204e049 Compare January 4, 2025 17:02
@purplg purplg marked this pull request as ready for review January 4, 2025 17:03
@BenjaminBrienen BenjaminBrienen added the D-Straightforward Simple bug fixes and API improvements, docs, test and examples label Jan 4, 2025
Copy link
Contributor

@cBournhonesque cBournhonesque left a comment

Choose a reason for hiding this comment

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

This generally LGTM.
It's a shame that we don't have unit tests for BRP

@purplg
Copy link
Contributor Author

purplg commented Jan 6, 2025

It's a shame that we don't have unit tests for BRP

I agree. I did look at writing tests for this, but I didn't see an existing way to do this.

Copy link
Member

@LiamGallagher737 LiamGallagher737 left a comment

Choose a reason for hiding this comment

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

It would be useful to link to the following docs in the crate level docs for this method to show how the path field should be structured.

https://docs.rs/bevy/latest/bevy/prelude/trait.GetPath.html#syntax

@BenjaminBrienen BenjaminBrienen added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jan 9, 2025
Copy link
Contributor

Your PR increases Bevy Minimum Supported Rust Version. Please update the rust-version field in the root Cargo.toml file.

@purplg
Copy link
Contributor Author

purplg commented Jan 10, 2025

Good point on that doc link. Added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Dev-Tools Tools used to debug Bevy applications. C-Feature A new feature, making something new possible D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants