@@ -146,25 +146,33 @@ use thiserror::Error;
146
146
/// assert_eq!(&C(0), world.entity(id).get::<C>().unwrap());
147
147
/// ```
148
148
///
149
- /// You can also define a custom constructor:
149
+ /// You can also define a custom constructor function or closure :
150
150
///
151
151
/// ```
152
152
/// # use bevy_ecs::prelude::*;
153
153
/// #[derive(Component)]
154
- /// #[require(B(init_b ))]
154
+ /// #[require(C(init_c ))]
155
155
/// struct A;
156
- ///
156
+ ///
157
157
/// #[derive(Component, PartialEq, Eq, Debug)]
158
- /// struct B(usize);
158
+ /// #[require(C(|| C(20)))]
159
+ /// struct B;
159
160
///
160
- /// fn init_b() -> B {
161
- /// B(10)
161
+ /// #[derive(Component, PartialEq, Eq, Debug)]
162
+ /// struct C(usize);
163
+ ///
164
+ /// fn init_c() -> C {
165
+ /// C(10)
162
166
/// }
163
167
///
164
168
/// # let mut world = World::default();
165
- /// // This will implicitly also insert B with the init_b () constructor
169
+ /// // This will implicitly also insert C with the init_c () constructor
166
170
/// let id = world.spawn(A).id();
167
- /// assert_eq!(&B(10), world.entity(id).get::<B>().unwrap());
171
+ /// assert_eq!(&C(10), world.entity(id).get::<C>().unwrap());
172
+ ///
173
+ /// // This will implicitly also insert C with the `|| C(20)` constructor closure
174
+ /// let id = world.spawn(B).id();
175
+ /// assert_eq!(&C(20), world.entity(id).get::<C>().unwrap());
168
176
/// ```
169
177
///
170
178
/// Required components are _recursive_. This means, if a Required Component has required components,
@@ -202,24 +210,16 @@ use thiserror::Error;
202
210
/// struct X(usize);
203
211
///
204
212
/// #[derive(Component, Default)]
205
- /// #[require(X(x1 ))]
213
+ /// #[require(X(|| X(1) ))]
206
214
/// struct Y;
207
215
///
208
- /// fn x1() -> X {
209
- /// X(1)
210
- /// }
211
- ///
212
216
/// #[derive(Component)]
213
217
/// #[require(
214
218
/// Y,
215
- /// X(x2 ),
219
+ /// X(|| X(2) ),
216
220
/// )]
217
221
/// struct Z;
218
222
///
219
- /// fn x2() -> X {
220
- /// X(2)
221
- /// }
222
- ///
223
223
/// # let mut world = World::default();
224
224
/// // In this case, the x2 constructor is used for X
225
225
/// let id = world.spawn(Z).id();
0 commit comments