Recursive timer not firing

Posted by Areadien on Fri 16 Mar 2018 01:41 AM — 7 posts, 29,794 views.

USA #0
So I have Lua this function here, where, when I type the alias "test timer", "5\n", "4\n", "3\n", "2\n", "\1", is displayed, with each line being displayed 0.1 after the previous one, then display "done" when count is 0. Problem is, even though count is being decremented, the timer isn't working, and I don't know why. Any other similar timers I have are working fine. At first, I thought maybe that the timer called the function it's in was why it's not working, but I just tested that hypothesis by calling "testTimer2()" to call "testTimer()", but it's still now working. I even made the test_timer not be OneShot, but that didn't work either. What am I doing wrong?

local count = 5
function testTimer()
    ColourNote("blue", "black", count)
    count = count - 1
    if count == 0 then
        ColourNote("cyan", "black", "done")
    else
        AddTimer("test_timer", 0, 0, 0.1, "testTimer()", timer_flag.Enabled + timer_flag.OneShot)
        SetTimerOption("test_timer", "send_to", 12)
    end
end
Australia Forum Administrator #1
Can you show the alias?

Template:copying
For advice on how to copy aliases, timers or triggers from within MUSHclient, and paste them into a forum message, please see Copying XML.


Possibly the issue is that you are adding the timer test_timer when such a timer already exists.
Amended on Fri 16 Mar 2018 06:17 AM by Nick Gammon
USA #2
Yeah, it's

<alias enabled="y" match="^test timer$" regexp="y" send_to="12" sequence="100">
    <send>testTimer()</send>
</alias>


All this does is post 5 but nothing else.
Amended on Sun 18 Mar 2018 06:12 PM by Areadien
USA #3
OK, I figured out what was going on, but that raises two new questions. The timer wasn't working because I didn't have a connection to the game server. Is there any way I can make the timer work while I'm offline?

Also, setting timer_flag.OneShot doesn't make the timer fire until count equals 0; it just displays 5 and then 4, and then it stops. But not setting timer_flag.OneShot makes the timer go ad infinitum. How can I fix this problem?
Amended on Sun 18 Mar 2018 10:38 PM by Areadien
Australia Forum Administrator #4
If you add the "check" function call you can debug a bit better, like this:


   if count == 0 then
        ColourNote("cyan", "black", "done")
    else
        check (AddTimer("test_timer", 0, 0, 0.1, "testTimer()", timer_flag.Enabled + timer_flag.OneShot))
        SetTimerOption("test_timer", "send_to", 12)
    end



When I did that I saw, after two ticks:


Run-time error
World: smaug2
Immediate execution
[string "Script file"]:778: Attempt to add a timer that already exists
stack traceback:


I suggest adding the flag "timer_flag.Replace" to the other two flags you are using.

Also, nothing ever sets the count back to 5, so I suggest removing:


local count = 5


... from the script, and adding it to the alias, eg.


 <send>
count = 5
testTimer()
</send>


However your problem then is that if you ever use "count" somewhere else you may throw out this counter.

What might be better would be to make a timer that isn't one shot, and in the timer script check to see how many times it has fired, and when it has fired 4 times set it to one-shot then. That way you don't need an external counter.

GetTimerInfo (10) should tell you how many times the timer fired.
USA #5
Nick Gammon said:

If you add the "check" function call you can debug a bit better, like this:


   if count == 0 then
        ColourNote("cyan", "black", "done")
    else
        check (AddTimer("test_timer", 0, 0, 0.1, "testTimer()", timer_flag.Enabled + timer_flag.OneShot))
        SetTimerOption("test_timer", "send_to", 12)
    end



When I did that I saw, after two ticks:


Run-time error
World: smaug2
Immediate execution
[string "Script file"]:778: Attempt to add a timer that already exists
stack traceback:


I suggest adding the flag "timer_flag.Replace" to the other two flags you are using.


But I thought OneShot was supposed to delete the timer. Why doesn't it? Also, I tried adding timer_flag.Replace, but it didn't change anything. And I'm not sure how to use the check() function since I tried it and got nothing, not even when I did a ColourNote() with it.
Australia Forum Administrator #6
Yes, one-shot deletes the timer. After it has done its stuff. I can't delete something that is running code, so you can't delete the timer while it is running its script.

The check function just tests return codes from the scripting functions (not all of them work with it). However the ones that are documented to return an error code of some sort, and eOK for no error, can have "check" wrapped around them. If there is no error you don't see anything.