Timers?
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.
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.
- 3cmSailorfuku
- Posts: 354
- Joined: Mon Jan 21, 2008 6:25 pm
Timers?
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?
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?
- Administrator
- Site Admin
- Posts: 5318
- Joined: Sat Jan 05, 2008 4:21 pm
Re: Timers?
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.
We put this wherever we move the mouse and want to reset the time out timer.
Now, when we are moving/checking for the timeout, we can do something like this:
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
__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
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");
- 3cmSailorfuku
- Posts: 354
- Joined: Mon Jan 21, 2008 6:25 pm
Re: Timers?
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
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
- 3cmSailorfuku
- Posts: 354
- Joined: Mon Jan 21, 2008 6:25 pm
Re: Timers?
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
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
- Administrator
- Site Admin
- Posts: 5318
- Joined: Sat Jan 05, 2008 4:21 pm
Re: Timers?
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);
- 3cmSailorfuku
- Posts: 354
- Joined: Mon Jan 21, 2008 6:25 pm
Re: Timers?
Doesn't seem to work, even after I tried to fix it. Simply won't call the timed function.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);
- Administrator
- Site Admin
- Posts: 5318
- Joined: Sat Jan 05, 2008 4:21 pm
Re: Timers?
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.
- 3cmSailorfuku
- Posts: 354
- Joined: Mon Jan 21, 2008 6:25 pm
Re: Timers?
Works like a charm~ thank you :3elverion 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.
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
Re: Timers?
and just a minor correction the last code line in main() should be
if I'm not mistaken..
Code: Select all
unregisterTimer("unique name")
- Administrator
- Site Admin
- Posts: 5318
- Joined: Sat Jan 05, 2008 4:21 pm
Re: Timers?
You, sir, are correct. I have edited my post to correct this, in case anybody else looks here for answers.
Who is online
Users browsing this forum: No registered users and 0 guests