Skip to content

Commit

Permalink
Remove AssetServer::watch_for_changes() (bevyengine#5968)
Browse files Browse the repository at this point in the history
# Objective
`AssetServer::watch_for_changes()` is racy and redundant with `AssetServerSettings`.
Closes bevyengine#5964.

## Changelog

* Remove `AssetServer::watch_for_changes()`
* Add `AssetServerSettings` to the prelude.
* Minor cleanup.

## Migration Guide
`AssetServer::watch_for_changes()` was removed.
Instead, use the `AssetServerSettings` resource.
```rust
app // AssetServerSettings must be inserted before adding the AssetPlugin or DefaultPlugins.
	.insert_resource(AssetServerSettings {
		watch_for_changes: true,
		..default()
	})
```


Co-authored-by: devil-ira <[email protected]>
  • Loading branch information
2 people authored and ItsDoot committed Feb 1, 2023
1 parent a3d49c6 commit 00ba707
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 25 deletions.
18 changes: 10 additions & 8 deletions crates/bevy_asset/src/asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,16 @@ pub struct AssetServerInternal {
///
/// The asset server is the primary way of loading assets in bevy. It keeps track of the load state
/// of the assets it manages and can even reload them from the filesystem with
/// [`AssetServer::watch_for_changes`]!
/// ```
/// # use bevy_asset::*;
/// # use bevy_app::*;
/// # let mut app = App::new();
/// // AssetServerSettings must be inserted before adding the AssetPlugin or DefaultPlugins.
/// app.insert_resource(AssetServerSettings {
/// watch_for_changes: true,
/// ..Default::default()
/// });
/// ```
///
/// The asset server is a _resource_, so in order to access it in a system you need a `Res`
/// accessor, like this:
Expand Down Expand Up @@ -169,13 +178,6 @@ impl AssetServer {
loaders.push(Arc::new(loader));
}

/// Enable watching of the filesystem for changes, if support is available, starting from after
/// the point of calling this function.
pub fn watch_for_changes(&self) -> Result<(), AssetServerError> {
self.asset_io().watch_for_changes()?;
Ok(())
}

/// Gets a strong handle for an asset with the provided id.
pub fn get_handle<T: Asset, I: Into<HandleId>>(&self, id: I) -> Handle<T> {
let sender = self.server.asset_ref_counter.channel.sender.clone();
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ mod path;
/// The `bevy_asset` prelude.
pub mod prelude {
#[doc(hidden)]
pub use crate::{AddAsset, AssetEvent, AssetServer, Assets, Handle, HandleUntyped};
pub use crate::{
AddAsset, AssetEvent, AssetServer, AssetServerSettings, Assets, Handle, HandleUntyped,
};
}

pub use anyhow::Error;
Expand Down
11 changes: 7 additions & 4 deletions examples/scene/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ use bevy::{prelude::*, tasks::IoTaskPool, utils::Duration};

fn main() {
App::new()
// This tells the AssetServer to watch for changes to assets.
// It enables our scenes to automatically reload in game when we modify their files.
// AssetServerSettings must be inserted before the DefaultPlugins are added.
.insert_resource(AssetServerSettings {
watch_for_changes: true,
..default()
})
.add_plugins(DefaultPlugins)
.register_type::<ComponentA>()
.register_type::<ComponentB>()
Expand Down Expand Up @@ -65,10 +72,6 @@ fn load_scene_system(mut commands: Commands, asset_server: Res<AssetServer>) {
scene: asset_server.load(SCENE_FILE_PATH),
..default()
});

// This tells the AssetServer to watch for changes to assets.
// It enables our scenes to automatically reload in game when we modify their files
asset_server.watch_for_changes().unwrap();
}

// This system logs all ComponentA components in our world. Try making a change to a ComponentA in
Expand Down
16 changes: 6 additions & 10 deletions examples/shader/post_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy::{
prelude::*,
reflect::TypeUuid,
render::{
camera::{Camera, RenderTarget},
camera::RenderTarget,
render_resource::{
AsBindGroup, Extent3d, ShaderRef, TextureDescriptor, TextureDimension, TextureFormat,
TextureUsages,
Expand All @@ -20,13 +20,12 @@ use bevy::{
};

fn main() {
let mut app = App::new();
app.add_plugins(DefaultPlugins)
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(Material2dPlugin::<PostProcessingMaterial>::default())
.add_startup_system(setup)
.add_system(main_camera_cube_rotator_system);

app.run();
.add_system(main_camera_cube_rotator_system)
.run();
}

/// Marks the first camera cube (rendered to a texture.)
Expand All @@ -40,11 +39,8 @@ fn setup(
mut post_processing_materials: ResMut<Assets<PostProcessingMaterial>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut images: ResMut<Assets<Image>>,
asset_server: Res<AssetServer>,
) {
asset_server.watch_for_changes().unwrap();

let window = windows.get_primary_mut().unwrap();
let window = windows.primary_mut();
let size = Extent3d {
width: window.physical_width(),
height: window.physical_height(),
Expand Down
1 change: 0 additions & 1 deletion examples/shader/shader_instancing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ pub struct CustomPipeline {
impl FromWorld for CustomPipeline {
fn from_world(world: &mut World) -> Self {
let asset_server = world.resource::<AssetServer>();
asset_server.watch_for_changes().unwrap();
let shader = asset_server.load("shaders/instancing.wgsl");

let mesh_pipeline = world.resource::<MeshPipeline>();
Expand Down
2 changes: 1 addition & 1 deletion examples/tools/scene_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! With no arguments it will load the `FieldHelmet` glTF model from the repository assets subdirectory.

use bevy::{
asset::{AssetServerSettings, LoadState},
asset::LoadState,
gltf::Gltf,
input::mouse::MouseMotion,
math::Vec3A,
Expand Down

0 comments on commit 00ba707

Please sign in to comment.