Timers?

Discuss, ask for help, share ideas, give suggestions, read tutorials, and tell us about bugs you have found with MicroMacro in here.

Do not post RoM-Bot stuff here. There is a subforum for that.
Forum rules
This is a sub-forum for things specific to MicroMacro.

This is not the place to ask questions about the RoM bot, which uses MicroMacro. There is a difference.
Post Reply
Message
Author
User avatar
3cmSailorfuku
Posts: 354
Joined: Mon Jan 21, 2008 6:25 pm

Timers?

#1 Post by 3cmSailorfuku » Thu May 15, 2008 4:48 pm

Ooohhh I got another Problem again and I dont know how to solve it.
My Script can't tell the difference between Allies and Foes and now I need to make a timeout without loosing its fast effiency.
I thought a timer would be good, but the documentation lacks a bit and im not sure how I should use it perfectly.

So basically it needs to drop the target after 5 seconds (By doing setMouse(0,0), if the mouse havent been moved during that time.
How should I done this?

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Timers?

#2 Post by Administrator » Thu May 15, 2008 9:59 pm

Every time you move the mouse, reset the timer and a tracking variable. You could use the old timer stuff, but automatic timers are much easier to work with (most of the work is taken care of for you, automatically). To set or reset a timer, just call registerTimer(). We will make a function to change the tracking variable to a true value, meaning it has been triggered. It should also unregister itself, so that it won't be called continuously until we manually stop it.

Code: Select all

__mouseTimedOut = false;          -- declare and initialize tracking variable. not completely necessary, just recommended.
function mouseTimeOut()
  __mouseTimedOut = true;         -- this is our tracking variable. true = timed out, false = not timed out
  unregisterTimer("unique name"); -- unregister me, so I'm not called again until reset.
end
We put this wherever we move the mouse and want to reset the time out timer.

Code: Select all

  __mouseTimedOut = false;
  registerTimer("unique name", secondsToTimer(10), mouseTimeOut);

Now, when we are moving/checking for the timeout, we can do something like this:

Code: Select all

while(active) do
  if( __mouseTimedOut ~= false ) -- if we are timed out...
    mouseSet(0, 0);       -- reset the mouse to 0,0
    __mouseTimedOut = false; -- reset the tracking variable
  end

  mouseSet(x, y); -- whatever you want here, really.
  registerTimer("unique name", secondsToTimer(10), mouseTimeOut);
  -- note: we don't need to set __mouseTimedOut to false here, because
  -- it was already taken care of before.

  coroutine.yield(); -- give the automatic timers time to process
  rest(1);              -- reduce CPU time
end


When it comes down to it, just use registerTimer() to call a function after a given amount of time. If registerTimer() is called again for that same unique timer (is given the same name [first argument]), it will cancel out the old timer and replace it. unregisterTimer() will remove it from the automatic timer list, and it will not call the given function. You *must* use coroutine.yield() to give automatic timers time to process, otherwise they will never be called. In the example above, all we really did was use a timed function to change a variable, which we then checked. You could have just as easily put setMouse(0, 0) into the timed function instead...but this way is more practical.

Just in case you wanted to use the old timers, just keep in mind they are not automatic. You must manually create, destroy, and check if they have been triggered.

Code: Select all

  -- outside your main loop. only call this once.
  newTimer("unique name");

  while( active ) do
    setMouse(x, y);
    startTimer("unique name", secondsToTimer(10));

    while( whatever ) do
      do_something();
    end
    
    if( isTriggered("unique name") )
      mouseSet(0, 0);
    end
  end

  removeTimer("unique name");

User avatar
3cmSailorfuku
Posts: 354
Joined: Mon Jan 21, 2008 6:25 pm

Re: Timers?

#3 Post by 3cmSailorfuku » Fri May 16, 2008 4:26 am

Hmm I think I was able to solve it on a different way. Gonna try it later.
And thank you, I understand timers more now. Though I still wasn't able to include it into my script so it works. Tsk, atleast it didn't gave me an error ;)

User avatar
3cmSailorfuku
Posts: 354
Joined: Mon Jan 21, 2008 6:25 pm

Re: Timers?

#4 Post by 3cmSailorfuku » Fri Aug 01, 2008 11:47 am

I guess I won't be able to avoid timers anymore so im going to revive this thread.
Evelrion, can you give me a more simple example? Just forgot everything above, like a variable hasn't changed for a minute.
I'm not able to reproduce anything from the example above x,x

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Timers?

#5 Post by Administrator » Fri Aug 01, 2008 4:06 pm

It's really this simple: you make a function that will be called every x miliseconds, you call registerTimer() to do some internal work allowing the function to be a timed function, and unregisterTimer() to cancel it out. You must use startMacro() to start your actual script execution, otherwise automatic timers do not work properly.

Code: Select all

function timed()
  printf("1 second passed\n");
end

function main()
  registerTimer("unique name", secondsToTimer(1), timed);

  while(1) do -- our main loop
    yrest(1); -- go as fast as possible without wasting up the CPU
  end

  unregisterTimer("unique name");
end

startMacro(main);

User avatar
3cmSailorfuku
Posts: 354
Joined: Mon Jan 21, 2008 6:25 pm

Re: Timers?

#6 Post by 3cmSailorfuku » Fri Aug 01, 2008 5:02 pm

elverion wrote:It's really this simple: you make a function that will be called every x miliseconds, you call registerTimer() to do some internal work allowing the function to be a timed function, and unregisterTimer() to cancel it out. You must use startMacro() to start your actual script execution, otherwise automatic timers do not work properly.

Code: Select all

function timed()
  printf("1 second passed\n");
end

function main()
  registerTimer("unique name", secondsToTimer(1), timed);

  while(1) do -- our main loop
    yrest(1); -- go as fast as possible without wasting up the CPU
  end

  registerTimer("unique name");
end

startMacro(main);
Doesn't seem to work, even after I tried to fix it. Simply won't call the timed function.

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Timers?

#7 Post by Administrator » Fri Aug 01, 2008 6:27 pm

Ah, yes. My mistake. The problem in that example was that coroutine.yield() was not being called (yrest only calls it every 100ms, so at 1ms it would not work), and is required to give automatic timers time to process. I'm going to update yrest() to always call coroutine.yield() at least once when applicable. If you add a call to coroutine.yield() below yrest(1), you'll see that it should work.

User avatar
3cmSailorfuku
Posts: 354
Joined: Mon Jan 21, 2008 6:25 pm

Re: Timers?

#8 Post by 3cmSailorfuku » Sat Aug 02, 2008 6:59 am

elverion wrote:Ah, yes. My mistake. The problem in that example was that coroutine.yield() was not being called (yrest only calls it every 100ms, so at 1ms it would not work), and is required to give automatic timers time to process. I'm going to update yrest() to always call coroutine.yield() at least once when applicable. If you add a call to coroutine.yield() below yrest(1), you'll see that it should work.
Works like a charm~ thank you :3

Can I somehow break the timer during my _Searching_ function, and recreate it during attacking? without wasting cpu?

edit: I solved that with an incremental variable that tells me how many times it attacked the monster already

zer0
Posts: 213
Joined: Sat Feb 16, 2008 11:55 pm

Re: Timers?

#9 Post by zer0 » Thu Aug 07, 2008 9:16 am

and just a minor correction the last code line in main() should be

Code: Select all

unregisterTimer("unique name")
if I'm not mistaken.. ;)

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Timers?

#10 Post by Administrator » Thu Aug 07, 2008 9:42 am

You, sir, are correct. I have edited my post to correct this, in case anybody else looks here for answers.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests