how to make a tick timer?

Post Reply
Posts: 42
Joined: Tue Feb 22, 2011 12:03 pm

how to make a tick timer?

Post by kakku » Sat Mar 19, 2011 12:38 am

i'm thinking to make a tick timer, but i'm kinda lost as how to do it.

what i would like is:
a little clock/timer that counts down to zero. and on zero tells me a tick has occurred.
i'm planning to add triggers to sync the timer.

i was looking through the forum to see what's the best way to do this and permTimers were mentioned.
are those the times i can create under the timer icon?
how do i use them in a repeating manner?
and what if i wanna change the time interval dynamically?

and once i get that to work: how would i go about making it visual?

does anyone maybe have any working tick timer i could have a look at for ideas?

Posts: 49
Joined: Sun Sep 12, 2010 1:50 am

Re: how to make a tick timer?

Post by Lucky24 » Tue Mar 22, 2011 3:08 am

There isn't much info on permTimers in the manual, but I've made re-occuring timers by encasing normal tempTimers in a function that calls itself when the timer completes.

For example my playing toggle:

If the function is passed a parameter of -1, it will fire again by calling the function again after a few (default of 5) minutes, over and over again with the parameter of -1. As long as the function is passed a parameter of -1, it will keep firing after 5 minutes. If any other number is given as the parameter (say, 10), the timer will kill the loop after the given time value (10 minutes) by calling the function again with a value of 0. A blank parameter also kills the loop.
Code: [show] | [select all] lua
function scriptLog(...) end --dummy function

g = g or {}
g.debugLevel = "debug"
g.playing = g.playing or {}
g.playing.on = g.playing.on or false
g.playing.defaultTime = 5 --minutes 
function g.playing:toggle(time)
	if time then
		self.time = time
		if self.on then self.time = 0 else self.time = self.defaultTime end
	   if self.time==-1 then
			scriptLog("\nPlaying on permanently (-1). Starting recurring playing notice.","warning")
			self.on = true
			if self.timer then killTimer(self.timer) end
			self.timer = tempTimer(self.defaultTime*60,[[g.playing.timer=false;g.playing:toggle(-1)]])
		elseif self.time>0 then
			scriptLog("\nToggling playing on.  Disabling AI for "..self.time.." minutes. ","warning")
			self.on = true
			if self.timer then killTimer(self.timer) end
			self.timer = tempTimer(self.time*60,[[g.playing.timer=false;g.playing:toggle(0)]])
			scriptLog("\nToggling playing off (0). ","warning")
			self.on = false
			if self.timer then killTimer(self.timer); self.timer=false end
Note 1: The function takes advantage of the fact that a tempTimer() returns a unique timer index, and I assign that value to self.timer. When the timer fires, I clear the value from self.timer (as the timer has fired and no longer exists), then call the toggle function. If the function is called *before* the temp timer fires, then killTimer(self.timer) kills the still running timer, and I set self.timer to the new (if any) timer index.

Note: The function takes advantage of lua's inheritance model for functions (the self variable), which is described in the Programming in Lua book.

User avatar
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: how to make a tick timer?

Post by Heiko » Tue Mar 22, 2011 7:55 am

The simplest way is to use GUI timer object (trigger editor -> timers -> new). GUI timer objects keep running while they are enabled. To enable/disable them via scripts: enableTimer(name) / disableTimer(name)
When the timer fires the timer script is being run. The interval of GUI timers cannot be changed via script. permTimer() is just a function to create GUI timers, but if you want to change the time interval this isn't what you want to use either.
To make timers with changing time intervals, you have to use tempTimers which are one shot timers. Note that you cannot change the time interval on a running timer because of cross platform OS limitations. If you need to change the interval of a running timer you have to stop the temp timer and destroy it (killTimer(id)) and then create a new tempTimer with the remaining time offset. Also take a look at Mudlet's stop watch functions that allow you to create you own complex timer frameworks if necessary.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests