bevyengine / bevyengine/bevy

Alternative to `remove_with_requires` that doesn't remove components that still have other dependants

Open
#17,757 1 comment 2 reactions 0 assignees View on GitHub
A-ECS C-Feature S-Needs-Design
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

## What problem does this solve or what need does it fill?

`remove_with_requires` currently makes no additional checks to see if any other component requires the same dependencies. This can result in the method removing another component's required dependency.

Take this example, where I expected `A` to remain because it is still required by `C`
```rust
#[derive(Component, Default)]
struct A;

#[derive(Component, Default)]
#[require(A)]
struct B;

#[derive(Component, Default)]
#[require(A)]
struct C;

#[test]
fn test_remove_double_requires() {
let mut world = World::new();
let mut entity = world.spawn((B, C));
entity.remove_with_requires::();
assert!(entity.contains::()); // fails
}
```

## What solution would you like?

Let required components have a sort of ref-counting of required components. The current implementation of `remove_with_requires` could be altered, or a soft variant called something like `remove_with_checked_requires` could be added. This method would only remove a required component if no remaining components still requires it.

## What alternative(s) have you considered?

It's possible to make your own function that makes sure to check if specific components are present before removing a dependency, and call that instead if you need to remove the required component.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.