Allow optional extraction of resources from the main world (#10109)
# Objective
From my understanding, although resources are not meant to be created
and removed at every frame, they are still meant to be created
dynamically during the lifetime of the App.
But because the extract_resource API does not allow optional resources
from the main world, it's impossible to use resources in the render
phase that were not created before the render sub-app itself.
## Solution
Because the ECS engine already allows for system parameters to be
`Option<Res>`, it just had to be added.
---
## Changelog
- Changed
- `extract_resource` now takes an optional main world resource
- Fixed
- `ExtractResourcePlugin` doesn't cause panics anymore if the resource
is not already inserted
This commit is contained in:
parent
35073cf7aa
commit
4ae6a66481
@ -40,13 +40,14 @@ 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(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);
|
||||
*target_resource = R::extract_resource(main_resource);
|
||||
}
|
||||
} else {
|
||||
#[cfg(debug_assertions)]
|
||||
@ -58,6 +59,7 @@ pub fn extract_resource<R: ExtractResource>(
|
||||
std::any::type_name::<R>()
|
||||
);
|
||||
}
|
||||
commands.insert_resource(R::extract_resource(&main_resource));
|
||||
commands.insert_resource(R::extract_resource(main_resource));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user