Skip to content

Allow entities to be added to a relationship using the new spawn api (remade) #19046

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
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Freyja-moth
Copy link
Contributor

Objective

Reopens #18961

Solution

Copy the code

Testing

It worked in the previous pull request

Showcase

See #18961

@hukasu
Copy link
Contributor

hukasu commented May 3, 2025

i would have a #[test] with a simple example, could very well be the code from the old PR with asserts to show that the ChildOf is being added correctly

@alice-i-cecile alice-i-cecile changed the title Fixed me stupidly deleting the fork Allow entities to be added to a relationship using the new spawn api (remade) May 3, 2025
@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-ECS Entities, components, systems, and events S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels May 3, 2025
@alice-i-cecile alice-i-cecile added this to the 0.17 milestone May 3, 2025
@Freyja-moth
Copy link
Contributor Author

Where should I put the test? In src/spawn.rs or are the tests for the Spawn/SpawnIter/SpawnWith that I should place them with.

Copy link
Contributor

@Trashtalk217 Trashtalk217 left a comment

Choose a reason for hiding this comment

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

Looks good!

@hukasu
Copy link
Contributor

hukasu commented May 3, 2025

yes, it could be on spawn.rs
on the end of the file do

#[cfg(test)]
mod tests {
   ...
}

@Freyja-moth
Copy link
Contributor Author

Added the tests, also took the liberty of adding tests for Spawn, SpawnIter, and SpawnWith.

@hukasu
Copy link
Contributor

hukasu commented May 3, 2025

i would have asserts to check if the ChildOf of child1 and child2 point correctly to parent, just to be pedantic

@Freyja-moth
Copy link
Contributor Author

I'm not all that familiar with working directly with world and got stuck trying to find a way to do it by validating the Children, rather than ChildOf and eventually gave up. Will try that

@hukasu
Copy link
Contributor

hukasu commented May 3, 2025

world.entity(child1).get::<ChildOf>() or something similar

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Contentious There are nontrivial implications that should be thought through and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels May 4, 2025
/// Name::new("Root"),
/// Children::spawn((
/// Spawn(Name::new("Child1")),
/// WithRelated([child2, child3].into_iter()),
Copy link
Member

Choose a reason for hiding this comment

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

Reading this I'm not immediately sure what the inferred generic I is, or how that's determined. Can you add a comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to check, something like this?

world.spawn((
    Name::new("Root"),
    Children::spawn((
        Spawn(Name::new("Child1")),
        // [`I`] here is [`Iterator<Item = Entity>`]
        WithRelated([child2, child3].into_iter()),
    )),
));

Copy link
Member

Choose a reason for hiding this comment

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

No, explaining that WithRelated adds the existing entities as children, inferred due to the Children type above.

/// A wrapper over an [`Entity`] indicating that an entity should be added.
/// This is intended to be used for hierarchical spawning via traits like [`SpawnableList`] and [`SpawnRelated`].
///
/// Also see the [`children`](crate::children) and [`related`](crate::related) macros that abstract over the [`Spawn`] API.
Copy link
Member

Choose a reason for hiding this comment

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

These docs need to crosslink against WithRelated, explaining the differences.

@@ -133,6 +133,73 @@ impl<R: Relationship, F: FnOnce(&mut RelatedSpawner<R>) + Send + Sync + 'static>
}
}

/// A [`SpawnableList`] that adds entities using an iterator of [`Entity`]:
Copy link
Member

Choose a reason for hiding this comment

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

This isn't a particularly useful description by itself, can you add another line explaining why users might to do this?

@alice-i-cecile alice-i-cecile added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it labels May 6, 2025
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

Generally looks like a nice feature, but I found the docs and motivation hard to follow, even after spending a fair bit of time puzzling over it. We need to make sure that this is clear so users know when to use this new API :)

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
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-Feature A new feature, making something new possible S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged X-Contentious There are nontrivial implications that should be thought through
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants