-
-
Couldn't load subscription status.
- Fork 4.2k
Add helper methods to dynamically access relationships #21639
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
Open
cBournhonesque
wants to merge
11
commits into
bevyengine:main
Choose a base branch
from
cBournhonesque:cb/dynamic-access-relationships
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5168eb5
add helpers to dynamic access relationships
cBournhonesque 49604d5
update migration guide
cBournhonesque 7434087
fixed filtered mut
cBournhonesque 046a91d
fixed filtered mut
cBournhonesque 1ac028c
fmt
5c25534
use impl Iterator instead of Box<dyn Iterator>
cBournhonesque fc4ab07
markdownlint
cBournhonesque 4e4c208
address comments
cBournhonesque 59fcdc7
nits
cBournhonesque 1310741
fix lifetime
cBournhonesque 1db8571
cargo fmt
cBournhonesque File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
crates/bevy_ecs/compile_fail/tests/ui/filtered_entity_mut_get_relationship_target_by_id.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| //@no-rustfix | ||
|
|
||
| use bevy_ecs::prelude::*; | ||
| use bevy_ecs::world::FilteredEntityMut; | ||
| fn main() { | ||
| let mut world = World::new(); | ||
| let parent = world.spawn_empty().id(); | ||
| let _ = world.spawn(ChildOf(parent)).id(); | ||
|
|
||
| let children_id = world.register_component::<Children>(); | ||
|
|
||
| let mut query = QueryBuilder::<FilteredEntityMut>::new(&mut world) | ||
| .data::<&Children>() | ||
| .build(); | ||
| let mut filtered_entity = query.single_mut(&mut world).unwrap(); | ||
|
|
||
| let _borrows_r1 = filtered_entity.get_relationship_targets_by_id(children_id); | ||
| let _borrows_r1_mutably = filtered_entity.get_mut_by_id(children_id); | ||
| } |
13 changes: 13 additions & 0 deletions
13
...s/bevy_ecs/compile_fail/tests/ui/filtered_entity_mut_get_relationship_target_by_id.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| error[E0502]: cannot borrow `filtered_entity` as mutable because it is also borrowed as immutable | ||
| --> tests/ui/filtered_entity_mut_get_relationship_target_by_id.rs:18:31 | ||
| | | ||
| 17 | let _borrows_r1 = filtered_entity.get_relationship_targets_by_id(children_id); | ||
| | --------------- immutable borrow occurs here | ||
| 18 | let _borrows_r1_mutably = filtered_entity.get_mut_by_id(children_id); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here | ||
| 19 | } | ||
| | - immutable borrow might be used here, when `_borrows_r1` is dropped and runs the destructor for type `Option<impl Iterator<Item = Entity>>` | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0502`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -591,6 +591,70 @@ impl<'w> EntityMut<'w> { | |
| unsafe { component_ids.fetch_mut_assume_mutable(self.cell) } | ||
| } | ||
|
|
||
| /// Gets the "target" entity of this entity via the [`Relationship`] component with the given [`ComponentId`]. | ||
| /// | ||
| /// **You should prefer to use the typed API where possible and only | ||
| /// use this in cases where the actual component types are not known at | ||
| /// compile time.** | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ## Single [`ComponentId`] | ||
| /// | ||
| /// ``` | ||
| /// # use bevy_ecs::prelude::*; | ||
| /// # | ||
| /// # let mut world = World::new(); | ||
| /// let parent = world.spawn_empty().id(); | ||
| /// let child = world.spawn(ChildOf(parent)).id(); | ||
| /// | ||
| /// // Grab the component ID for `ChildOf` in whatever way you like. | ||
| /// let component_id = world.register_component::<ChildOf>(); | ||
| /// | ||
| /// // Then, get the target entity via the 'ChildOf' relationship by ID. | ||
| /// let target = world.entity(child).get_relationship_by_id(component_id).unwrap(); | ||
| /// assert_eq!(target, parent); | ||
| /// ``` | ||
| /// | ||
| /// [`Relationship`]: crate::relationship::Relationship | ||
| pub fn get_relationship_by_id(&self, relationship_id: ComponentId) -> Option<Entity> { | ||
| self.as_readonly().get_relationship_by_id(relationship_id) | ||
| } | ||
|
|
||
| /// Gets an iterator to the "related" entities of this entity via the [`RelationshipTarget`] component with the given [`ComponentId`]. | ||
| /// | ||
| /// **You should prefer to use the typed API where possible and only | ||
| /// use this in cases where the actual component types are not known at | ||
| /// compile time.** | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ## Single [`ComponentId`] | ||
| /// | ||
| /// ``` | ||
| /// # use bevy_ecs::prelude::*; | ||
| /// # | ||
| /// # let mut world = World::new(); | ||
| /// let parent = world.spawn_empty().id(); | ||
| /// let child = world.spawn(ChildOf(parent)).id(); | ||
| /// | ||
| /// // Grab the component ID for `Children` in whatever way you like. | ||
| /// let component_id = world.register_component::<Children>(); | ||
| /// | ||
| /// // Then, get the target entities via the 'Children' relationship by ID. | ||
| /// let targets: Vec<Entity> = world.entity(parent).get_relationship_targets_by_id(component_id).unwrap().collect(); | ||
| /// assert_eq!(targets, vec![child]); | ||
| /// ``` | ||
| /// | ||
| /// [`RelationshipTarget`]: crate::relationship::RelationshipTarget | ||
| pub fn get_relationship_targets_by_id( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto on naming here. |
||
| &self, | ||
| relationship_target_id: ComponentId, | ||
| ) -> Option<impl Iterator<Item = Entity> + use<'_>> { | ||
| self.as_readonly() | ||
| .get_relationship_targets_by_id(relationship_target_id) | ||
| } | ||
|
|
||
| /// Consumes `self` and returns untyped mutable reference(s) | ||
| /// to component(s) with lifetime `'w` for the current entity, based on the | ||
| /// given [`ComponentId`]s. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,15 @@ | ||
| --- | ||
| title: API for working with `Relationships` and `RelationshipTargets` in type-erased contexts | ||
| pull_requests: [21601] | ||
| pull_requests: [21601, 21639] | ||
| --- | ||
|
|
||
| `ComponentDescriptor` now stores additional data for working with relationships in dynamic contexts. | ||
| This resulted in changes to `ComponentDescriptor::new_with_layout`: | ||
|
|
||
| - Now requires additional parameter `relationship_accessor`, which should be set to `None` for all existing code creating `ComponentDescriptors`. | ||
|
|
||
| `UnsafeEntityCell`, `EntityRef`, `EntityMut`, `FilteredEntityRef`, `FilteredEntityMut` can now access relationship values | ||
| in dynamic contexts with the new public methods: | ||
|
|
||
| - `get_relationship_by_id` | ||
| - `get_relationship_targets_by_id |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this needs a clearer name. You're not getting the relationship itself, you're getting the contained entity.