-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
convert more examples to new spawn api #20876
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
Conversation
examples/2d/text2d.rs
Outdated
..Default::default() | ||
}, | ||
Transform::from_translation(250. * Vec3::Y), | ||
Children::spawn(SpawnIter( |
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.
not a fan of this one, but won't block on it
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 don't think I am either but I also didn't know about this functionality so maybe it's worth it!
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 the ecs/hierarchy.rs
example would lend itself quite well to demonstrating the different possible methods for inserting children/handling relations. Would you be more against having this in this example if that example included a demonstration of SpawnIter
?
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.
The API in this case just looks clunky to me, no matter if it's also used in other places. But that might just be me being more used to the "old" way :)
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.
Quite a significant improvement!
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 know that this new style can cause issues with lifetimes, as they are lazy in some parts, so i don't know if the uses of Children::spawn_one
that i highlighted are intended
the ones using SpawnIter
could be using SpawnableList
commands.spawn(Children::spawn(MySpawnList(vec![(...),(...),(...),(...)])));
struct MySpawnList(Vec<(...)>);
impl SpawnableList<ChildOf> for MySpawnList {
fn size_hint(&self) -> usize {
self.0.len()
}
fn spawn(self, world: &mut World, entity: Entity) {
for (...) in self.0 {
world.spawn((
ChildOf(entity),
(...),
...
));
}
}
}
Children::spawn_one(( | ||
Transform::default(), | ||
Visibility::default(), | ||
orbit_controller, | ||
AnimationTarget { | ||
id: orbit_controller_animation_target_id, | ||
player: planet_entity, | ||
}, | ||
Children::spawn_one(( | ||
Mesh3d(meshes.add(Cuboid::new(0.5, 0.5, 0.5))), | ||
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.9, 0.3))), | ||
Transform::from_xyz(1.5, 0.0, 0.0), | ||
AnimationTarget { | ||
id: orbit_controller_animation_target_id, | ||
id: satellite_animation_target_id, | ||
player: planet_entity, | ||
}, | ||
)) | ||
.with_children(|p| { | ||
// The satellite, placed at a distance of the planet | ||
p.spawn(( | ||
Mesh3d(meshes.add(Cuboid::new(0.5, 0.5, 0.5))), | ||
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.9, 0.3))), | ||
Transform::from_xyz(1.5, 0.0, 0.0), | ||
AnimationTarget { | ||
id: satellite_animation_target_id, | ||
player: planet_entity, | ||
}, | ||
satellite, | ||
)); | ||
}); | ||
}); | ||
satellite, | ||
)), | ||
)), | ||
)); |
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.
What stops these from being children!
?
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.
nothing, really, I just didn't see the point of using the macro here when it's just a single child. I'm not sure I see why using children!
everywhere, even when it results in more indirection (which might get compiled out), is better.
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 have a vague memory of someone saying that children!
should always take precedence over Children::spawn
or Children::spawn_one
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 personally think using children!
everywhere is preferable in learning material to lower the complexity for beginners.
examples/animation/animated_ui.rs
Outdated
entity.insert(Children::spawn_one(( | ||
Text::new("Bevy"), | ||
TextFont { | ||
font: asset_server.load("fonts/FiraSans-Bold.ttf"), | ||
font_size: 24.0, | ||
..default() | ||
}, | ||
TextColor(Color::Srgba(Srgba::RED)), | ||
TextLayout::new_with_justify(Justify::Center), | ||
AnimationTarget { | ||
id: animation_target_id, | ||
player, | ||
}, | ||
animation_target_name, | ||
))); |
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.
Can't this one be children!
?
Children::spawn_one(( | ||
Text(format!("{label:?}")), | ||
if index > 0 { | ||
button_text_style.clone() | ||
} else { | ||
selected_button_text_style.clone() | ||
}, | ||
TextLayout::new_with_justify(Justify::Center), | ||
Node { | ||
flex_grow: 1.0, | ||
margin: UiRect::vertical(px(3)), | ||
..default() | ||
}, | ||
)), |
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.
children!
?
Objective
works on #18238
Solution
convert calls to
.with_children
to use theChildren::spawn
orChildren::spawn_one
types orchildren!
macro.This touches the
window
,2d
,animation
folders, as well asecs/one_shot_systems.rs
.observer_propagation.rs
looks like exactly whatwith_children
is useful for, so I deliberately haven't touched it.I can break this up into more PRs or squash if desired.
Testing
I've run the examples before and after this patch and verified visually that nothing has changed