Get milliseconds lapsed on different platforms (SOLVED)

Using os.clock() in the editor and HTML5 give quite different results; HTML5 seems to be a order of magnitude higher.

I found this thread on Github:

Which would translate to including this quite over-elaborate library:

What’s the best way to get cross-platform accuracy when it comes to millisecond timers in Defold?

You can try something like this

sysinfo = sys.get_sys_info()

if sysinfo.system_name == "HTML5" then
    timestamp = html5.run("Date.now()")
else
    timestamp = whatever you are currently doing
end

(But I don’t generally write Lua so someone please correct me if I’m wrong)

1 Like

I would say that it is socket.gettime(). I’m not aware that this would give worse results on HTML5 builds.

2 Likes

Will try this and report back.

In the end I solved it like this :innocent::

local drag_duration = os.clock()-start_time

if sys.get_sys_info().system_name == "HTML5" then
	drag_duration = drag_duration / 10
end

Didn’t socket.get_time() work?

I didn’t try it in the end, because in my tests the HTML5 version was exactly 10 times off the editor. I didn’t want to include a fairly large library just to be able to get the time. Trying to save some download time. Facebook Instant, yo!

The socket library is included in the engine core!

Face palm. Will try this!

Edit: Yes, socket.get_time() works great! It returns the expected time in both the editor and HTML5.

1 Like