# Variable dt causes game to run twice as fast (DEF-3146) (SOLVED)

**URL:** <https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827>\
**Category:** Bugs\
**Created:** [January 16, 2018, 2:20pm UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827 "2018-01-16T14:20:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![connor.halford](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/connor.halford/32/9210_2.png) [@connor.halford](https://forum.defold.com/u/connor.halford)\
**Post date:** [January 16, 2018, 2:20pm UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/1 "2018-01-16T14:20:49Z")

</div>

Maybe I’m just not understanding what the ‘variable\_dt’ project setting does but this feels like a bug to me. Update frequency is set to 30 but if I tick the variable\_dt setting then the game seems to run twice at fast as if it were at the default update frequency of 60.

[https://forum.defold.com/uploads/default/original/2X/9/935504abaa9576a34b85195a6f816cdf64ee72e0.mp4](https://forum.defold.com/uploads/default/original/2X/9/935504abaa9576a34b85195a6f816cdf64ee72e0.mp4)

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [January 16, 2018, 3:14pm UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/2 "2018-01-16T15:14:01Z")

</div>

You will likely get more calls to update() but dt should be smaller and go.animate() and similar should still work as normal.

---

<div class="post-metadata">

**Author:** ![dapetcu21](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dapetcu21/32/25120_2.png) [@dapetcu21](https://forum.defold.com/u/dapetcu21)\
**Post date:** [February 16, 2018, 8:20pm UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/3 "2018-02-16T20:20:07Z")

</div>

I have encountered the same issue with my game. Disabling dynamic dt works around the issue. Also, I believe @Tomires and somebody else on Slack have the same issue.

Here’s my completely un-scientific findings:

Update frequency: 60  
Dynamic dt: checked

Windows Desktop:  
Windows 10, NVIDIA GTX 1060, 60Hz display: 60s in-game = 40s real time, 1.5x speedup

Windows Laptop #1:  
Windows 7, NVIDIA 610M, tried with both 60Hz internal display and 60Hz external display: 60s in-game = 55s real time, 1.09x speedup

Windows Laptop #2:  
Windows 10, Intel HD Graphics 620, 60Hz internal display: 60s in-game = 40s real time, 1.5x speedup  
Also tested HTML build in Chrome on this laptop: works as intended

MacBook:  
macOS 10.13.3, NVIDIA 650M, 60Hz internal display: works as intended

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [February 16, 2018, 8:30pm UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/4 "2018-02-16T20:30:19Z")

</div>

Thank you for taking the time to investigate this! This is definitely something we need to look into.

---

<div class="post-metadata">

**Author:** ![Tomires](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/tomires/32/3885_2.png) [@Tomires](https://forum.defold.com/u/Tomires)\
**Post date:** [February 16, 2018, 11:43pm UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/5 "2018-02-16T23:43:05Z")

</div>

Can confirm @dapetcu21 's findings, here are mine:

Windows 7, 2x GTX 880 - slightly faster (10%)  
Windows 10, HD 620 - 50% faster  
Windows 10, UHD 620 - 50% faster  
elementaryOS 0.4 Loki, HD 620 - works as intended  
macOS 10.12 Sierra, Iris 5100 - works as intended  
HTML across all tested devices - works as intended

All displays on tested machines are 60Hz. It seems like the issue is specific to Windows builds. It does not affect the timer native extension, only the animate methods and the update loop.

---

<div class="post-metadata">

**Author:** ![tacklemcclean](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/tacklemcclean/32/7634_2.png) [@tacklemcclean](https://forum.defold.com/u/tacklemcclean)\
**Post date:** [February 17, 2018, 12:03am UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/6 "2018-02-17T00:03:40Z")

</div>

I stopped investigating this once I activated variable dt for my game and I perceived it to be the same speed on my android phone and windows build.

But, I noticed that with variable dt unchecked (and all scenarios = 60fps), the game went a LOT faster on Windows than Android.

---

<div class="post-metadata">

**Author:** ![dapetcu21](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dapetcu21/32/25120_2.png) [@dapetcu21](https://forum.defold.com/u/dapetcu21)\
**Post date:** [February 23, 2018, 12:09am UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/7 "2018-02-23T00:09:04Z")

</div>

@britzl Hi! Sorry for insisting, but did this get an issue number? It’s pretty serious and reading the forum I see other people are having seemingly similar timing issues:

[https://forum.defold.com/t/defold-timer-the-native-timer-extension/6581/74](https://forum.defold.com/t/defold-timer-the-native-timer-extension/6581/74)

> [@Frames per second - different when using dt versus system clock](https://forum.defold.com/t/frames-per-second-different-when-using-dt-versus-system-clock/16853/1):
>
> Hello! I was having some genuine performance issues with my game (which have since gone away with some refactoring, and by realising that streaming in OBS using window capture and Windows Aero enabled is a baaaaaad idea), which highlighted something odd with how I am tracking frames per second. I have written two functions for tracking fps - one using the dt reported by the engine, and one using differences in os.clock(). The dt function reports 53 fps as the maximum framerate. The os.clock() …

Additionally, a colleague noticed that there are issues even when variable dt is turned off (animations with go.animate() go too slowly, while the dt we get in update goes at the right rate). I’ve yet to confirm this, though (I’ll only get to Win PC tomorrow). Might have been just frame skips.

---

<div class="post-metadata">

**Author:** ![Johan\_Beck-Noren](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/johan_beck-noren/32/2622_2.png) [@Johan\_Beck-Noren](https://forum.defold.com/u/Johan_Beck-Noren)\
**Post date:** [February 23, 2018, 7:30am UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/8 "2018-02-23T07:30:16Z")

</div>

When `variable_dt` is not checked the engine will rely on hardware vsync for each frame. The engine also assumes as a refresh rate of 60Hz.

With `variable_dt` checked the engine will update as fast as possible and calculate dt accordingly. This means that the size of dt can vary greatly between two frames which can be a problem for physics simulations for example.

From what is described in this thread and the fact that the issue seems to be isolated to Windows, there might be some precision issue with the dt calculation when running with `variable_dt` on Windows. Created issue DEF-3146 for this.

It is important to remember to use dt in your script update functions correctly. With `variable_dt` your update functions will most likely be called much more often, but with a smaller dt, than when running your game without `variable_dt`

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [February 23, 2018, 7:37am UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/9 "2018-02-23T07:37:03Z")

</div>

> [@dapetcu21](#):
>
> Sorry for insisting

Don’t be sorry. You’re right to insist! This is a critical issue and something we need to look into. Windows seems to be the problematic platform, and if I recall correctly there were differences between version (Win 7 and 10).

Johan created a ticket that hopefully will be picked up as soon as possible.

---

<div class="post-metadata">

**Author:** ![dapetcu21](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dapetcu21/32/25120_2.png) [@dapetcu21](https://forum.defold.com/u/dapetcu21)\
**Post date:** [February 23, 2018, 10:12am UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/10 "2018-02-23T10:12:33Z")

</div>

> [@Johan\_Beck-Noren](#):
>
> With `variable_dt` checked the engine will update as fast as possible

Wait, does that mean that checking `variable_dt` should turn off vsync? That doesn’t happen on OS X and that probably shouldn’t happen.

The way I understood `variable_dt` is that when it’s turned off:

- The game displays frames on vsync
- `update()` is called with a `dt` always equal to `1/update_frequency`
- `update()` might not be called every frame if the screen is drawn faster than `update_frequency`.
- `update()` might be called twice for a frame if the screen is drawn slower than `update_frequency`.

And when `variable_dt` is turned on:

- The game displays frames on vsync
- `update()` is called with a `dt` based on diffing system time.
- `update()` is only called exactly once per frame.

Did I understand correctly?

In which case, what happens in the following scenarios?

1. `variable_dt` off, `update_frequency = 60`, user’s display is 50Hz? Does the game run 1.2 times slower or does `update()` get called twice per frame once every fifth frame?
2. `variable_dt` off, `update_frequency = 30`, user’s display is 60Hz? Does `update()` only get called once every other frame?
3. `variable_dt` off, `update_frequency = 60`, user’s display is 60Hz, but his GPU/CPU is too slow and sometimes misses the vsync. Does the game still assume it’s running smoothly at 60Hz and the frame skips cause the animations to slow down? Or does it call `update()` twice after a missed vsync?

---

<div class="post-metadata">

**Author:** ![Johan\_Beck-Noren](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/johan_beck-noren/32/2622_2.png) [@Johan\_Beck-Noren](https://forum.defold.com/u/Johan_Beck-Noren)\
**Post date:** [February 23, 2018, 11:41am UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/11 "2018-02-23T11:41:11Z")

</div>

> [@dapetcu21](#):
>
> Wait, does that mean that checking variable\_dt should turn off vsync? That doesn’t happen on OS X and that probably shouldn’t happen.
> 
> The way I understood variable\_dt is that when it’s turned off:
> 
> The game displays frames on vsync
> 
> update() is called with a dt always equal to 1/update\_frequency
> 
> update() might not be called every frame if the screen is drawn faster than update\_frequency.
> 
> update() might be called twice for a frame if the screen is drawn slower than update\_frequency.
> 
> And when variable\_dt is turned on:
> 
> The game displays frames on vsync
> 
> update() is called with a dt based on diffing system time.
> 
> update() is only called exactly once per frame.
> 
> Did I understand correctly?

With `variable_dt` turned off the engine will set the GL swap interval to `60 / update_frequency` ([OpenGL set swap interval](https://www.khronos.org/opengl/wiki/Swap_Interval)) and will only call script `update()` at each display refresh, with dt set to `1/update_frequency`

With `variable_dt` turned on the engine does not care about vsync at all (vsync can be enabled/disabled in GPU drivers settings which we have no way of controlling) and will just update as fast as possible (calling script `update()` each time) with dt being calculated using platform timing functions . This might result in screen tearing ([screen tearing](https://en.wikipedia.org/wiki/Screen_tearing)) and big variations in dt if for example a fast frame is followed by a frame doing lots of heavy computations.

Defold does not have any decoupling between an engine update and calling script `update()` and we don’t skip frames to compensate for mismatching refresh rates, we rely entirely on hardware vsync (e.g. the combination of the set swap interval and the driver vsync-settings). A caveat; you can use [set\_vsync](https://www.defold.com/ref/sys/#set_vsync:swap_interval) message to achieve software vsync in defold.

Perhaps the documentation around this should be improved. Questions about, variable\_dt and vsync etc. pop up every now and then here on the forums. I’ll make a note of it!

Correct me if I’m wrong or missed anything @sven @Mathias_Westerdahl @Andreas_Tadic.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [February 23, 2018, 12:19pm UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/12 "2018-02-23T12:19:34Z")

</div>

> [@Johan\_Beck-Noren](#):
>
> Perhaps the documentation around this should be improved. Questions about, variable\_dt and vsync etc. pop up every now and then here on the forums. I’ll make a note of it!

Yes, your answer is probably the best summary of the impact of variable\_dt on and off that we have anywhere.

---

<div class="post-metadata">

**Author:** ![tacklemcclean](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/tacklemcclean/32/7634_2.png) [@tacklemcclean](https://forum.defold.com/u/tacklemcclean)\
**Post date:** [February 23, 2018, 1:55pm UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/13 "2018-02-23T13:55:48Z")

</div>

I’m using a 165hz monitor with nvidia g-sync, does this also have an effect? I have yet to sit down in my reading lounge with these last explanations so I’m a bit confused.

Edit: Just moving the running dmengine.exe from my 165hz monitor to my 60hz monitor speeds up and slows down the game immensly, so yes, it does have an effect 🙂

2 things I’m still confused about now is:

1. Is there a performance hit for settings `msg.post("@system:", "set_vsync", { swap_interval = 0 } )` ? I tried that, and that removes the “sped up” effect on my 165hz monitor, both monitors seems to act the same.  
This is with variable dt **unchecked** btw.

2. What settings gives the most similar/consistent experience between my dev pc and slower mobile phones that might drop below 60 fps here and there? I suppose the answer is dependant on what I define as similar experience..

Is it correct to assume that with variable dt, the game could actually slow down due to frame drops but not let the player “miss” anything, but without variable dt, the game could drop frames and the choppy gameplay could make it harder for the player to react to things, essentially “missing” stuff happening between the frames?

---

<div class="post-metadata">

**Author:** ![dapetcu21](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dapetcu21/32/25120_2.png) [@dapetcu21](https://forum.defold.com/u/dapetcu21)\
**Post date:** [February 23, 2018, 11:42pm UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/14 "2018-02-23T23:42:58Z")

</div>

The way I understood it:

WITHOUT variable dt, the game will slow down, but not let the player miss things. WITH variable dt, the game will run at constant speed, but frame skips might make the player miss things

Without variable dt, there is no way to make sure the game runs at the same speed if the display refresh rate is not 60Hz (Which makes this option a no-go for me), UNLESS you use swap\_interval = 0, which updates the game on a timer, ignoring vsync (which I believe might cause tearing).

With variable dt, you rely on whatever the driver setting for vsync is and the game will always run at the same speed. Also physics updates might be imprecise due to variable dt.

There is no performance penalty for swap\_interval = 0 except your FPS will be fixed at update\_frequency.

---

<div class="post-metadata">

**Author:** ![tacklemcclean](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/tacklemcclean/32/7634_2.png) [@tacklemcclean](https://forum.defold.com/u/tacklemcclean)\
**Post date:** [February 23, 2018, 11:54pm UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/15 "2018-02-23T23:54:44Z")

</div>

This makes it a bit clearer for me, but I’m not sure I’m 100% there.

I don’t want tearing, and I don’t want imprecise physics.

@devs, documentation suggestion, maybe have like a simple table with different approaches and their pro’s/con’s, and a comment on different types of games that would benefit from which type of approach?

---

<div class="post-metadata">

**Author:** ![tacklemcclean](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/tacklemcclean/32/7634_2.png) [@tacklemcclean](https://forum.defold.com/u/tacklemcclean)\
**Post date:** [March 6, 2018, 10:03pm UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/16 "2018-03-06T22:03:51Z")

</div>

I’m working on my game on Windows, which runs the game slightly faster than on my Galaxy S7.

I noticed that if I just bump up the timestep on my collection proxy, the game feels a lot better (like on my computer), so I think I’m going to lower the timestep instead during development, adjust gameplay feel accordingly, and then put it to 1.0 when finishing up.

It’s not hard to do since I’m already tweaking timestep due to gameplay features. Only drawback off the bat is that the ratio between “full speed” and slowed down events are off if I keep switching the full speed timestep value and not the others.  
The “others” being for example slow down on player death or dramatic events in game.

---

<div class="post-metadata">

**Author:** ![sicher](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sicher/32/8_2.png) [@sicher](https://forum.defold.com/u/sicher)\
**Post date:** [March 7, 2018, 6:29am UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/17 "2018-03-07T06:29:28Z")

</div>

> [@tacklemcclean](#):
>
> @devs, documentation suggestion, maybe have like a simple table with different approaches and their pro’s/con’s, and a comment on different types of games that would benefit from which type of approach?

That is a great idea.

---

<div class="post-metadata">

**Author:** ![dapetcu21](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dapetcu21/32/25120_2.png) [@dapetcu21](https://forum.defold.com/u/dapetcu21)\
**Post date:** [March 22, 2018, 12:12pm UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/18 "2018-03-22T12:12:49Z")

</div>

In the meantime, I made this work-around to the `variable_dt` on Windows bug. It measures game time and real time for an interval of 1 second and tells you the time step you should use in your collection proxies.

> **[GitHub - dapetcu21/defold-dtfixup: Workaround for a Defold bug](https://github.com/dapetcu21/defold-dtfixup)**
>
> Workaround for a Defold bug

---

<div class="post-metadata">

**Author:** ![dapetcu21](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dapetcu21/32/25120_2.png) [@dapetcu21](https://forum.defold.com/u/dapetcu21)\
**Post date:** [April 17, 2018, 7:10am UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/19 "2018-04-17T07:10:28Z")

</div>

Hi! Any news on this? @Johan_Beck-Noren? @britzl? It’s a pretty serious bug and my workaround is not perfect.

---

<div class="post-metadata">

**Author:** ![Johan\_Beck-Noren](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/johan_beck-noren/32/2622_2.png) [@Johan\_Beck-Noren](https://forum.defold.com/u/Johan_Beck-Noren)\
**Post date:** [April 17, 2018, 8:04am UTC](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827/20 "2018-04-17T08:04:15Z")

</div>

We added ticket DEF-3146 for tracking this issue, but no progress so far I’m sorry to say.

As a first step we talked about better documenting how vsync, variable\_dt and monitor refresh rate play together in Defold.

As the problem is described in this thread it definitely seems like a problem with how we measure time, perhaps too low os timer resolution. We are working on it!

[Next page](https://forum.defold.com/t/variable-dt-causes-game-to-run-twice-as-fast-def-3146-solved/15827.md?page=2)
