Skip to main content

Timer

Struct Timer 

Source
pub struct Timer(pub Rc<RefCell<TimerData>>);
Expand description

§Timer

A timer that counts down each frame. Can be paused and extended. Actions can be boundd to the timer that will run on completion.

Don’t put the timer in the loop

Timers won’t run by default, you must call add_timer() and pass in the timer to add them to the timer list. Or call spawn_timer(seconds) and it will run immediately.

let mut my_timer = add_timer(Timer::new(5.0));
// or
let mut my_timer = spawn_timer(5.0);
println!("Time left: {}", my_timer.time_left);
println!("Timer is paused: {}", my_timer.paused);
my_timer.extend(2.0); // extend by 2 seconds

if my_timer.completed() {
    // timer is completed
}

if my_timer.ongoing() {
    // timer is active
}

// Pause/Unpause a timer
my_timer.pause();
my_timer.unpause();
my_timer.set_paused(true);
my_timer.toggle_paused();

// Action will run if the timer is completed (must be called each frame)
my_timer.on_completion(|| {
    println!("Timer was completed!");
})

// Reset the time left to the original (does not include `extend()`)
my_timer.reset();

// Stop the timer (marks as completed())
my_timer.finish();

Once a timer is completed it will be removed from the timer list. The timer will stop ticking and on_completion() will stop firing.

// Re-add a timer to the timer list (REMEMBER TO RESET IT)
my_timer.reset();
add_timer(my_timer);

Tuple Fields§

§0: Rc<RefCell<TimerData>>

Implementations§

Source§

impl Timer

Source

pub fn new(seconds: f32) -> Self

Create a new timer using seconds

Source

pub fn new_repeating(seconds: f32) -> Self

Create a new repeating timer using seconds. Restarts after completing

Source

pub fn tick(&mut self, dt: f32)

Tick down the time left by dt (deltatime) only if it’s unpaused

Source

pub fn reset(&self)

Reset the timer to original time (does not include extend())

Source

pub fn finish(&self)

Sets time left to zero (completed) which may also remove the timer from the timer list

Source

pub fn pause(&self)

Pause the timer

Source

pub fn unpause(&self)

Unpause the timer

Source

pub fn set_paused(&self, paused: bool)

Set whether timer is paused or not

Source

pub fn toggle_paused(&self)

Toggle the timer paused/unpaused

Source

pub fn extend(&self, seconds: f32)

Extend (or decrease) the timer’s time left

Source

pub fn ongoing(&self) -> bool

Check if timer is ongoing (time left > 0)

Source

pub fn is_active(&self) -> bool

Check if timer is active (in the timer list)

Source

pub fn on_completion(&self, action: impl FnMut())

Runs an action if timer is completed (only runs if timer is in timer list)

Source

pub fn completed(&self) -> bool

Returns true if the timer was just completed this frame

Source

pub fn restart(&self) -> Timer ⓘ

Restarts the timer and adds it to the timer list

Source

pub fn completed_and_restart(&self) -> bool

Restarts the timer if it just got completed this frame, also returns whether its completed() or not

Source

pub fn start(&self) -> Timer ⓘ

Starts the timer (adds it to the timer list)

Source

pub async fn wait(&self)

Trait Implementations§

Source§

impl Clone for Timer

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Timer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Future for Timer

Source§

type Output = ()

The type of value produced on completion.
Source§

fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output>

Attempts to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more
Source§

impl PartialEq for Timer

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for Timer

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Timer

Auto Trait Implementations§

§

impl !RefUnwindSafe for Timer

§

impl !Send for Timer

§

impl !Sync for Timer

§

impl !UnwindSafe for Timer

§

impl Freeze for Timer

§

impl Unpin for Timer

§

impl UnsafeUnpin for Timer

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<F> FutureExt for F
where F: Future,

Source§

fn block_on(self) -> Self::Output
where Self: Sized,

Block the thread until the future is ready. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<F> IntoFuture for F
where F: Future,

Source§

type Output = <F as Future>::Output

The output that the future will produce on completion.
Source§

type IntoFuture = F

Which kind of future are we turning this into?
Source§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more