My timer speed is going faster than the set delay (SOLVED)

Hello, I’m new to Defold! I’m having a little trouble with the timer api. Here is a screen of my little script:

Now, what is weird is that, my delay is 1 second so each print should happen every second. But these callbacks came all in less than one second. If I put the delay to 10 seconds, then the prints happen every second or so, sometimes they come a little faster or slower. Am I doing something wrong?

Here is a second test I made:

With a 0.1 sec timer and with this callback, I should print a new second each second. But instead, these 10 calls came instantly in one second.

I’m confused… If someone has any idea to help me, thank you!

First check in your video card settings that you can force enable VSync globally to test and see if that helps.

To confirm, try this simple script

function init(self)
	timer.delay(1, true, function()
		print("tick")
	end)
end

For me it does print every 1 second.

If forcing VSync doesn’t help / you can’t find it for your card, try unchecking VSync in the game.project and see what it does.

2 Likes

Activating VSync on my video card solved the problem, thank you very much!

1 Like

@Pkeod

If I may ask: When disabling vsync on the game project and then setting the frame cap to 60, will I get the desired effect of keeping the game at 60 fps? And will this be kept for all bundles?

Probably not, you can try to mitigate it but there is no one best solution for this issue currently. Most mobile devices shouldn’t have a problem. Some web users might have the problem. Some PCs will have the problem. It is a solvable problem but requires engine work by someone who understands the issues and knows how to deal with the cards which are currently not allowing VSync use as a setting (but still working when forcing it).

This is the setting we use in our projects, and for anyone who tells us the game runs fast we ask them to force enable VSync for the game.

[display]
width = 1366
height = 768
update_frequency = 60
high_dpi = 1
2 Likes

@Pkeod

If I tried something with os.date() and os.difftime() to get a constant 100ms tick clock, would that work? Since it is based on OS time rather than the game FPS. Or this is an awful idea?

socket.gettime() is the most precise timer you have availably from Lua.

1 Like

Oh, that is really nice. Also found it in the documantation:

t = socket.gettime()
-- do stuff
print(socket.gettime() - t .. " seconds elapsed")

Seems to be the one I will be using!

Thank you all for the help!