What are the differences between Dart's Timer
and Flame's Timer
? Since games are typically executed on a frame-by-frame basis, I assume there might be some relation (such as Time Scale
?), but I couldn't quite understand it from the documentation.
We can discuss about the Dart's timer vs Flame's timer like below points,
1. Purpose and Context
It was part of the dart core (dart.async)
.
It was made for typically pourpose such as async task for any dart application and keep to delay like that.
Independent of any frame base system or game loop.
Specilly design for games using flame game engine.
Tied to the game loop, where updates are called on a frame-by-frame basis.
Provides integration with game mechanics, such as respecting time scaling.
2. Time management
This was use real world time by system clock.
It was runing independently of the game loop.
It was not any affect for game speed or game scale.
Executes a callback once after a duration or at regular intervals, even if the frame rate changes.
Relies on the game's frame update cycle.
Progress is calculated based on the delta time (dt)
passed during each frame update, making it part of the game's simulated time.
Affected by time scaling in the game, allowing features like slowing down or pausing timers if the game speed changes.
3.Implementation
Manage by dart system.
import 'dart:async';
void main() {
Timer(Duration(seconds: 2), () => print("Timer fired!"));
}
Flame timer
import 'package:flame/timer.dart';
final timer = Timer(2.0, onTick: () => print("Timer fired!"));
void gameUpdate(double dt) { timer.update(dt); // Must be called every frame }
4.Flexibility
Simple and straightforward.
Best suited for non-game contexts or asynchronous tasks in apps.
Built with advanced game mechanics in mind.
Supports pausing, resuming, and resetting within the game context.
Automatically syncs with other game features like animations and physics, thanks to its integration with the game loop.
5.Example: Time scaling
Not affected by time scale. Runs based on real-world time:
Timer(Duration(seconds: 2), () => print("Real-time timer!"));
Respects the time scale set by the game engine:
// Assuming timeScale = 0.5 means half-speed game
final timer = Timer(2.0, onTick: () => print("Game-time timer!"));
void updateGame(double dt) {
timer.update(dt * timeScale); // Timer slows down when timeScale < 1
}
6.What kind of things can we use those timers.
Non-game contexts or background tasks.
Tasks that don’t depend on game time (e.g., network polling or reminders).
Timers that interact with or depend on the game world.
Features that require synchronization with the game loop or time scale (e.g., spawn timers, cooldowns, or animations).