Skip to content

Repeated Timer that fires immediately after creation #21860

@kaoet

Description

@kaoet

What problem does this solve or what need does it fill?

I want to have an easy way to create a repeated Timer that fires immediately in the next tick, and then fires periodically.

So rather than

<---wait 10s--->FIRE<---wait 10s--->FIRE

I want

FIRE<---wait 10s--->FIRE<---wait 10s--->FIRE

What solution would you like?

An ergonomic API like:

let timer = Timer::from_seconds(10.0, TimerMode::Repeating).with_finish_immediately();

The actual function name can be discussed.

What alternative(s) have you considered?

Now I use this, which takes 3 lines rather than 1:

let timer = {
    let mut timer = Timer::from_seconds(10.0, TimerMode::Repeating);
    timer.tick(Duration::from_secs_f32(10.0 - EPSILON));
    timer
};

BTW, I've also tried this which doesn't work:

let timer = {
    let mut timer = Timer::from_seconds(10.0, TimerMode::Repeating);
    timer.finish();
    timer
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-TimeInvolves time keeping and reportingC-UsabilityA targeted quality-of-life change that makes Bevy easier to useD-StraightforwardSimple bug fixes and API improvements, docs, test and examplesS-Needs-DesignThis issue requires design work to think about how it would best be accomplishedX-UncontroversialThis work is generally agreed upon

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions