Introducing Dragonfly Cloud! Learn More

Question: How do you implement delta time in GameMaker?

Answer

Delta time refers to the time that has passed since the last frame was rendered. In GameMaker, this can be incredibly useful for creating frame rate independent motion and animations. To use delta time in GameMaker, you typically access the delta_time built-in variable.

Here is an example of how you might use delta_time to move an object at a consistent speed regardless of the frame rate:

// Create Event speed_per_second = 100; // The speed at which you want the object to move, in pixels per second. // Step Event var dt = delta_time / 1000000; // Convert delta_time to seconds (since it's given in microseconds). x += speed_per_second * dt; // Move the object.

The delta_time variable holds the number of microseconds since the last step event, so by dividing by 1,000,000, you turn it into a value representing the number of seconds passed. This enables you to calculate movement or animations based on time rather than frames, which can lead to smoother and more consistent behavior across devices with different performance characteristics.

When using delta time, it's important to also consider any potential maximum step size to avoid issues with very large delta times (e.g., when your game is resuming from being paused). You can handle this by capping the delta time:

// Step Event var dt = delta_time / 1000000; var max_step = 0.05; // Maximum step size of 0.05 seconds dt = min(dt, max_step); x += speed_per_second * dt;

This ensures that even if a large delta time occurs, your object will not move an unexpectedly large distance.

Be aware that delta_time affects all time-based calculations, so you should use it consistently throughout all parts of your game that depend on timing for consistency.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.