Skip to content

fix: Reset the tick count of a Timer on reset and start - #4241

Merged
ufrshubham merged 3 commits into
flame-engine:mainfrom
matthewalberto:fix/timer-reset-tick-count
Nov 21, 2026
Merged

ufrshubham merged 3 commits into
flame-engine:mainfrom
matthewalberto:fix/timer-reset-tick-count

Conversation

@matthewalberto

@matthewalberto matthewalberto commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

Timer.reset() now also resets the number of ticks counted towards tickCount, so start() and stop() do too, since they call it. Before, a Timer with tickCount that had ticked its limit stayed finished after start() (a TimerComponent with removeOnFinish was removed on its next update), and it ticked once more and stopped instead of ticking tickCount times again. A reset() part way through also kept the earlier ticks.

When the limit is reached, _callTicker used to call stop(). Since stop() now clears the ticks, that would make the timer report not finished right after finishing, so it sets current to 0 and pauses instead, which is what stop() did there before. One behaviour change: stop() on a timer that has used up its ticks now leaves it not finished, the same as stop() already does for a non-repeating timer. TimerComponent and SpawnComponent do not depend on the old behaviour.

After review, the tick is counted before onTick runs, so start() or reset() called from onTick restarts the count instead of being undone straight away. The tickCount note in doc/flame/other/util.md now says that start() and reset() restart the tick count and that stop() resets it too.

Testing: six new tests in test/timer_test.dart (restart with start(), reset() part way through, start() from onTick with tickCount: 3 and tickCount: 1, reset() from onTick, and stop() after the limit). The first two fail on main, the three onTick ones fail on the first commit of this PR, and all pass with the fix. I also broke each part of the original fix on its own (ticks not reset, stop() called again at the limit, no pause at the limit, current not set to 0 at the limit), and a test failed for each. The full packages/flame suite on Windows: 2536 passed on main and 2542 with this PR, with the same 10 failures on both, by name (8 in assets_cache_test.dart, 1 in text_layouting_test.dart and 1 in skills_lint_test.dart, which fail on Windows on untouched main). dart format and flutter analyze are clean for the changed files. Not tested: on Linux or macOS, or in a running game.

Checklist

  • I have followed the Contributor Guide when preparing my PR.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • [-] I have updated/added relevant examples in examples or docs.

Breaking Change?

  • Yes, this PR is a breaking change.
  • No, this PR is not a breaking change.

Related Issues

Closes #4240

Timer.reset, and so start and stop, now also reset the ticks counted
towards tickCount. Before, a Timer with tickCount stayed finished after
start, and ticked only once more before stopping again.

When the tick limit is reached the timer now sets current to 0 and pauses
instead of calling stop, so it still reports finished.

@ufrshubham ufrshubham left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, and for the thorough testing notes in the description! The core change is right. I found one off-by-one when the timer is restarted from its own onTick, and I'd like the new stop() behaviour documented. Details are in the inline comments.

Comment thread packages/flame/lib/src/timer.dart Resolved
Comment thread packages/flame/lib/src/timer.dart Resolved
The tick is now counted before onTick runs, so start() or reset() called
from onTick restarts the tick count instead of being undone straight away.

Adds tests for start() and reset() from onTick, and for stop() after the
tick limit, and documents that start(), reset() and stop() reset the
tick count.

@ufrshubham ufrshubham left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@ufrshubham
ufrshubham merged commit c4c6b3f into flame-engine:main Nov 21, 2026
9 checks passed
matthewalberto added a commit to matthewalberto/matthewalberto that referenced this pull request Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Timer with tickCount stays finished after start() or reset()

2 participants