@@ -4,7 +4,7 @@ use crate::{
44 bundle:: Bundle ,
55 component:: Component ,
66 entity:: { Entities , Entity } ,
7- world:: World ,
7+ world:: { FromWorld , World } ,
88} ;
99use bevy_utils:: tracing:: { error, warn} ;
1010pub use command_queue:: CommandQueue ;
@@ -261,9 +261,45 @@ impl<'w, 's> Commands<'w, 's> {
261261 self . queue . push ( InsertOrSpawnBatch { bundles_iter } ) ;
262262 }
263263
264+ /// Inserts a resource with standard starting values to the [`World`].
265+ ///
266+ /// If the resource already exists, nothing happens.
267+ ///
268+ /// The value given by the [`FromWorld::from_world`] method will be used.
269+ /// Note that any resource with the `Default` trait automatically implements `FromWorld`,
270+ /// and those default values will be here instead.
271+ ///
272+ /// See [`World::init_resource`] for more details.
273+ /// Note that commands do not take effect immediately.
274+ /// When possible, prefer the equivalent methods on `App` or `World`.
275+ ///
276+ /// # Example
277+ ///
278+ /// ```
279+ /// # use bevy_ecs::prelude::*;
280+ /// #
281+ /// # #[derive(Default)]
282+ /// # struct Scoreboard {
283+ /// # current_score: u32,
284+ /// # high_score: u32,
285+ /// # }
286+ /// #
287+ /// # fn system(mut commands: Commands) {
288+ /// commands.init_resource::<Scoreboard>();
289+ /// # }
290+ /// # system.system();
291+ /// ```
292+ pub fn init_resource < R : Resource + FromWorld > ( & mut self ) {
293+ self . queue . push ( InitResource :: < R > {
294+ _phantom : PhantomData :: < R > :: default ( ) ,
295+ } )
296+ }
297+
264298 /// Inserts a resource to the [`World`], overwriting any previous value of the same type.
265299 ///
266300 /// See [`World::insert_resource`] for more details.
301+ /// Note that commands do not take effect immediately.
302+ /// When possible, prefer the equivalent methods on `App` or `World`.
267303 ///
268304 /// # Example
269305 ///
@@ -283,7 +319,7 @@ impl<'w, 's> Commands<'w, 's> {
283319 /// # }
284320 /// # bevy_ecs::system::assert_is_system(system);
285321 /// ```
286- pub fn insert_resource < T : Resource > ( & mut self , resource : T ) {
322+ pub fn insert_resource < R : Resource > ( & mut self , resource : R ) {
287323 self . queue . push ( InsertResource { resource } )
288324 }
289325
@@ -306,8 +342,8 @@ impl<'w, 's> Commands<'w, 's> {
306342 /// # }
307343 /// # bevy_ecs::system::assert_is_system(system);
308344 /// ```
309- pub fn remove_resource < T : Resource > ( & mut self ) {
310- self . queue . push ( RemoveResource :: < T > {
345+ pub fn remove_resource < R : Resource > ( & mut self ) {
346+ self . queue . push ( RemoveResource :: < R > {
311347 phantom : PhantomData ,
312348 } ) ;
313349 }
@@ -713,23 +749,33 @@ where
713749 }
714750}
715751
716- pub struct InsertResource < T : Resource > {
717- pub resource : T ,
752+ pub struct InitResource < R : Resource + FromWorld > {
753+ _phantom : PhantomData < R > ,
718754}
719755
720- impl < T : Resource > Command for InsertResource < T > {
756+ impl < R : Resource + FromWorld > Command for InitResource < R > {
757+ fn write ( self , world : & mut World ) {
758+ world. init_resource :: < R > ( ) ;
759+ }
760+ }
761+
762+ pub struct InsertResource < R : Resource > {
763+ pub resource : R ,
764+ }
765+
766+ impl < R : Resource > Command for InsertResource < R > {
721767 fn write ( self , world : & mut World ) {
722768 world. insert_resource ( self . resource ) ;
723769 }
724770}
725771
726- pub struct RemoveResource < T : Resource > {
727- pub phantom : PhantomData < T > ,
772+ pub struct RemoveResource < R : Resource > {
773+ pub phantom : PhantomData < R > ,
728774}
729775
730- impl < T : Resource > Command for RemoveResource < T > {
776+ impl < R : Resource > Command for RemoveResource < R > {
731777 fn write ( self , world : & mut World ) {
732- world. remove_resource :: < T > ( ) ;
778+ world. remove_resource :: < R > ( ) ;
733779 }
734780}
735781
0 commit comments