๐Ÿ“ฆ bevyengine / bevy

๐Ÿ“„ hotpatching_systems.rs ยท 95 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95//! This example demonstrates how to hot patch systems.
//!
//! It needs to be run with the dioxus CLI:
//! ```sh
//! dx serve --hot-patch --example hotpatching_systems --features hotpatching
//! ```
//!
//! All systems are automatically hot patchable.
//!
//! You can change the text in the `update_text` system, or the color in the
//! `on_click` system, and those changes will be hotpatched into the running
//! application.
//!
//! It's also possible to make any function hot patchable by wrapping it with
//! `bevy::dev_tools::hotpatch::call`.

use std::time::Duration;

use bevy::{color::palettes, prelude::*};

fn main() {
    let (sender, receiver) = crossbeam_channel::unbounded::<()>();

    // This function is here to demonstrate how to make something hot patchable outside of a system
    // It uses a thread for simplicity but could be an async task, an asset loader, ...
    start_thread(receiver);

    App::new()
        .add_plugins(DefaultPlugins)
        .insert_resource(TaskSender(sender))
        .add_systems(Startup, setup)
        .add_systems(Update, update_text)
        .run();
}

fn update_text(mut text: Single<&mut Text>) {
    // Anything in the body of a system can be changed.
    // Changes to this string should be immediately visible in the example.
    text.0 = "before".to_string();
}

fn on_click(
    _click: On<Pointer<Click>>,
    mut color: Single<&mut TextColor>,
    task_sender: Res<TaskSender>,
) {
    // Observers are also hot patchable.
    // If you change this color and click on the text in the example, it will have the new color.
    color.0 = palettes::tailwind::RED_600.into();

    let _ = task_sender.0.send(());
}

#[derive(Resource)]
struct TaskSender(crossbeam_channel::Sender<()>);

fn setup(mut commands: Commands) {
    commands.spawn(Camera2d);

    commands
        .spawn((
            Node {
                width: percent(100),
                height: percent(100),
                align_items: AlignItems::Center,
                justify_content: JustifyContent::Center,
                flex_direction: FlexDirection::Column,
                ..default()
            },
            children![(
                Text::default(),
                TextFont {
                    font_size: 100.0,
                    ..default()
                },
            )],
        ))
        .observe(on_click);
}

fn start_thread(receiver: crossbeam_channel::Receiver<()>) {
    std::thread::spawn(move || {
        while receiver.recv().is_ok() {
            let start = bevy::platform::time::Instant::now();

            // You can also make any part outside of a system hot patchable by wrapping it
            // In this part, only the duration is hot patchable:
            let duration = bevy::app::hotpatch::call(|| Duration::from_secs(2));

            std::thread::sleep(duration);
            info!("done after {:?}", start.elapsed());
        }
    });
}