@@ -28,6 +28,7 @@ pub struct InstanceInfo {
28
28
pub entity_map : HashMap < Entity , Entity > ,
29
29
}
30
30
31
+ /// Unique id identifying a scene instance.
31
32
#[ derive( Copy , Clone , Debug , Eq , PartialEq , Hash ) ]
32
33
pub struct InstanceId ( Uuid ) ;
33
34
@@ -37,6 +38,25 @@ impl InstanceId {
37
38
}
38
39
}
39
40
41
+ /// Handles spawning and despawning scenes in the world, either synchronously or batched through the [`scene_spawner_system`].
42
+ ///
43
+ /// Synchronous methods: (Scene operations will take effect immediately)
44
+ /// - [`spawn_dynamic_sync`](Self::spawn_dynamic_sync)
45
+ /// - [`spawn_sync`](Self::spawn_sync)
46
+ /// - [`despawn_sync`](Self::despawn_sync)
47
+ /// - [`despawn_instance_sync`](Self::despawn_instance_sync)
48
+ /// - [`update_spawned_scenes`](Self::update_spawned_scenes)
49
+ /// - [`spawn_queued_scenes`](Self::spawn_queued_scenes)
50
+ /// - [`despawn_queued_scenes`](Self::despawn_queued_scenes)
51
+ /// - [`despawn_queued_instances`](Self::despawn_queued_instances)
52
+ ///
53
+ /// Deferred methods: (Scene operations will be processed when the [`scene_spawner_system`] is run)
54
+ /// - [`spawn_dynamic`](Self::spawn_dynamic)
55
+ /// - [`spawn_dynamic_as_child`](Self::spawn_dynamic_as_child)
56
+ /// - [`spawn`](Self::spawn)
57
+ /// - [`spawn_as_child`](Self::spawn_as_child)
58
+ /// - [`despawn`](Self::despawn)
59
+ /// - [`despawn_instance`](Self::despawn_instance)
40
60
#[ derive( Default , Resource ) ]
41
61
pub struct SceneSpawner {
42
62
spawned_scenes : HashMap < AssetId < Scene > , Vec < InstanceId > > ,
@@ -50,27 +70,50 @@ pub struct SceneSpawner {
50
70
scenes_with_parent : Vec < ( InstanceId , Entity ) > ,
51
71
}
52
72
73
+ /// Errors that can occur when spawning a scene.
53
74
#[ derive( Error , Debug ) ]
54
75
pub enum SceneSpawnError {
76
+ /// Scene contains an unregistered component type.
55
77
#[ error( "scene contains the unregistered component `{type_name}`. consider adding `#[reflect(Component)]` to your type" ) ]
56
- UnregisteredComponent { type_name : String } ,
78
+ UnregisteredComponent {
79
+ /// Type of the unregistered component.
80
+ type_name : String ,
81
+ } ,
82
+ /// Scene contains an unregistered resource type.
57
83
#[ error( "scene contains the unregistered resource `{type_name}`. consider adding `#[reflect(Resource)]` to your type" ) ]
58
- UnregisteredResource { type_name : String } ,
84
+ UnregisteredResource {
85
+ /// Type of the unregistered resource.
86
+ type_name : String ,
87
+ } ,
88
+ /// Scene contains an unregistered type.
59
89
#[ error( "scene contains the unregistered type `{type_name}`. consider registering the type using `app.register_type::<T>()`" ) ]
60
- UnregisteredType { type_name : String } ,
90
+ UnregisteredType {
91
+ /// The unregistered type.
92
+ type_name : String ,
93
+ } ,
94
+ /// Dynamic scene with the given id does not exist.
61
95
#[ error( "scene does not exist" ) ]
62
- NonExistentScene { id : AssetId < DynamicScene > } ,
96
+ NonExistentScene {
97
+ /// Id of the non-existent dynamic scene.
98
+ id : AssetId < DynamicScene > ,
99
+ } ,
100
+ /// Scene with the given id does not exist.
63
101
#[ error( "scene does not exist" ) ]
64
- NonExistentRealScene { id : AssetId < Scene > } ,
102
+ NonExistentRealScene {
103
+ /// Id of the non-existent scene.
104
+ id : AssetId < Scene > ,
105
+ } ,
65
106
}
66
107
67
108
impl SceneSpawner {
109
+ /// Schedule the spawn of a new instance of the provided dynamic scene.
68
110
pub fn spawn_dynamic ( & mut self , id : impl Into < AssetId < DynamicScene > > ) -> InstanceId {
69
111
let instance_id = InstanceId :: new ( ) ;
70
112
self . dynamic_scenes_to_spawn . push ( ( id. into ( ) , instance_id) ) ;
71
113
instance_id
72
114
}
73
115
116
+ /// Schedule the spawn of a new instance of the provided dynamic scene as a child of `parent`.
74
117
pub fn spawn_dynamic_as_child (
75
118
& mut self ,
76
119
id : impl Into < AssetId < DynamicScene > > ,
@@ -82,27 +125,32 @@ impl SceneSpawner {
82
125
instance_id
83
126
}
84
127
128
+ /// Schedule the spawn of a new instance of the provided scene.
85
129
pub fn spawn ( & mut self , id : impl Into < AssetId < Scene > > ) -> InstanceId {
86
130
let instance_id = InstanceId :: new ( ) ;
87
131
self . scenes_to_spawn . push ( ( id. into ( ) , instance_id) ) ;
88
132
instance_id
89
133
}
90
134
135
+ /// Schedule the spawn of a new instance of the provided scene as a child of `parent`.
91
136
pub fn spawn_as_child ( & mut self , id : impl Into < AssetId < Scene > > , parent : Entity ) -> InstanceId {
92
137
let instance_id = InstanceId :: new ( ) ;
93
138
self . scenes_to_spawn . push ( ( id. into ( ) , instance_id) ) ;
94
139
self . scenes_with_parent . push ( ( instance_id, parent) ) ;
95
140
instance_id
96
141
}
97
142
143
+ /// Schedule the despawn of all instances of the provided dynamic scene.
98
144
pub fn despawn ( & mut self , id : impl Into < AssetId < DynamicScene > > ) {
99
145
self . scenes_to_despawn . push ( id. into ( ) ) ;
100
146
}
101
147
148
+ /// Schedule the despawn of a scene instance, removing all its entities from the world.
102
149
pub fn despawn_instance ( & mut self , instance_id : InstanceId ) {
103
150
self . instances_to_despawn . push ( instance_id) ;
104
151
}
105
152
153
+ /// Immediately despawns all instances of a dynamic scene.
106
154
pub fn despawn_sync (
107
155
& mut self ,
108
156
world : & mut World ,
@@ -116,6 +164,7 @@ impl SceneSpawner {
116
164
Ok ( ( ) )
117
165
}
118
166
167
+ /// Immediately despawns a scene instance, removing all its entities from the world.
119
168
pub fn despawn_instance_sync ( & mut self , world : & mut World , instance_id : & InstanceId ) {
120
169
if let Some ( instance) = self . spawned_instances . remove ( instance_id) {
121
170
for & entity in instance. entity_map . values ( ) {
@@ -124,6 +173,7 @@ impl SceneSpawner {
124
173
}
125
174
}
126
175
176
+ /// Immediately spawns a new instance of the provided dynamic scene.
127
177
pub fn spawn_dynamic_sync (
128
178
& mut self ,
129
179
world : & mut World ,
@@ -153,6 +203,7 @@ impl SceneSpawner {
153
203
} )
154
204
}
155
205
206
+ /// Immediately spawns a new instance of the provided scene.
156
207
pub fn spawn_sync (
157
208
& mut self ,
158
209
world : & mut World ,
@@ -182,6 +233,9 @@ impl SceneSpawner {
182
233
} )
183
234
}
184
235
236
+ /// Iterate through all instances of the provided scenes and update those immediately.
237
+ ///
238
+ /// Useful for updating already spawned scene instances after their corresponding scene has been modified.
185
239
pub fn update_spawned_scenes (
186
240
& mut self ,
187
241
world : & mut World ,
@@ -199,6 +253,7 @@ impl SceneSpawner {
199
253
Ok ( ( ) )
200
254
}
201
255
256
+ /// Immediately despawns all scenes scheduled for despawn by despawning their instances.
202
257
pub fn despawn_queued_scenes ( & mut self , world : & mut World ) -> Result < ( ) , SceneSpawnError > {
203
258
let scenes_to_despawn = std:: mem:: take ( & mut self . scenes_to_despawn ) ;
204
259
@@ -208,6 +263,7 @@ impl SceneSpawner {
208
263
Ok ( ( ) )
209
264
}
210
265
266
+ /// Immediately despawns all scene instances scheduled for despawn.
211
267
pub fn despawn_queued_instances ( & mut self , world : & mut World ) {
212
268
let instances_to_despawn = std:: mem:: take ( & mut self . instances_to_despawn ) ;
213
269
@@ -216,6 +272,7 @@ impl SceneSpawner {
216
272
}
217
273
}
218
274
275
+ /// Immediately spawns all scenes scheduled for spawn.
219
276
pub fn spawn_queued_scenes ( & mut self , world : & mut World ) -> Result < ( ) , SceneSpawnError > {
220
277
let scenes_to_spawn = std:: mem:: take ( & mut self . dynamic_scenes_to_spawn ) ;
221
278
@@ -308,6 +365,7 @@ impl SceneSpawner {
308
365
}
309
366
}
310
367
368
+ /// System that handles scheduled scene instance spawning and despawning through a [`SceneSpawner`].
311
369
pub fn scene_spawner_system ( world : & mut World ) {
312
370
world. resource_scope ( |world, mut scene_spawner : Mut < SceneSpawner > | {
313
371
// remove any loading instances where parent is deleted
0 commit comments