Skip to content

Commit

Permalink
Book updates
Browse files Browse the repository at this point in the history
  • Loading branch information
AnneKitsune committed Jul 10, 2019
1 parent 76695d0 commit b0190bb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 36 deletions.
2 changes: 1 addition & 1 deletion amethyst_assets/src/prefab/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ mod tests {
world.add_resource(pool.clone());
world.add_resource(Loader::new(".", pool));
world.add_resource(Time::default());
let mut system = PrefabLoaderSystem::<MyPrefab>::default();
let mut system = PrefabLoaderSystem::<MyPrefab>::new(&mut world);
RunNow::setup(&mut system, &mut world.res);

let prefab = Prefab::new_main(Transform::default());
Expand Down
21 changes: 12 additions & 9 deletions book/src/concepts/event-channel.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ and you also need to get read access:
# }
```

Then, in the `System`'s setup method:
Then, in the `System`'s new method:
```rust,edition2018,no_run,noplaypen
# extern crate amethyst;
# use amethyst::shrev::{EventChannel, ReaderId};
Expand All @@ -183,15 +183,18 @@ Then, in the `System`'s setup method:
# A,
# B,
# }
# struct MySystem { reader: Option<ReaderId<MyEvent>>, }
# struct MySystem { reader: ReaderId<MyEvent>, }
#
# impl MySystem {
# pub fn new(world: &mut World) -> Self {
# <Self as System<'_>>::SystemData::setup(&mut world.res);
# Self {
# reader: world.res.fetch_mut::<EventChannel<MyEvent>>().register_reader(),
# }
#
# impl<'a> amethyst::ecs::System<'a> for MySystem {
# type SystemData = ();
# fn run(&mut self, _: Self::SystemData) { }
# fn setup(&mut self, res: &mut amethyst::ecs::Resources) {
// IMPORTANT: You need to setup your system data BEFORE you try to fetch the resource. Especially if you plan use `Default` to create your resource.
Self::SystemData::setup(res);
self.reader = Some(res.fetch_mut::<EventChannel<MyEvent>>().register_reader());
# }
# }
```
Expand All @@ -207,12 +210,12 @@ Finally, you can read events from your `System`.
# B,
# }
# struct MySystem {
# reader: Option<amethyst::shrev::ReaderId<MyEvent>>,
# reader: amethyst::shrev::ReaderId<MyEvent>,
# }
# impl<'a> amethyst::ecs::System<'a> for MySystem {
# type SystemData = Read<'a, EventChannel<MyEvent>>;
fn run(&mut self, my_event_channel: Self::SystemData) {
for event in my_event_channel.read(self.reader.as_mut().unwrap()) {
for event in my_event_channel.read(&mut self.reader) {
println!("Received an event: {:?}", event);
}
}
Expand Down
19 changes: 0 additions & 19 deletions book/src/concepts/system.md
Original file line number Diff line number Diff line change
Expand Up @@ -540,22 +540,3 @@ impl<'a> System<'a> for MyFirstSystem {
}
```

## The setup method

Systems have a method called setup which is called a single time, before any of the system runs.
Here is how to use it:

```rust,edition2018,no_run,noplaypen
# extern crate amethyst;
# use amethyst::ecs::{System, Resources, SystemData, Entity};
# struct MySystem { entity: Entity }
# impl<'a> System<'a> for MySystem {
# type SystemData = ();
# fn run(&mut self, _: Self::SystemData) { }
fn setup(&mut self, res: &mut Resources) {
// Ensures that resources that implement `Default` and are present in your `SystemData` are added to `Resources`.
Self::SystemData::setup(res);
// Do what you want with `Resources` past this point.
}
# }
```
16 changes: 9 additions & 7 deletions book/src/testing/test_examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,26 @@
# #[derive(Debug)]
# struct MySystem;
#
# impl MySystem {
# pub fn new(world: &mut World) -> Self {
# <Self as System<'_>>::SystemData::setup(&mut world.res);
# world.res.insert(ApplicationResource);
# Self
# }
#
# impl<'s> System<'s> for MySystem {
# type SystemData = ReadExpect<'s, ApplicationResource>;
#
# fn run(&mut self, _: Self::SystemData) {}
#
# fn setup(&mut self, res: &mut Resources) {
# Self::SystemData::setup(res);
# res.insert(ApplicationResource);
# }
# }
#
#[derive(Debug)]
struct MyBundle;
impl<'a, 'b> SystemBundle<'a, 'b> for MyBundle {
fn build(self, builder: &mut DispatcherBuilder<'a, 'b>) -> Result<(), Error> {
fn build(self, world: &mut World, builder: &mut DispatcherBuilder<'a, 'b>) -> Result<(), Error> {
// System that adds `ApplicationResource` to the `World`
builder.add(MySystem, "my_system", &[]);
builder.add(MySystem::new(&mut world), "my_system", &[]);
Ok(())
}
}
Expand Down

0 comments on commit b0190bb

Please sign in to comment.