Skip to content

Conversation

janis-bhm
Copy link
Contributor

Update examples to showcase Children, children!, SpawnIter, and related! usage

Objective

Related to #18238
The new spawning API #17521 added different ways of adding related entities, but most examples use either the old with_children method or the new children! macros.

Solution

This patch adds SceneStates to the hierarchy example which accomplishes the same relationship hierarchy using 5 different methods.

Testing

I have run the example.

@BenjaminBrienen BenjaminBrienen added A-ECS Entities, components, systems, and events C-Examples An addition or correction to our examples D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Sep 6, 2025
@janis-bhm janis-bhm force-pushed the spawn-api/ecs-hierarchy branch from 3655f24 to cd78e72 Compare September 6, 2025 16:37

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
#[derive(Debug, Clone, Eq, PartialEq, Hash, States, Default)]
#[states(scoped_entities)]
Copy link
Contributor

Choose a reason for hiding this comment

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

State scoped entities are now enabled by default. You can remove this line.

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
#[derive(Debug, Clone, Eq, PartialEq, Hash, States, Default)]
#[states(scoped_entities)]
enum Scene {
Copy link
Contributor

Choose a reason for hiding this comment

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

Given Bevy already has a Scene type, I would pick another name.

delta.0 = time.elapsed();
commands.spawn((
Text::new(title),
TextFont {
Copy link
Contributor

Choose a reason for hiding this comment

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

You could do TextFont::from_font_size(36.).

))),
));
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe showcase SpawnWith as well ?


// To demonstrate removing children, we'll remove a child after a couple of seconds.
if time.elapsed_secs() >= 2.0 && children.len() == 2 {
let elapsed = time.elapsed() - delta.0;
Copy link
Contributor

Choose a reason for hiding this comment

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

You could do

let elapsed = time.elapsed_secs() - delta.0.as_secs_f32();

such that you don't have to call .as_secs_f32() twice later.

#[derive(Resource)]
struct Delta(Duration);

fn setup_common(
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a fan of this function. I would rather copy paste its body.

Sprite::from_image(texture.clone()),
Transform::from_scale(Vec3::splat(0.75)),
DespawnOnExit(Scene::ChildrenSpawn),
Children::spawn((
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure I would showcase the use of Children::spawn because I think the children! macro should always be preferred over it. On the other hand I use Children::spawn_one because it emphasizes that a single child is being spawned.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Children::spawn can mix Spawn and other implementers of SpawnableList which I think is worth showcasing, so maybe replacing one of these Spawns with a SpawnWith would be a good idea.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh yeah I forgot about that. That's a good point.

@janis-bhm janis-bhm force-pushed the spawn-api/ecs-hierarchy branch from a46fbdf to 62edb85 Compare September 24, 2025 11:34
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-Examples An addition or correction to our examples D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants