Difference between revisions of "Manual:Timer Engine"

From Mudlet
Jump to navigation Jump to search
(→‎Timer Engine: fixed syntax highlighting)
Line 10: Line 10:
 
You can also enable/disable this timer via scripting with [[Manual:Mudlet_Object_Functions#enableTimer|enableTimer()]] and [[Manual:Mudlet_Object_Functions#disableTimer|disableTimer()]]:
 
You can also enable/disable this timer via scripting with [[Manual:Mudlet_Object_Functions#enableTimer|enableTimer()]] and [[Manual:Mudlet_Object_Functions#disableTimer|disableTimer()]]:
  
<lua>
+
<syntaxhighlight lang="lua">
 
enableTimer("Newbie timer") -- enables the timer, so 2s after being enabled, it'll tick - and every 2s after that
 
enableTimer("Newbie timer") -- enables the timer, so 2s after being enabled, it'll tick - and every 2s after that
  
 
disableTimer("Newbie timer") -- disables it, so it won't go off anymore
 
disableTimer("Newbie timer") -- disables it, so it won't go off anymore
</lua>
+
</syntaxhighlight>
  
 
==Temporary Timers==
 
==Temporary Timers==
Line 23: Line 23:
 
One thing to keep in mind when working with tempTimers is that they are unlike #wait statements you might see in other clients. While #waits are typically cumulative, tempTimers aren't - they go off from the same point in time. Thus if you'd like two timers to go off - one after 3s, and another 1s after the first one - you'd set the second timer to go off at 4s. For example:
 
One thing to keep in mind when working with tempTimers is that they are unlike #wait statements you might see in other clients. While #waits are typically cumulative, tempTimers aren't - they go off from the same point in time. Thus if you'd like two timers to go off - one after 3s, and another 1s after the first one - you'd set the second timer to go off at 4s. For example:
  
<lua>
+
<syntaxhighlight lang="lua">
 
tempTimer(3, [[ echo("this is timer #1 going off 3s after being made\n") ]])
 
tempTimer(3, [[ echo("this is timer #1 going off 3s after being made\n") ]])
 
tempTimer(4, [[ echo("this is timer #2 going off 4s after being made - and 1s after the first one\n") ]])
 
tempTimer(4, [[ echo("this is timer #2 going off 4s after being made - and 1s after the first one\n") ]])
</lua>
+
</syntaxhighlight>
  
 
{{note}} Temporary timers cannot be accessed from the GUI and are not saved in profiles.
 
{{note}} Temporary timers cannot be accessed from the GUI and are not saved in profiles.
Line 33: Line 33:
 
To stop a temporary timer once you've made it and before before it went off - you use [[Manual:Mudlet_Object_Functions#killTimer|killTimer()]]. You give killTimer(id) the ID of the timer that you've made - which you get from Mudlet when you make it. Here's an example:
 
To stop a temporary timer once you've made it and before before it went off - you use [[Manual:Mudlet_Object_Functions#killTimer|killTimer()]]. You give killTimer(id) the ID of the timer that you've made - which you get from Mudlet when you make it. Here's an example:
  
<lua>
+
<syntaxhighlight lang="lua">
 
-- get and store the timer ID in the global "greeting_timer_id" variable
 
-- get and store the timer ID in the global "greeting_timer_id" variable
 
greeting_timer_id = tempTimer(2, [[ echo("hello!\n") ]])
 
greeting_timer_id = tempTimer(2, [[ echo("hello!\n") ]])
Line 39: Line 39:
 
-- delete the timer - thus nothing will actually happen!
 
-- delete the timer - thus nothing will actually happen!
 
killTimer(greeting_timer_id)
 
killTimer(greeting_timer_id)
</lua>
+
</syntaxhighlight>
  
 
===Refreshing===
 
===Refreshing===
 
You can also use [[Manual:Mudlet_Object_Functions#killTimer|killTimer()]] to "refresh" a timer - the following code example will delete the previous timer if one exists and create the new one, thus making sure you don't get multiple copies of it:
 
You can also use [[Manual:Mudlet_Object_Functions#killTimer|killTimer()]] to "refresh" a timer - the following code example will delete the previous timer if one exists and create the new one, thus making sure you don't get multiple copies of it:
  
<lua>
+
<syntaxhighlight lang="lua">
 
if portal_timer then killTimer(portal_timer) end
 
if portal_timer then killTimer(portal_timer) end
 
portal_timer = tempTimer(2, [[ send("enter portal") ]])
 
portal_timer = tempTimer(2, [[ send("enter portal") ]])
</lua>
+
</syntaxhighlight>
  
 
===Using variables===
 
===Using variables===
 
To embed a value of a variable in tempTimer code, you might try using this given what you've learnt:
 
To embed a value of a variable in tempTimer code, you might try using this given what you've learnt:
  
<lua>
+
<syntaxhighlight lang="lua">
 
tempTimer(1.4, [[ echo("hello, "..matches[2].."!\n") ]])
 
tempTimer(1.4, [[ echo("hello, "..matches[2].."!\n") ]])
</lua>
+
</syntaxhighlight>
  
 
But that won't work as you'd expect it to - that will try and use the value of matches[2] ''when the timer goes off'' - while by then, the variable could have changed! Instead, you take it outside the square [[ ]] brackets - this is correct:
 
But that won't work as you'd expect it to - that will try and use the value of matches[2] ''when the timer goes off'' - while by then, the variable could have changed! Instead, you take it outside the square [[ ]] brackets - this is correct:
  
<lua>
+
<syntaxhighlight lang="lua">
 
tempTimer(1.4, [[ echo("hello, ]]..matches[2]..[[!\n") ]])
 
tempTimer(1.4, [[ echo("hello, ]]..matches[2]..[[!\n") ]])
</lua>
+
</syntaxhighlight>
  
 
===Nesting===
 
===Nesting===
 
If you'd like, you can also nest tempTimers one inside another - though the first [[]]'s will become [=[ ]=]:
 
If you'd like, you can also nest tempTimers one inside another - though the first [[]]'s will become [=[ ]=]:
  
<lua>
+
<syntaxhighlight lang="lua">
 
tempTimer(1, [=[
 
tempTimer(1, [=[
 
   echo("this is timer #1 reporting, 1s after being made!\n")
 
   echo("this is timer #1 reporting, 1s after being made!\n")
Line 72: Line 72:
 
   ]])
 
   ]])
 
]=])
 
]=])
</lua>
+
</syntaxhighlight>
  
 
If you'd like to nest more of them, you'd increase the amount of ='s on the outside:
 
If you'd like to nest more of them, you'd increase the amount of ='s on the outside:
  
<lua>
+
<syntaxhighlight lang="lua">
 
tempTimer(1, [==[
 
tempTimer(1, [==[
 
   echo("this is timer #1 reporting, 1s after being made!\n")
 
   echo("this is timer #1 reporting, 1s after being made!\n")
Line 87: Line 87:
 
   ]=])
 
   ]=])
 
]==])
 
]==])
</lua>
+
</syntaxhighlight>
  
 
===Closures===
 
===Closures===
 
Last but not least, you can also use [http://www.lua.org/pil/6.1.html closures] with tempTimer - using a slightly different syntax that has advantages of being able to access variables in it's scope, when it goes off:
 
Last but not least, you can also use [http://www.lua.org/pil/6.1.html closures] with tempTimer - using a slightly different syntax that has advantages of being able to access variables in it's scope, when it goes off:
  
<lua>
+
<syntaxhighlight lang="lua">
 
local name = matches[2]
 
local name = matches[2]
 
tempTimer(2.4, function() echo("hello, "..name.."!\n") end)
 
tempTimer(2.4, function() echo("hello, "..name.."!\n") end)
</lua>
+
</syntaxhighlight>
  
 
==Offset Timers==
 
==Offset Timers==
Line 114: Line 114:
 
This'll make use of tempTimer to enable a trigger and disable it after a short period of time:
 
This'll make use of tempTimer to enable a trigger and disable it after a short period of time:
  
<lua>
+
<syntaxhighlight lang="lua">
 
enableTrigger("Get enemy list")
 
enableTrigger("Get enemy list")
 
tempTimer(3, [[disableTrigger("Get enemy list")]])
 
tempTimer(3, [[disableTrigger("Get enemy list")]])
</lua>
+
</syntaxhighlight>
  
 
=== Running a script after the current triggers ===
 
=== Running a script after the current triggers ===
 
A useful trick to get your code to run right after all of the current triggers (GUI and temporary) ran would be to use a time of 0:
 
A useful trick to get your code to run right after all of the current triggers (GUI and temporary) ran would be to use a time of 0:
  
<lua>
+
<syntaxhighlight lang="lua">
 
-- in a script, this will run after all scripts were loaded - including the system, wherever in the order it is.
 
-- in a script, this will run after all scripts were loaded - including the system, wherever in the order it is.
 
tempTimer(0, function()
 
tempTimer(0, function()
 
   print("this timer is running after all triggers have run")
 
   print("this timer is running after all triggers have run")
 
end)
 
end)
</lua>
+
</syntaxhighlight>
  
 
Have more examples you'd like to see? Please add or [https://discord.gg/kuYvMQ9 request them]!
 
Have more examples you'd like to see? Please add or [https://discord.gg/kuYvMQ9 request them]!

Revision as of 05:19, 29 June 2017

Timer Engine

Mudlet supports 3 different sorts of timers:

Regular GUI Timers

Regular GUI Timers that fire repeatedly in a certain interval specified by the user. To make one, go to the Timers section (1) in Mudlet, click Add (2), select the time periods you'd like the timer to be going off at - save (3) and activate it (4). The timer will go off after your specified interval and then at regular specified intervals after that, until disabled.

Simple timer.png

You can also enable/disable this timer via scripting with enableTimer() and disableTimer():

enableTimer("Newbie timer") -- enables the timer, so 2s after being enabled, it'll tick - and every 2s after that

disableTimer("Newbie timer") -- disables it, so it won't go off anymore

Temporary Timers

Temporary Timers are timers that go off only once and are the most common type of timer used for scripting purposes. You don't work with them from the Timers section - you just code with them only.

For a basic introduction to temporary timers, read up about them here. Here, we'll continue expanding on what you've learnt so far.

One thing to keep in mind when working with tempTimers is that they are unlike #wait statements you might see in other clients. While #waits are typically cumulative, tempTimers aren't - they go off from the same point in time. Thus if you'd like two timers to go off - one after 3s, and another 1s after the first one - you'd set the second timer to go off at 4s. For example:

tempTimer(3, [[ echo("this is timer #1 going off 3s after being made\n") ]])
tempTimer(4, [[ echo("this is timer #2 going off 4s after being made - and 1s after the first one\n") ]])

Note Note: Temporary timers cannot be accessed from the GUI and are not saved in profiles.

Stopping

To stop a temporary timer once you've made it and before before it went off - you use killTimer(). You give killTimer(id) the ID of the timer that you've made - which you get from Mudlet when you make it. Here's an example:

-- get and store the timer ID in the global "greeting_timer_id" variable
greeting_timer_id = tempTimer(2, [[ echo("hello!\n") ]])

-- delete the timer - thus nothing will actually happen!
killTimer(greeting_timer_id)

Refreshing

You can also use killTimer() to "refresh" a timer - the following code example will delete the previous timer if one exists and create the new one, thus making sure you don't get multiple copies of it:

if portal_timer then killTimer(portal_timer) end
portal_timer = tempTimer(2, [[ send("enter portal") ]])

Using variables

To embed a value of a variable in tempTimer code, you might try using this given what you've learnt:

tempTimer(1.4, [[ echo("hello, "..matches[2].."!\n") ]])

But that won't work as you'd expect it to - that will try and use the value of matches[2] when the timer goes off - while by then, the variable could have changed! Instead, you take it outside the square [[ ]] brackets - this is correct:

tempTimer(1.4, [[ echo("hello, ]]..matches[2]..[[!\n") ]])

Nesting

If you'd like, you can also nest tempTimers one inside another - though the first [[]]'s will become [=[ ]=]:

tempTimer(1, [=[
  echo("this is timer #1 reporting, 1s after being made!\n")
  tempTimer(1, [[
    echo("this is timer #2 reporting, 1s after the first one and 2s after the start\n")
  ]])
]=])

If you'd like to nest more of them, you'd increase the amount of ='s on the outside:

tempTimer(1, [==[
  echo("this is timer #1 reporting, 1s after being made!\n")
  tempTimer(1, [=[
    echo("this is timer #2 reporting, 1s after the first one and 2s after the start\n")

    tempTimer(1, [[
      echo("this is timer #2 reporting, 1s after the second one, 2s after the first one, 3s after the start\n")
    ]])
  ]=])
]==])

Closures

Last but not least, you can also use closures with tempTimer - using a slightly different syntax that has advantages of being able to access variables in it's scope, when it goes off:

local name = matches[2]
tempTimer(2.4, function() echo("hello, "..name.."!\n") end)

Offset Timers

Offset Timers are child timers of a parent timer and fire a single shot after a specified timeout after their parent fired its respective timeout. This interval is an offset to the interval of its parent timer. To make them, add a regular GUI timer (see above), then create another timer and drag it onto the timer. This will make the timer that is "inside" the timer (the child inside the parent) go off at a certain time after it's parent goes off. Offset timers differ visually from regular timers and are represented with a + icon for offset. Offset timers can be turned on and off by the user just like any other timer. For example - a parent timer fires every 30 seconds and by doing so kicks off 3 offset timers with an offset of 5 seconds each. Consequently, the 3 children fire 5 seconds after each time the parent timer fired. To make this happen, make the parent timer tic every 30 seconds, drag 3 timers into it with an offset of 5s on each:

Offset timers.png


Uses and examples

Enable/disable triggers

This'll make use of tempTimer to enable a trigger and disable it after a short period of time:

enableTrigger("Get enemy list")
tempTimer(3, [[disableTrigger("Get enemy list")]])

Running a script after the current triggers

A useful trick to get your code to run right after all of the current triggers (GUI and temporary) ran would be to use a time of 0:

-- in a script, this will run after all scripts were loaded - including the system, wherever in the order it is.
tempTimer(0, function()
  print("this timer is running after all triggers have run")
end)

Have more examples you'd like to see? Please add or request them!