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

Locals in exclusive systems #3946

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add example
  • Loading branch information
TheRawMeatball committed Feb 14, 2022
commit b7ff9a4784e5092703dd6de91c82e08cd6540de0
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ path = "examples/ecs/component_change_detection.rs"
name = "event"
path = "examples/ecs/event.rs"

[[example]]
name = "exclusive_system_tricks"
path = "examples/ecs/exclusive_system_tricks.rs"

[[example]]
name = "fixed_timestep"
path = "examples/ecs/fixed_timestep.rs"
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ Example | File | Description
`ecs_guide` | [`ecs/ecs_guide.rs`](./ecs/ecs_guide.rs) | Full guide to Bevy's ECS
`component_change_detection` | [`ecs/component_change_detection.rs`](./ecs/component_change_detection.rs) | Change detection on components
`event` | [`ecs/event.rs`](./ecs/event.rs) | Illustrates event creation, activation, and reception
`exclusive_system_tricks` | [`ecs/exclusive_system_tricks.rs`](./ecs/exclusive_system_tricks.rs) | A basic guide to using exclusive systems and exclusive world access.
`fixed_timestep` | [`ecs/fixed_timestep.rs`](./ecs/fixed_timestep.rs) | Shows how to create systems that run every fixed timestep, rather than every tick
`generic_system` | [`ecs/generic_system.rs`](./ecs/generic_system.rs) | Shows how to create systems that can be reused with different types
`hierarchy` | [`ecs/hierarchy.rs`](./ecs/hierarchy.rs) | Creates a hierarchy of parents and children entities
Expand Down
57 changes: 57 additions & 0 deletions examples/ecs/exclusive_system_tricks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use bevy::{
ecs::system::{lifetimeless::*, SystemState},
prelude::*,
};

fn main() {
App::new()
.init_resource::<MyCustomSchedule>()
.insert_resource(5u32)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use wrapper types, even in examples.

.add_system(simple_exclusive_system.exclusive_system())
.add_system(stateful_exclusive_system.exclusive_system())
.run();
}

#[derive(Default)]
struct MyCustomSchedule(Schedule);

#[derive(Component)]
struct MyComponent;

/// Just a simple exclusive system - this function will run with mutable access to
/// the main app world. This lets it call into other schedules, or modify and query the
/// world in hard-to-predict ways, which makes it a powerful primitive. However, because
/// this is usually not needed, and because such wide access makes parallelism impossible,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain why not -> should be avoided when not needed.

/// it should generally be avoided.
fn simple_exclusive_system(world: &mut World) {
world.resource_scope(|world, mut my_schedule: Mut<MyCustomSchedule>| {
// The resource_scope method is one of the main tools for working with &mut World.
// This method will temporarily remove the resource from the ECS world and let you
// access it while still keeping &mut World. A particularly popular pattern is storing
// schedules, stages, and other similar "runnables" in the world, taking them out
// using resource_scope, and running them with the world:
my_schedule.0.run(world);
// This is fairly simple, but you can implement rather complex custom executors in this manner.
});
}

/// While it's usually not recommended due to parallelism concerns, you can also use exclusive systems
/// as mostly-normal systems but with the ability to change parameter sets and flush commands midway through.
fn stateful_exclusive_system(
world: &mut World,
mut part_one_state: Local<SystemState<(SRes<u32>, SCommands)>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mut part_one_state: Local<SystemState<(SRes<u32>, SCommands)>>,
mut part_one_state: Local<SystemState<(Res<u32>, Commands)>>,

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately the SystemState API needs the lifetimeless versions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no. Please leave a comment explaining this then.

That surprises me a lot though; I request standard Res through the SystemState API in exclusive systems all the time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the problem is the systemstate api; it's that the Locals value must (correctly) be 'static, but a type with non-'static type paramaters cannot itself be `'static' (even though the lifetime cannot be used. I run into a similar issue in #3817.

That is, the issue is Local not SystemState; and the issue in Local is 'due to' our SystemParam design. I don't think this is avoidable.

mut part_two_state: Local<SystemState<SQuery<Read<MyComponent>>>>,
) {
let (resource, mut commands) = part_one_state.get(world);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh fascinating: this "just works" without initial values because of the FromWorld impl 🤔

let res = *resource as usize;
commands.spawn_batch((0..res).map(|_| (MyComponent,)));

// Don't forget to apply your state, or commands won't take effect!
part_one_state.apply(world);
let query = part_two_state.get(world);
let entity_count = query.iter().len();
// note how the entities spawned in this system are observed,
// and how resources fetched in earlier stages can still be
// used if they're cloned out, or small enough to copy out.
assert_eq!(entity_count, res);
}