Get milliseconds lapsed on different platforms (SOLVED)

#1

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?

0 Likes

#2

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

#3

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

2 Likes

#4

Will try this and report back.

0 Likes

#5

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
0 Likes

#6

Didn’t socket.get_time() work?

0 Likes

#7

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!

0 Likes

#8

The socket library is included in the engine core!

0 Likes

#9

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