Skip to content

Commit

Permalink
Adding the global thread pool to the state specific dispatcher examples
Browse files Browse the repository at this point in the history
  • Loading branch information
alec-deason committed May 14, 2019
1 parent 209f898 commit bc1fa61
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ PongSystemsBundle::default()
.expect("Failed to register PongSystemsBundle");
```

The `DispatcherBuilder` can be initialized and populated wherever desired, be it inside the `State` or in an external location. However, the `Dispatcher` needs to modify the `World`s resources in order to initialize the resources used by its `System`s. Therefore, we need to defer building the `Dispatcher` until we can access the `World`. This is commonly done in the `State`s `on_start` method. To showcase how this is done, we'll create a `SimpleState` with a `dispatcher` field and a `on_start` method that builds the `Dispatcher`.
The `DispatcherBuilder` can be initialized and populated wherever desired, be it inside the `State` or in an external location. However, the `Dispatcher` needs to modify the `World`s resources in order to initialize the resources used by its `System`s. Normally you will also want the `Dispatcher` to share the global thread pool which much be retrieved from the `World`. Therefore, we need to defer building the `Dispatcher` until we can access the `World`. This is commonly done in the `State`s `on_start` method. To showcase how this is done, we'll create a `SimpleState` with a `dispatcher` field and a `on_start` method that builds the `Dispatcher`.

```rust,edition2018,no_run,noplaypen
# external crate amethyst;
#
# use amethyst::{
# ecs::prelude::*,
# prelude::*,
# core::ArcThreadPool,
# };
#
#[derive(Default)]
Expand All @@ -74,7 +75,9 @@ impl<'a, 'b> SimpleState for CustomState<'a, 'b> {
dispatcher_builder.add(MovePaddlesSystem, "move_paddles_system", &[]);
// Build and setup the `Dispatcher`.
let mut dispatcher = dispatcher_builder.build();
let mut dispatcher = dispatcher_builder
.with_pool(world.read_resource::<ArcThreadPool>().clone())
.build();
dispatcher.setup(&mut world.res);
self.dispatcher = Some(dispatcher);
Expand Down

0 comments on commit bc1fa61

Please sign in to comment.