1
- use crate :: prelude:: { Children , Parent , PreviousParent } ;
1
+ use smallvec:: SmallVec ;
2
+
2
3
use bevy_ecs:: {
3
4
bundle:: Bundle ,
4
5
entity:: Entity ,
5
6
system:: { Command , Commands , EntityCommands } ,
6
7
world:: { EntityMut , World } ,
7
8
} ;
8
- use smallvec:: SmallVec ;
9
+
10
+ use crate :: prelude:: { Children , Parent , PreviousParent } ;
9
11
10
12
/// Command that adds a child to an entity
11
13
#[ derive( Debug ) ]
@@ -165,7 +167,40 @@ impl<'w, 's, 'a> ChildBuilder<'w, 's, 'a> {
165
167
/// Trait defining how to build children
166
168
pub trait BuildChildren {
167
169
/// Creates a [`ChildBuilder`] with the given children built in the given closure
170
+ ///
171
+ /// Compared to [`add_children`][BuildChildren::add_children], this method returns self
172
+ /// to allow chaining.
168
173
fn with_children ( & mut self , f : impl FnOnce ( & mut ChildBuilder ) ) -> & mut Self ;
174
+ /// Creates a [`ChildBuilder`] with the given children built in the given closure
175
+ ///
176
+ /// Compared to [`with_children`][BuildChildren::with_children], this method returns the
177
+ /// the value returned from the closure, but doesn't allow chaining.
178
+ ///
179
+ /// ## Example
180
+ ///
181
+ /// ```no_run
182
+ /// # use bevy_ecs::prelude::*;
183
+ /// # use bevy_hierarchy::*;
184
+ /// #
185
+ /// # #[derive(Component)]
186
+ /// # struct SomethingElse;
187
+ /// #
188
+ /// # #[derive(Component)]
189
+ /// # struct MoreStuff;
190
+ /// #
191
+ /// # fn foo(mut commands: Commands) {
192
+ /// let mut parent_commands = commands.spawn();
193
+ /// let child_id = parent_commands.add_children(|parent| {
194
+ /// parent.spawn().id()
195
+ /// });
196
+ ///
197
+ /// parent_commands.insert(SomethingElse);
198
+ /// commands.entity(child_id).with_children(|parent| {
199
+ /// parent.spawn().insert(MoreStuff);
200
+ /// });
201
+ /// # }
202
+ /// ```
203
+ fn add_children < T > ( & mut self , f : impl FnOnce ( & mut ChildBuilder ) -> T ) -> T ;
169
204
/// Pushes children to the back of the builder's children
170
205
fn push_children ( & mut self , children : & [ Entity ] ) -> & mut Self ;
171
206
/// Inserts children at the given index
@@ -178,21 +213,25 @@ pub trait BuildChildren {
178
213
179
214
impl < ' w , ' s , ' a > BuildChildren for EntityCommands < ' w , ' s , ' a > {
180
215
fn with_children ( & mut self , spawn_children : impl FnOnce ( & mut ChildBuilder ) ) -> & mut Self {
216
+ self . add_children ( spawn_children) ;
217
+ self
218
+ }
219
+
220
+ fn add_children < T > ( & mut self , spawn_children : impl FnOnce ( & mut ChildBuilder ) -> T ) -> T {
181
221
let parent = self . id ( ) ;
182
- let push_children = {
183
- let mut builder = ChildBuilder {
184
- commands : self . commands ( ) ,
185
- push_children : PushChildren {
186
- children : SmallVec :: default ( ) ,
187
- parent,
188
- } ,
189
- } ;
190
- spawn_children ( & mut builder) ;
191
- builder. push_children
222
+ let mut builder = ChildBuilder {
223
+ commands : self . commands ( ) ,
224
+ push_children : PushChildren {
225
+ children : SmallVec :: default ( ) ,
226
+ parent,
227
+ } ,
192
228
} ;
193
229
194
- self . commands ( ) . add ( push_children) ;
195
- self
230
+ let result = spawn_children ( & mut builder) ;
231
+ let children = builder. push_children ;
232
+ self . commands ( ) . add ( children) ;
233
+
234
+ result
196
235
}
197
236
198
237
fn push_children ( & mut self , children : & [ Entity ] ) -> & mut Self {
@@ -460,15 +499,18 @@ impl<'w> BuildWorldChildren for WorldChildBuilder<'w> {
460
499
461
500
#[ cfg( test) ]
462
501
mod tests {
463
- use super :: { BuildChildren , BuildWorldChildren } ;
464
- use crate :: prelude :: { Children , Parent , PreviousParent } ;
502
+ use smallvec :: { smallvec , SmallVec } ;
503
+
465
504
use bevy_ecs:: {
466
505
component:: Component ,
467
506
entity:: Entity ,
468
507
system:: { CommandQueue , Commands } ,
469
508
world:: World ,
470
509
} ;
471
- use smallvec:: { smallvec, SmallVec } ;
510
+
511
+ use crate :: prelude:: { Children , Parent , PreviousParent } ;
512
+
513
+ use super :: { BuildChildren , BuildWorldChildren } ;
472
514
473
515
#[ derive( Component ) ]
474
516
struct C ( u32 ) ;
@@ -479,12 +521,13 @@ mod tests {
479
521
let mut queue = CommandQueue :: default ( ) ;
480
522
let mut commands = Commands :: new ( & mut queue, & world) ;
481
523
482
- let mut children = Vec :: new ( ) ;
483
524
let parent = commands. spawn ( ) . insert ( C ( 1 ) ) . id ( ) ;
484
- commands. entity ( parent) . with_children ( |parent| {
485
- children. push ( parent. spawn ( ) . insert ( C ( 2 ) ) . id ( ) ) ;
486
- children. push ( parent. spawn ( ) . insert ( C ( 3 ) ) . id ( ) ) ;
487
- children. push ( parent. spawn ( ) . insert ( C ( 4 ) ) . id ( ) ) ;
525
+ let children = commands. entity ( parent) . add_children ( |parent| {
526
+ [
527
+ parent. spawn ( ) . insert ( C ( 2 ) ) . id ( ) ,
528
+ parent. spawn ( ) . insert ( C ( 3 ) ) . id ( ) ,
529
+ parent. spawn ( ) . insert ( C ( 4 ) ) . id ( ) ,
530
+ ]
488
531
} ) ;
489
532
490
533
queue. apply ( & mut world) ;
0 commit comments