Timer

Timer

  • stopwatch : bevy_time::stopwatch::Stopwatch
  • duration : bevy_utils::Duration
  • mode : bevy_time::timer::TimerMode
  • finished : bool
  • times_finished_this_tick : u32

Description

Tracks elapsed time. Enters the finished state once duration is reached.

Non repeating timers will stop tracking and stay in the finished state until reset. Repeating timers will only be in the finished state on each tick duration is reached or exceeded, and can still be reset at any given point.

Paused timers will not have elapsed time increased.

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

Functions

FunctionSummary
assert_receiver_is_total_eq(_self)No Documentation 🚧
clone(_self)No Documentation 🚧
duration(_self) Returns the duration of the timer. # Examples ``` # use bevy_time::*; use std::time::Duration; let timer = Timer::new(Duration::from_secs(1), TimerMode::Once); assert_eq!(timer.duration(), Duration::from_secs(1)); ```
elapsed(_self) Returns the time elapsed on the timer. Guaranteed to be between 0.0 and `duration`. Will only equa
elapsed_secs(_self) Returns the time elapsed on the timer as an `f32`. See also [`Timer::elapsed`](Timer::elapsed).
elapsed_secs_f64(_self) Returns the time elapsed on the timer as an `f64`. See also [`Timer::elapsed`](Timer::elapsed).
eq(_self, other)No Documentation 🚧
finished(_self) Returns `true` if the timer has reached its duration. For repeating timers, this method behaves id
fraction(_self) Returns the fraction of the timer elapsed time (goes from 0.0 to 1.0). # Examples ``` # use bevy_time::*; use std::time::Duration; let mut timer = Timer::from_seconds(2.0, TimerMode::Once); timer.tick(Duration::from_secs_f32(0.5)); assert_eq!(timer.fraction(), 0.25); ```
fraction_remaining(_self) Returns the fraction of the timer remaining time (goes from 1.0 to 0.0). # Examples ``` # use bevy_time::*; use std::time::Duration; let mut timer = Timer::from_seconds(2.0, TimerMode::Once); timer.tick(Duration::from_secs_f32(0.5)); assert_eq!(timer.fraction_remaining(), 0.75); ```
from_seconds(duration, mode) Creates a new timer with a given duration in seconds. # Example ``` # use bevy_time::*; let mut timer = Timer::from_seconds(1.0, TimerMode::Once); ```
just_finished(_self) Returns `true` only on the tick the timer reached its duration. # Examples ``` # use bevy_time::*; use std::time::Duration; let mut timer = Timer::from_seconds(1.0, TimerMode::Once); timer.tick(Duration::from_secs_f32(1.5)); assert!(timer.just_finished()); timer.tick(Duration::from_secs_f32(0.5)); assert!(!timer.just_finished()); ```
mode(_self) Returns the mode of the timer. # Examples ``` # use bevy_time::*; let mut timer = Timer::from_seconds(1.0, TimerMode::Repeating); assert_eq!(timer.mode(), TimerMode::Repeating); ```
new(duration, mode) Creates a new timer with a given duration. See also [`Timer::from_seconds`](Timer::from_seconds).
pause(_self) Pauses the Timer. Disables the ticking of the timer. See also [`Stopwatch::pause`](Stopwatch::pause). # Examples ``` # use bevy_time::*; use std::time::Duration; let mut timer = Timer::from_seconds(1.0, TimerMode::Once); timer.pause(); timer.tick(Duration::from_secs_f32(0.5)); assert_eq!(timer.elapsed_secs(), 0.0); ```
paused(_self) Returns `true` if the timer is paused. See also [`Stopwatch::is_paused`](Stopwatch::is_paused). # Examples ``` # use bevy_time::*; let mut timer = Timer::from_seconds(1.0, TimerMode::Once); assert!(!timer.paused()); timer.pause(); assert!(timer.paused()); timer.unpause(); assert!(!timer.paused()); ```
remaining(_self) Returns the remaining time using Duration # Examples ``` # use bevy_time::*; use std::time::Duration; let mut timer = Timer::from_seconds(2.0, TimerMode::Once); timer.tick(Duration::from_secs_f32(0.5)); assert_eq!(timer.remaining(), Duration::from_secs_f32(1.5)); ```
remaining_secs(_self) Returns the remaining time in seconds # Examples ``` # use bevy_time::*; use std::cmp::Ordering; use std::time::Duration; let mut timer = Timer::from_seconds(2.0, TimerMode::Once); timer.tick(Duration::from_secs_f32(0.5)); let result = timer.remaining_secs().total_cmp(&1.5); assert_eq!(Ordering::Equal, result); ```
reset(_self) Resets the timer. The reset doesn't affect the `paused` state of the timer. See also [`Stopwatch::reset`](Stopwatch::reset)
set_duration(_self, duration) Sets the duration of the timer. # Examples ``` # use bevy_time::*; use std::time::Duration; let mut timer = Timer::from_seconds(1.5, TimerMode::Once); timer.set_duration(Duration::from_secs(1)); assert_eq!(timer.duration(), Duration::from_secs(1)); ```
set_elapsed(_self, time) Sets the elapsed time of the timer without any other considerations. See also [`Stopwatch::set`](Stopwatch::set). # ``` # use bevy_time::*; use std::time::Duration; let mut timer = Timer::from_seconds(1.0, TimerMode::Once); timer.set_elapsed(Duration::from_secs(2)); assert_eq!(timer.elapsed(), Duration::from_secs(2)); // the timer is not finished even if the elapsed time is greater than the duration. assert!(!timer.finished()); ```
set_mode(_self, mode) Sets the mode of the timer. # Examples ``` # use bevy_time::*; let mut timer = Timer::from_seconds(1.0, TimerMode::Repeating); timer.set_mode(TimerMode::Once); assert_eq!(timer.mode(), TimerMode::Once); ```
times_finished_this_tick(_self) Returns the number of times a repeating timer finished during the last [`tick`](Timer::tick) call. For non repeating-timers, this method will only ever return 0 or 1. # Examples ``` # use bevy_time::*; use std::time::Duration; let mut timer = Timer::from_seconds(1.0, TimerMode::Repeating); timer.tick(Duration::from_secs_f32(6.0)); assert_eq!(timer.times_finished_this_tick(), 6); timer.tick(Duration::from_secs_f32(2.0)); assert_eq!(timer.times_finished_this_tick(), 2); timer.tick(Duration::from_secs_f32(0.5)); assert_eq!(timer.times_finished_this_tick(), 0); ```
unpause(_self) Unpauses the Timer. Resumes the ticking of the timer. See also [`Stopwatch::unpause()`](Stopwatch::unpause). # Examples ``` # use bevy_time::*; use std::time::Duration; let mut timer = Timer::from_seconds(1.0, TimerMode::Once); timer.pause(); timer.tick(Duration::from_secs_f32(0.5)); timer.unpause(); timer.tick(Duration::from_secs_f32(0.5)); assert_eq!(timer.elapsed_secs(), 0.5); ```