Page 1 of 1

mm2 timestamp usage

Posted: Thu Dec 17, 2015 7:28 pm
by lisa
Ok So I have been browsing the wiki and checking out the timestamp class, would this now be the preferred way to track times?

So in my current project I am still using the dt

Code: Select all

function macro.main(dt)
	gaptime = gaptime - dt
	walktime = walktime - dt
if gaptime < 0 and walktime < 0 then
Or is the timestamp class now the better option?

Didn't really find any examples of timestamp usage but there seems to be a few ways of going about it.

You could add the seconds to now time and then use Timestamp:isPast to see if the time is after what you are looking for.
You could add the seconds to now time and then use Timestamp:diffInSeconds in comparison to now time.
You could set now time to a variable and then use Timestamp:diffInSeconds to compare to now and see if the time you wanted has passed.

I am thinking for what I want the first option will work as it only has 1 variable to keep track of, you just set the time + the number of seconds added to it and then check if that time isPast or not.

Any advice on the way you intended it for use?

This is so different to just doing a yrest in the code, so much more to think about but at the end of the day it is a better way to do things.

Re: mm2 timestamp usage

Posted: Thu Dec 17, 2015 11:15 pm
by Administrator
Sure, it's really good for timing tasks, but only has second precision; not high-precision (sub-second). This might be changed in the future.

isPast() can be used to tell if it has elapsed (or "triggered", if you're using it to time a task), but you can also use < (isPast), <=, == (isNow), > (isFuture), or >= on timestamp objects. Open up lib/timestamp.lua and browse through the different functions.

Code: Select all

if( myTimestamp > Timestamp:now() ) then
    -- This happens when myTimestamp is in the future
end

Re: mm2 timestamp usage

Posted: Fri Dec 18, 2015 2:37 am
by lisa
testing with this atm, seems to be fine but this is very simple, just does a mouse click every 30 seconds.

Code: Select all

			if timer:isPast() then
				mouse.setVirtualPosition(win, clickw, clickh)
				mouse.virtualPress(win,  key.VK_LMOUSE,true)
				timer = Timestamp:now():addSeconds(waittime)
			end

Re: mm2 timestamp usage

Posted: Fri Dec 18, 2015 1:17 pm
by Administrator
If you're just doing simple operations like that, you can make use of the taskqueue (see: lib/taskqueue/), I know I've made a post about how that all works somewhere... It was kind of experimental and I haven't touched it in awhile, but might help to simplify your code further. Basically you create a task and register it with the queue; it will automatically handle launching the tasks for you.

Re: mm2 timestamp usage

Posted: Sat Dec 19, 2015 2:12 am
by lisa
Found it
http://www.solarstrike.net/phpBB3/viewt ... eue#p58747

and yeah that will pretty much do what I want ;)