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 insert_unique command #9231

Closed
wants to merge 2 commits into from
Closed

Add insert_unique command #9231

wants to merge 2 commits into from

Conversation

jfaz1
Copy link
Contributor

@jfaz1 jfaz1 commented Jul 21, 2023

Objective

Currently, calling insert on an entity will overwrite any components that share the same type, without any way to specify whether that should be the case. There are a couple of workarounds, most of which (in my experience) are either ugly or trigger unwanted change detection. This is less than ideal since there are relatively common use cases for wanting to insert a bundle without overwriting, like wanting to insert a bundle into an entity that already has a transform.

Solution

  • Add an insert_unique command for usage when inserting a bundle without overwriting, e.g:
let mut entity = commands.spawn(TransformBundle::from_transform(Transform::from_xyz(0.0, 1.5, 0.0)));
entity.insert_unique(PbrBundle {
    mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
    material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
    ..default()
});
// Transform components on the entity are NOT affected by the PbrBundle insert

Starting as a draft since this is my first time touching bevy_ecs so I kept it totally unobtrusive, originally I wanted to separate chunks into inlined functions but I'd rather hear what you guys have to say first 😄 Feel free to make any edits.

@github-actions
Copy link
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Jul 21, 2023
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-Usability A targeted quality-of-life change that makes Bevy easier to use
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants