From 948d71917cf819041bb80cf7442199799c4ac50b Mon Sep 17 00:00:00 2001 From: Trashtalk217 Date: Mon, 12 Feb 2024 19:33:38 -0800 Subject: [PATCH] 0.13: One-Shot system Improvements (#909) Co-authored-by: Alice Cecile --- content/news/2024-02-03-bevy-0.13/index.md | 33 ++++++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/content/news/2024-02-03-bevy-0.13/index.md b/content/news/2024-02-03-bevy-0.13/index.md index 15bea03655..8ba0d94898 100644 --- a/content/news/2024-02-03-bevy-0.13/index.md +++ b/content/news/2024-02-03-bevy-0.13/index.md @@ -318,11 +318,38 @@ app.add_systems( If this new behavior does not work for you, please consult the migration guide. There are several new APIs that allow you to opt-out. -## Input for One-Shot Systems +## One-Shot Systems Improvements -
authors: @TODO
+
authors: @Nathan-Fenner
-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 input and output. + +```rust + +fn increment_sys(In(increment_by): In, mut counter: ResMut) -> i32 { + counter.0 += increment_by; + counter.0 +} + +let mut world = World::new(); +let id = world.register_system(increment_sys); + +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 0.12.1, but didn't get a blog post) and the ability to register exclusive systems as one-shot systems. + +```rust +world.register_system(|world: &mut World| { /* do anything */ }); +``` + +All these improvements round out one-shot systems significantly and they should now behave normally in any Bevy context. ## WGPU Upgrade