Skip to content

Commit ec34fe0

Browse files
authored
Finish documenting bevy_scene (#9949)
# Objective Finish documenting `bevy_scene`. ## Solution Document the remaining items and add a crate-level `warn(missing_doc)` attribute as for the other crates with completed documentation.
1 parent a1a81e5 commit ec34fe0

File tree

7 files changed

+164
-7
lines changed

7 files changed

+164
-7
lines changed

crates/bevy_scene/src/bundle.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,20 @@ pub struct SceneInstance(InstanceId);
2424
/// Once it's spawned, the entity will have a [`SceneInstance`] component.
2525
#[derive(Default, Bundle)]
2626
pub struct SceneBundle {
27-
/// Handle to the scene to spawn
27+
/// Handle to the scene to spawn.
2828
pub scene: Handle<Scene>,
29+
/// Transform of the scene root entity.
2930
pub transform: Transform,
31+
/// Global transform of the scene root entity.
3032
pub global_transform: GlobalTransform,
3133

34+
/// User-driven visibility of the scene root entity.
3235
#[cfg(feature = "bevy_render")]
3336
pub visibility: Visibility,
37+
/// Inherited visibility of the scene root entity.
3438
#[cfg(feature = "bevy_render")]
3539
pub inherited_visibility: InheritedVisibility,
40+
/// Algorithmically-computed visibility of the scene root entity for rendering.
3641
#[cfg(feature = "bevy_render")]
3742
pub view_visibility: ViewVisibility,
3843
}
@@ -43,15 +48,20 @@ pub struct SceneBundle {
4348
/// Once it's spawned, the entity will have a [`SceneInstance`] component.
4449
#[derive(Default, Bundle)]
4550
pub struct DynamicSceneBundle {
46-
/// Handle to the scene to spawn
51+
/// Handle to the scene to spawn.
4752
pub scene: Handle<DynamicScene>,
53+
/// Transform of the scene root entity.
4854
pub transform: Transform,
55+
/// Global transform of the scene root entity.
4956
pub global_transform: GlobalTransform,
5057

58+
/// User-driven visibility of the scene root entity.
5159
#[cfg(feature = "bevy_render")]
5260
pub visibility: Visibility,
61+
/// Inherited visibility of the scene root entity.
5362
#[cfg(feature = "bevy_render")]
5463
pub inherited_visibility: InheritedVisibility,
64+
/// Algorithmically-computed visibility of the scene root entity for rendering.
5565
#[cfg(feature = "bevy_render")]
5666
pub view_visibility: ViewVisibility,
5767
}

crates/bevy_scene/src/dynamic_scene.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ use serde::Serialize;
2626
/// [`GlobalTransform`](bevy_transform::components::GlobalTransform) components)
2727
#[derive(Asset, TypePath, Default)]
2828
pub struct DynamicScene {
29+
/// Resources stored in the dynamic scene.
2930
pub resources: Vec<Box<dyn Reflect>>,
31+
/// Entities contained in the dynamic scene.
3032
pub entities: Vec<DynamicEntity>,
3133
}
3234

crates/bevy_scene/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
//! Provides scene definition, instantiation and serialization/deserialization.
2+
//!
3+
//! Scenes are collections of entities and their associated components that can be
4+
//! instantiated or removed from a world to allow composition. Scenes can be serialized/deserialized,
5+
//! for example to save part of the world state to a file.
6+
17
#![allow(clippy::type_complexity)]
8+
#![warn(missing_docs)]
29

310
mod bundle;
411
mod dynamic_scene;
@@ -20,6 +27,7 @@ pub use scene_filter::*;
2027
pub use scene_loader::*;
2128
pub use scene_spawner::*;
2229

30+
#[allow(missing_docs)]
2331
pub mod prelude {
2432
#[doc(hidden)]
2533
pub use crate::{
@@ -31,6 +39,7 @@ pub mod prelude {
3139
use bevy_app::prelude::*;
3240
use bevy_asset::AssetApp;
3341

42+
/// Plugin that provides scene functionality to an [`App`].
3443
#[derive(Default)]
3544
pub struct ScenePlugin;
3645

crates/bevy_scene/src/scene.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ use bevy_utils::HashMap;
1515
/// [`GlobalTransform`](bevy_transform::components::GlobalTransform) components)
1616
#[derive(Asset, TypePath, Debug)]
1717
pub struct Scene {
18+
/// The world of the scene, containing its entities and resources.
1819
pub world: World,
1920
}
2021

2122
impl Scene {
23+
/// Creates a new scene with the given world.
2224
pub fn new(world: World) -> Self {
2325
Self { world }
2426
}

crates/bevy_scene/src/scene_loader.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use bevy_utils::BoxedFuture;
99
#[cfg(feature = "serialize")]
1010
use serde::de::DeserializeSeed;
1111

12+
/// [`AssetLoader`] for loading serialized Bevy scene files as [`DynamicScene`].
1213
#[derive(Debug)]
1314
pub struct SceneLoader {
1415
type_registry: TypeRegistryArc,

crates/bevy_scene/src/scene_spawner.rs

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct InstanceInfo {
2828
pub entity_map: HashMap<Entity, Entity>,
2929
}
3030

31+
/// Unique id identifying a scene instance.
3132
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
3233
pub struct InstanceId(Uuid);
3334

@@ -37,6 +38,25 @@ impl InstanceId {
3738
}
3839
}
3940

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)
4060
#[derive(Default, Resource)]
4161
pub struct SceneSpawner {
4262
spawned_scenes: HashMap<AssetId<Scene>, Vec<InstanceId>>,
@@ -50,27 +70,50 @@ pub struct SceneSpawner {
5070
scenes_with_parent: Vec<(InstanceId, Entity)>,
5171
}
5272

73+
/// Errors that can occur when spawning a scene.
5374
#[derive(Error, Debug)]
5475
pub enum SceneSpawnError {
76+
/// Scene contains an unregistered component type.
5577
#[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.
5783
#[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.
5989
#[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.
6195
#[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.
63101
#[error("scene does not exist")]
64-
NonExistentRealScene { id: AssetId<Scene> },
102+
NonExistentRealScene {
103+
/// Id of the non-existent scene.
104+
id: AssetId<Scene>,
105+
},
65106
}
66107

67108
impl SceneSpawner {
109+
/// Schedule the spawn of a new instance of the provided dynamic scene.
68110
pub fn spawn_dynamic(&mut self, id: impl Into<AssetId<DynamicScene>>) -> InstanceId {
69111
let instance_id = InstanceId::new();
70112
self.dynamic_scenes_to_spawn.push((id.into(), instance_id));
71113
instance_id
72114
}
73115

116+
/// Schedule the spawn of a new instance of the provided dynamic scene as a child of `parent`.
74117
pub fn spawn_dynamic_as_child(
75118
&mut self,
76119
id: impl Into<AssetId<DynamicScene>>,
@@ -82,27 +125,32 @@ impl SceneSpawner {
82125
instance_id
83126
}
84127

128+
/// Schedule the spawn of a new instance of the provided scene.
85129
pub fn spawn(&mut self, id: impl Into<AssetId<Scene>>) -> InstanceId {
86130
let instance_id = InstanceId::new();
87131
self.scenes_to_spawn.push((id.into(), instance_id));
88132
instance_id
89133
}
90134

135+
/// Schedule the spawn of a new instance of the provided scene as a child of `parent`.
91136
pub fn spawn_as_child(&mut self, id: impl Into<AssetId<Scene>>, parent: Entity) -> InstanceId {
92137
let instance_id = InstanceId::new();
93138
self.scenes_to_spawn.push((id.into(), instance_id));
94139
self.scenes_with_parent.push((instance_id, parent));
95140
instance_id
96141
}
97142

143+
/// Schedule the despawn of all instances of the provided dynamic scene.
98144
pub fn despawn(&mut self, id: impl Into<AssetId<DynamicScene>>) {
99145
self.scenes_to_despawn.push(id.into());
100146
}
101147

148+
/// Schedule the despawn of a scene instance, removing all its entities from the world.
102149
pub fn despawn_instance(&mut self, instance_id: InstanceId) {
103150
self.instances_to_despawn.push(instance_id);
104151
}
105152

153+
/// Immediately despawns all instances of a dynamic scene.
106154
pub fn despawn_sync(
107155
&mut self,
108156
world: &mut World,
@@ -116,6 +164,7 @@ impl SceneSpawner {
116164
Ok(())
117165
}
118166

167+
/// Immediately despawns a scene instance, removing all its entities from the world.
119168
pub fn despawn_instance_sync(&mut self, world: &mut World, instance_id: &InstanceId) {
120169
if let Some(instance) = self.spawned_instances.remove(instance_id) {
121170
for &entity in instance.entity_map.values() {
@@ -124,6 +173,7 @@ impl SceneSpawner {
124173
}
125174
}
126175

176+
/// Immediately spawns a new instance of the provided dynamic scene.
127177
pub fn spawn_dynamic_sync(
128178
&mut self,
129179
world: &mut World,
@@ -153,6 +203,7 @@ impl SceneSpawner {
153203
})
154204
}
155205

206+
/// Immediately spawns a new instance of the provided scene.
156207
pub fn spawn_sync(
157208
&mut self,
158209
world: &mut World,
@@ -182,6 +233,9 @@ impl SceneSpawner {
182233
})
183234
}
184235

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.
185239
pub fn update_spawned_scenes(
186240
&mut self,
187241
world: &mut World,
@@ -199,6 +253,7 @@ impl SceneSpawner {
199253
Ok(())
200254
}
201255

256+
/// Immediately despawns all scenes scheduled for despawn by despawning their instances.
202257
pub fn despawn_queued_scenes(&mut self, world: &mut World) -> Result<(), SceneSpawnError> {
203258
let scenes_to_despawn = std::mem::take(&mut self.scenes_to_despawn);
204259

@@ -208,6 +263,7 @@ impl SceneSpawner {
208263
Ok(())
209264
}
210265

266+
/// Immediately despawns all scene instances scheduled for despawn.
211267
pub fn despawn_queued_instances(&mut self, world: &mut World) {
212268
let instances_to_despawn = std::mem::take(&mut self.instances_to_despawn);
213269

@@ -216,6 +272,7 @@ impl SceneSpawner {
216272
}
217273
}
218274

275+
/// Immediately spawns all scenes scheduled for spawn.
219276
pub fn spawn_queued_scenes(&mut self, world: &mut World) -> Result<(), SceneSpawnError> {
220277
let scenes_to_spawn = std::mem::take(&mut self.dynamic_scenes_to_spawn);
221278

@@ -308,6 +365,7 @@ impl SceneSpawner {
308365
}
309366
}
310367

368+
/// System that handles scheduled scene instance spawning and despawning through a [`SceneSpawner`].
311369
pub fn scene_spawner_system(world: &mut World) {
312370
world.resource_scope(|world, mut scene_spawner: Mut<SceneSpawner>| {
313371
// remove any loading instances where parent is deleted

0 commit comments

Comments
 (0)