-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Show off different methods for spawning related entities in examples/ecs/hierarchy.rs
#20904
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
base: main
Are you sure you want to change the base?
Conversation
3655f24
to
cd78e72
Compare
examples/ecs/hierarchy.rs
Outdated
|
||
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { | ||
#[derive(Debug, Clone, Eq, PartialEq, Hash, States, Default)] | ||
#[states(scoped_entities)] |
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.
State scoped entities are now enabled by default. You can remove this line.
examples/ecs/hierarchy.rs
Outdated
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { | ||
#[derive(Debug, Clone, Eq, PartialEq, Hash, States, Default)] | ||
#[states(scoped_entities)] | ||
enum Scene { |
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.
Given Bevy already has a Scene
type, I would pick another name.
examples/ecs/hierarchy.rs
Outdated
delta.0 = time.elapsed(); | ||
commands.spawn(( | ||
Text::new(title), | ||
TextFont { |
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.
You could do TextFont::from_font_size(36.)
.
))), | ||
)); | ||
} | ||
|
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.
Maybe showcase SpawnWith
as well ?
examples/ecs/hierarchy.rs
Outdated
|
||
// 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; |
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.
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( |
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 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(( |
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 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.
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::spawn
can mix Spawn
and other implementers of SpawnableList
which I think is worth showcasing, so maybe replacing one of these Spawn
s with a SpawnWith
would be a good idea.
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.
Oh yeah I forgot about that. That's a good point.
- Update examples to showcase Children, children!, SpawnIter, and related! usage
a46fbdf
to
62edb85
Compare
Update examples to showcase
Children
,children!
,SpawnIter,
andrelated!
usageObjective
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 newchildren!
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.