-
I have a Rust newbie question that I'm hoping is an easy answer for someone, but I've been digging around and I'm a bit stuck. I'm trying to mutably access a couple of resources in an exclusive system via struct ResourceA {
num: u32,
}
struct ResourceB {
num: u32,
}
pub fn exclusive_sys_1(world: &mut World) {
let mut res_a = world.resource_mut::<ResourceA>();
let mut res_b = world.resource_mut::<ResourceB>();
res_a.num += 1;
res_b.num += 1;
} which fails with:
Still wrapping my head around borrowing... seems that So, looking for another approach, I found pub unsafe fn exclusive_sys_2(world: &mut World) {
let res_a = &mut world
.get_resource_unchecked_mut::<ResourceA>()
.expect("must exist");
let res_b = &mut world
.get_resource_unchecked_mut::<ResourceB>()
.expect("must exist");
res_a.num += 1;
res_b.num += 1;
} in this case, the function itself seems to be fine (no compiler errors there), but it seems it's not possible to use an unsafe function as a bevy system, or at least not as an exclusive system:
I'm sure there's a ton I'm doing wrong, would love any tips or advice about how to mutably access multiple resources in an exclusive system, and/or other suggestions. Thanks! :-) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Oh oops, I just noticed |
Beta Was this translation helpful? Give feedback.
-
Your question is actually two questions, so here are the answers How to access multiple resources mutably at the same time on a
|
Beta Was this translation helpful? Give feedback.
Your question is actually two questions, so here are the answers
How to access multiple resources mutably at the same time on a
&mut World
There is two ways to go about it:
resource_scope
method onWorld
.This incurs rightward drift, but it's the quick and dirty solution
SystemState
to "emulate" a system parameter-style interface.I fin…