2023-09-19 20:17:05 +00:00
|
|
|
//! Demonstrates the use of "one-shot systems", which run once when triggered.
|
|
|
|
//!
|
|
|
|
//! These can be useful to help structure your logic in a push-based fashion,
|
|
|
|
//! reducing the overhead of running extremely rarely run systems
|
|
|
|
//! and improving schedule flexibility.
|
|
|
|
//!
|
2023-11-28 23:43:40 +00:00
|
|
|
//! See the [`World::run_system`](World::run_system) or
|
|
|
|
//! [`World::run_system_once`](World#method.run_system_once_with)
|
Fix intra-doc link warnings (#10445)
When `cargo doc -Zunstable-options -Zrustdoc-scrape-examples` (trying to
figure out why it doesn't work with bevy), I had the following warnings:
```
warning: unresolved link to `Quad`
--> examples/2d/mesh2d.rs:1:66
|
1 | //! Shows how to render a polygonal [`Mesh`], generated from a [`Quad`] primitive, in a 2D scene.
| ^^^^ no item named `Quad` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
= note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default
warning: `bevy` (example "mesh2d") generated 1 warning
warning: unresolved link to `update_weights`
--> examples/animation/morph_targets.rs:6:17
|
6 | //! See the [`update_weights`] system for details.
| ^^^^^^^^^^^^^^ no item named `update_weights` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
= note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default
warning: public documentation for `morph_targets` links to private item `name_morphs`
--> examples/animation/morph_targets.rs:7:43
|
7 | //! - How to read morph target names in [`name_morphs`].
| ^^^^^^^^^^^ this item is private
|
= note: this link will resolve properly if you pass `--document-private-items`
= note: `#[warn(rustdoc::private_intra_doc_links)]` on by default
warning: public documentation for `morph_targets` links to private item `setup_animations`
--> examples/animation/morph_targets.rs:8:48
|
8 | //! - How to play morph target animations in [`setup_animations`].
| ^^^^^^^^^^^^^^^^ this item is private
|
= note: this link will resolve properly if you pass `--document-private-items`
warning: `bevy` (example "morph_targets") generated 3 warnings
warning: unresolved link to `Quad`
--> examples/2d/mesh2d_vertex_color_texture.rs:1:66
|
1 | //! Shows how to render a polygonal [`Mesh`], generated from a [`Quad`] primitive, in a 2D scene.
| ^^^^ no item named `Quad` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
= note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default
warning: `bevy` (example "mesh2d_vertex_color_texture") generated 1 warning
warning: unresolved link to `UIScale`
--> examples/ui/ui_scaling.rs:1:36
|
1 | //! This example illustrates the [`UIScale`] resource from `bevy_ui`.
| ^^^^^^^ no item named `UIScale` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
= note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default
warning: `bevy` (example "ui_scaling") generated 1 warning
warning: unresolved link to `dependencies`
--> examples/app/headless.rs:5:6
|
5 | //! [dependencies]
| ^^^^^^^^^^^^ no item named `dependencies` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
= note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default
warning: `bevy` (example "headless") generated 1 warning
warning: unresolved link to `Material2d`
--> examples/2d/mesh2d_manual.rs:3:26
|
3 | //! It doesn't use the [`Material2d`] abstraction, but changes the vertex buffer to include verte...
| ^^^^^^^^^^ no item named `Material2d` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
= note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default
warning: `bevy` (example "mesh2d_manual") generated 1 warning
```
2023-11-08 14:33:46 +00:00
|
|
|
//! docs for more details.
|
2023-09-19 20:17:05 +00:00
|
|
|
|
|
|
|
use bevy::{
|
|
|
|
ecs::system::{RunSystemOnce, SystemId},
|
|
|
|
prelude::*,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::new()
|
2024-05-03 13:00:56 +00:00
|
|
|
.add_plugins(DefaultPlugins)
|
|
|
|
.add_systems(
|
|
|
|
Startup,
|
|
|
|
(
|
|
|
|
setup_ui,
|
|
|
|
setup_with_commands,
|
|
|
|
setup_with_world.after(setup_ui), // since we run `system_b` once in world it needs to run after `setup_ui`
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.add_systems(Update, (trigger_system, evaluate_callbacks).chain())
|
2023-09-19 20:17:05 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct Callback(SystemId);
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct Triggered;
|
|
|
|
|
2024-05-03 13:00:56 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
struct A;
|
|
|
|
#[derive(Component)]
|
|
|
|
struct B;
|
|
|
|
|
|
|
|
fn setup_with_commands(mut commands: Commands) {
|
|
|
|
let system_id = commands.register_one_shot_system(system_a);
|
|
|
|
commands.spawn((Callback(system_id), A));
|
2023-09-19 20:17:05 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 13:00:56 +00:00
|
|
|
fn setup_with_world(world: &mut World) {
|
|
|
|
// We can run it once manually
|
|
|
|
world.run_system_once(system_b);
|
|
|
|
// Or with a Callback
|
|
|
|
let system_id = world.register_system(system_b);
|
|
|
|
world.spawn((Callback(system_id), B));
|
2023-09-19 20:17:05 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 13:00:56 +00:00
|
|
|
/// Tag entities that have callbacks we want to run with the `Triggered` component.
|
|
|
|
fn trigger_system(
|
|
|
|
mut commands: Commands,
|
|
|
|
query_a: Query<Entity, With<A>>,
|
|
|
|
query_b: Query<Entity, With<B>>,
|
|
|
|
input: Res<ButtonInput<KeyCode>>,
|
|
|
|
) {
|
|
|
|
if input.just_pressed(KeyCode::KeyA) {
|
|
|
|
let entity = query_a.single();
|
|
|
|
commands.entity(entity).insert(Triggered);
|
|
|
|
}
|
|
|
|
if input.just_pressed(KeyCode::KeyB) {
|
|
|
|
let entity = query_b.single();
|
|
|
|
commands.entity(entity).insert(Triggered);
|
|
|
|
}
|
2023-09-19 20:17:05 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 13:00:56 +00:00
|
|
|
/// Runs the systems associated with each `Callback` component if the entity also has a `Triggered` component.
|
2023-09-19 20:17:05 +00:00
|
|
|
///
|
|
|
|
/// This could be done in an exclusive system rather than using `Commands` if preferred.
|
2024-05-03 13:00:56 +00:00
|
|
|
fn evaluate_callbacks(query: Query<(Entity, &Callback), With<Triggered>>, mut commands: Commands) {
|
|
|
|
for (entity, callback) in query.iter() {
|
2023-09-19 20:17:05 +00:00
|
|
|
commands.run_system(callback.0);
|
2024-05-03 13:00:56 +00:00
|
|
|
commands.entity(entity).remove::<Triggered>();
|
2023-09-19 20:17:05 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-03 13:00:56 +00:00
|
|
|
|
|
|
|
fn system_a(mut query: Query<&mut Text>) {
|
|
|
|
let mut text = query.single_mut();
|
|
|
|
text.sections[2].value = String::from("A");
|
|
|
|
info!("A: One shot system registered with Commands was triggered");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn system_b(mut query: Query<&mut Text>) {
|
|
|
|
let mut text = query.single_mut();
|
|
|
|
text.sections[2].value = String::from("B");
|
|
|
|
info!("B: One shot system registered with World was triggered");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup_ui(mut commands: Commands) {
|
|
|
|
commands.spawn(Camera2dBundle::default());
|
|
|
|
commands.spawn(
|
|
|
|
TextBundle::from_sections([
|
|
|
|
TextSection::new(
|
|
|
|
"Press A or B to trigger a one-shot system\n",
|
2024-06-20 21:01:28 +00:00
|
|
|
TextStyle::default(),
|
2024-05-03 13:00:56 +00:00
|
|
|
),
|
2024-05-31 16:41:27 +00:00
|
|
|
TextSection::new("Last Triggered: ", TextStyle::default()),
|
2024-05-03 13:00:56 +00:00
|
|
|
TextSection::new(
|
|
|
|
"-",
|
|
|
|
TextStyle {
|
|
|
|
color: bevy::color::palettes::css::ORANGE.into(),
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
),
|
|
|
|
])
|
|
|
|
.with_text_justify(JustifyText::Center)
|
|
|
|
.with_style(Style {
|
|
|
|
align_self: AlignSelf::Center,
|
|
|
|
justify_self: JustifySelf::Center,
|
|
|
|
..default()
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|