Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Extract resources into their target location #5271

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ bevy_core = { path = "../bevy_core", version = "0.8.0-dev" }
bevy_derive = { path = "../bevy_derive", version = "0.8.0-dev" }
bevy_ecs = { path = "../bevy_ecs", version = "0.8.0-dev" }
bevy_encase_derive = { path = "../bevy_encase_derive", version = "0.8.0-dev" }
bevy_log = { path = "../bevy_log", version = "0.8.0-dev" }
bevy_math = { path = "../bevy_math", version = "0.8.0-dev" }
bevy_mikktspace = { path = "../bevy_mikktspace", version = "0.8.0-dev" }
bevy_reflect = { path = "../bevy_reflect", version = "0.8.0-dev", features = ["bevy"] }
Expand Down
24 changes: 19 additions & 5 deletions crates/bevy_render/src/extract_resource.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::marker::PhantomData;

use bevy_app::{App, Plugin};
use bevy_ecs::system::{Commands, Res, Resource};
use bevy_ecs::system::{Commands, Local, Res, ResMut, Resource};
pub use bevy_render_macros::ExtractResource;

use crate::{Extract, RenderApp, RenderStage};
Expand Down Expand Up @@ -38,12 +38,26 @@ impl<R: ExtractResource> Plugin for ExtractResourcePlugin<R> {
}

/// This system extracts the resource of the corresponding [`Resource`] type
/// by cloning it.
pub fn extract_resource<R: ExtractResource>(
mut commands: Commands,
resource: Extract<Res<R::Source>>,
main_resource: Extract<Res<R::Source>>,
target_resource: Option<ResMut<R>>,
#[cfg(debug_assertions)] mut has_warned_on_remove: Local<bool>,
) {
if resource.is_changed() {
commands.insert_resource(R::extract_resource(&*resource));
if let Some(mut target_resource) = target_resource {
if main_resource.is_changed() {
*target_resource = R::extract_resource(&*main_resource);
}
} else {
#[cfg(debug_assertions)]
if !main_resource.is_added() && !*has_warned_on_remove {
*has_warned_on_remove = true;
bevy_log::warn!(
"Removing resource {} from render world not expected, adding using `Commands`.
This may decrease performance",
std::any::type_name::<R>()
);
}
commands.insert_resource(R::extract_resource(&*main_resource));
}
}