Skip to content

Commit

Permalink
Allow optional main resource in ExtractResource
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Wilgenbus authored and regnarock committed Oct 13, 2023
1 parent 068e42a commit c30cb17
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions crates/bevy_render/src/extract_resource.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::marker::PhantomData;
use std::{marker::PhantomData};

use bevy_app::{App, Plugin};
use bevy_ecs::prelude::*;
Expand Down Expand Up @@ -40,24 +40,26 @@ impl<R: ExtractResource> Plugin for ExtractResourcePlugin<R> {
/// This system extracts the resource of the corresponding [`Resource`] type
pub fn extract_resource<R: ExtractResource>(
mut commands: Commands,
main_resource: Extract<Res<R::Source>>,
main_resource: Extract<Option<Res<R::Source>>>,
target_resource: Option<ResMut<R>>,
#[cfg(debug_assertions)] mut has_warned_on_remove: Local<bool>,
) {
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`.
if let Some(main_resource) = main_resource.as_ref() {
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>()
);
std::any::type_name::<R>()
);
}
commands.insert_resource(R::extract_resource(&main_resource));
}
commands.insert_resource(R::extract_resource(&main_resource));
}
}

0 comments on commit c30cb17

Please sign in to comment.