Skip to content

Commit bfbcd47

Browse files
committed
Add default implementation of Serialize and Deserialize to Timer and Stopwatch (#6248)
# Objective Fixes #6244 ## Solution Uses derive to implement `Serialize` and `Deserialize` for `Timer` and `Stopwatch` ### Things to consider - Should fields such as `finished` and `times_finished_this_tick` in `Timer` be serialized? - Does `Countdown` and `PrintOnCompletionTimer` need to be serialized and deserialized? ## Changelog Added `Serialize` and `Deserialize` implementations to `Timer` and `Stopwatch`, `Countdown`.
1 parent 132e8fb commit bfbcd47

File tree

4 files changed

+8
-1
lines changed

4 files changed

+8
-1
lines changed

crates/bevy_internal/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ wav = ["bevy_audio/wav"]
4545
# Enable watching file system for asset hot reload
4646
filesystem_watcher = ["bevy_asset/filesystem_watcher"]
4747

48-
serialize = ["bevy_input/serialize", "bevy_window/serialize"]
48+
serialize = ["bevy_input/serialize", "bevy_time/serialize", "bevy_window/serialize"]
4949

5050
# Display server protocol support (X11 is enabled by default)
5151
wayland = ["bevy_winit/wayland"]

crates/bevy_time/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ repository = "https://github.com/bevyengine/bevy"
88
license = "MIT OR Apache-2.0"
99
keywords = ["bevy"]
1010

11+
[features]
12+
default = []
13+
serialize = ["serde"]
1114

1215
[dependencies]
1316
# bevy
@@ -18,3 +21,4 @@ bevy_utils = { path = "../bevy_utils", version = "0.9.0-dev" }
1821

1922
# other
2023
crossbeam-channel = "0.5.0"
24+
serde = { version = "1", features = ["derive"], optional = true }

crates/bevy_time/src/stopwatch.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use bevy_utils::Duration;
2424
/// assert_eq!(stopwatch.elapsed_secs(), 0.0);
2525
/// ```
2626
#[derive(Clone, Debug, Default, Reflect)]
27+
#[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))]
2728
#[reflect(Default)]
2829
pub struct Stopwatch {
2930
elapsed: Duration,

crates/bevy_time/src/timer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use bevy_utils::Duration;
1010
///
1111
/// Paused timers will not have elapsed time increased.
1212
#[derive(Clone, Debug, Default, Reflect)]
13+
#[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))]
1314
#[reflect(Default)]
1415
pub struct Timer {
1516
stopwatch: Stopwatch,
@@ -401,6 +402,7 @@ impl Timer {
401402

402403
/// Specifies [`Timer`] behavior.
403404
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Default, Reflect)]
405+
#[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))]
404406
#[reflect(Default)]
405407
pub enum TimerMode {
406408
/// Run once and stop.

0 commit comments

Comments
 (0)