Stopwatch

Stopwatch

  • elapsed : bevy_utils::Duration
  • is_paused : bool

Description

A Stopwatch is a struct that tracks elapsed time when started.

Note that in order to advance the stopwatch tick MUST be called.

Examples

# use bevy_time::*;
use std::time::Duration;
let mut stopwatch = Stopwatch::new();
assert_eq!(stopwatch.elapsed_secs(), 0.0);

stopwatch.tick(Duration::from_secs_f32(1.0)); // tick one second
assert_eq!(stopwatch.elapsed_secs(), 1.0);

stopwatch.pause();
stopwatch.tick(Duration::from_secs_f32(1.0)); // paused stopwatches don't tick
assert_eq!(stopwatch.elapsed_secs(), 1.0);

stopwatch.reset(); // reset the stopwatch
assert!(stopwatch.is_paused());
assert_eq!(stopwatch.elapsed_secs(), 0.0);

Functions

FunctionSummary
assert_receiver_is_total_eq(_self)No Documentation 🚧
clone(_self)No Documentation 🚧
elapsed(_self) Returns the elapsed time since the last [`reset`](Stopwatch::reset) of the stopwatch. # Examples ``` # use bevy_time::*; use std::time::Duration; let mut stopwatch = Stopwatch::new(); stopwatch.tick(Duration::from_secs(1)); assert_eq!(stopwatch.elapsed(), Duration::from_secs(1)); ``` # See Also [`elapsed_secs`](Stopwatch::elapsed_secs)
elapsed_secs(_self) Returns the elapsed time since the last [`reset`](Stopwatch::reset) of the stopwatch, in seconds. # Examples ``` # use bevy_time::*; use std::time::Duration; let mut stopwatch = Stopwatch::new(); stopwatch.tick(Duration::from_secs(1)); assert_eq!(stopwatch.elapsed_secs(), 1.0); ``` # See Also [`elapsed`](Stopwatch::elapsed)
elapsed_secs_f64(_self) Returns the elapsed time since the last [`reset`](Stopwatch::reset) of the stopwatch, in seconds, as f64. # See Also [`elapsed`](Stopwatch::elapsed)
eq(_self, other)No Documentation 🚧
is_paused(_self) Returns `true` if the stopwatch is paused. # Examples ``` # use bevy_time::*; let mut stopwatch = Stopwatch::new(); assert!(!stopwatch.is_paused()); stopwatch.pause(); assert!(stopwatch.is_paused()); stopwatch.unpause(); assert!(!stopwatch.is_paused()); ```
new() Create a new unpaused `Stopwatch` with no elapsed time. # Examples ``` # use bevy_time::*; let stopwatch = Stopwatch::new(); assert_eq!(stopwatch.elapsed_secs(), 0.0); assert_eq!(stopwatch.is_paused(), false); ```
pause(_self) Pauses the stopwatch. Any call to [`tick`](Stopwatch::tick) while paused will not have any effect on the elapsed time. # Examples ``` # use bevy_time::*; use std::time::Duration; let mut stopwatch = Stopwatch::new(); stopwatch.pause(); stopwatch.tick(Duration::from_secs_f32(1.5)); assert!(stopwatch.is_paused()); assert_eq!(stopwatch.elapsed_secs(), 0.0); ```
reset(_self) Resets the stopwatch. The reset doesn't affect the paused state of the stopwatch. # Examples ``` # use bevy_time::*; use std::time::Duration; let mut stopwatch = Stopwatch::new(); stopwatch.tick(Duration::from_secs_f32(1.5)); stopwatch.reset(); assert_eq!(stopwatch.elapsed_secs(), 0.0); ```
set_elapsed(_self, time) Sets the elapsed time of the stopwatch. # Examples ``` # use bevy_time::*; use std::time::Duration; let mut stopwatch = Stopwatch::new(); stopwatch.set_elapsed(Duration::from_secs_f32(1.0)); assert_eq!(stopwatch.elapsed_secs(), 1.0); ```
unpause(_self) Unpauses the stopwatch. Resume the effect of ticking on elapsed time. # Examples ``` # use bevy_time::*; use std::time::Duration; let mut stopwatch = Stopwatch::new(); stopwatch.pause(); stopwatch.tick(Duration::from_secs_f32(1.0)); stopwatch.unpause(); stopwatch.tick(Duration::from_secs_f32(1.0)); assert!(!stopwatch.is_paused()); assert_eq!(stopwatch.elapsed_secs(), 1.0); ```