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

0.13: One-Shot system Improvements #909

Merged
Merged
Changes from 2 commits
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
33 changes: 30 additions & 3 deletions content/news/2024-02-03-bevy-0.13/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,38 @@ TODO.

TODO.

## Input for One-Shot Systems
## One-Shot Systems Improvements

<div class="release-feature-authors">authors: @TODO</div>
<div class="release-feature-authors">authors: @Nathan-Fenner</div>

TODO.
In 0.12, we introduced [one-shot systems](https://bevyengine.org/news/bevy-0-12/#one-shot-systems), a handy way to call systems on demand without having to add them to a schedule.
The initial implementation had some limitations with regards to what systems could and could not be used as one-shot systems.
These limitations have since been resolved, starting with one-shot systems with in- and output.
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved

```rust

fn increment_sys(In(increment_by): In<i32>, mut counter: ResMut<Counter>) -> i32 {
counter.0 += increment_by;
counter.0
}

let mut world = World::new();
let id = world.register_system(increment_sys);

Choose a reason for hiding this comment

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

The type of id is slightly different now, it's a SystemId<i32, i32>, (though plain SystemId still works and now means SystemId<(), ()>) which might be worth mentioning- possibly also making the in/out types different in the example to clearly show which is which.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the sake of brevity, I think I'll leave it out of the main post, but I like the idea of adding type annotations and maybe a comment to the code example. I'll do that later.


world.insert_resource(Counter(1));
let count_one = world.run_system_with_input(id, 5).unwrap(); // increment counter by 5 and return 6
let count_two = world.run_system_with_input(id, 2).unwrap(); // increment counter by 2 and return 8
```

Using either `world.run_system_with_input(system_id, input)` or `commands.run_system_with_input(system_id, input)`, you can now supply input parameters to systems that accept them. Additionally, both `world.run_system` and `world.run_system_with_input` now return system output as `Ok(output)`. Note that output cannot be returned when calling the system through commands, because of their deferred nature.

Some smaller improvements to one-shot systems include registering boxed systems with `register_boxed_system` (which was already possible since 12.1, but didn't get a blog post) and it being possible to register exclusive systems as one-shot systems.
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved

```rust
world.system_register(|world: &mut World| { /* do anything */ });
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
```

All these improvements round out one-shot systems significantly and they should now behave normally in any Bevy context.

## WGPU Upgrade

Expand Down
Loading