PtokaX forum

Development Section => HOW-TO's => Topic started by: OpiumVolage on 10 October, 2003, 23:45:54

Title: More that one time in a script
Post by: OpiumVolage on 10 October, 2003, 23:45:54
-- MultiTimer by OpiumVolage (19/06/2003)
-- Ideas taken from tezlo's retrobot

tabTimers = {n=0}

-- Time Definition
Sec  = 1000
Min  = 60*Sec
Hour = 60*Min
Day  = 24*Hour

TmrFreq = 1*Min

function tmrtest()
-- SendToAll( "PtokaX", users.n )
end

function Main()
RegTimer(tmrtest, 1*Min)
SetTimer(TmrFreq)
StartTimer()
end

function OnTimer()
for i=1, getn(tabTimers) do
tabTimers[i].count = tabTimers[i].count + 1
if tabTimers[i].count > tabTimers[i].trig then
tabTimers[i].count=1
tabTimers[i]:func()
end
end
end

function RegTimer(f, Interval)
local tmpTrig = Interval / TmrFreq
assert(Interval >= TmrFreq , "RegTimer(): Please Adjust TmrFreq")
local Timer = {n=0}
Timer.func=f
Timer.trig=tmpTrig
Timer.count=1
tinsert(tabTimers, Timer)
end
Title:
Post by: pHaTTy on 27 October, 2003, 18:54:30
Heh, good one :o)
Title:
Post by: NightLitch on 30 October, 2003, 23:10:58
I must be dumb or I can't get it working with multi timers...

Plz give me an example off this...

I want one timer on 60000 and one on 20000 how would this look...

I am not so familiar with timers so plz help me understand...

Best Show'n with some examples....

/NightLitch
Title:
Post by: OpiumVolage on 31 October, 2003, 00:06:12
Here you are 60000 is One minute and 20000 is 20 seconds:

It's almost the same code, i've just added one function (second timer), and adjusted the timer frequency to someting more adjusted.

-- MultiTimer by OpiumVolage (19/06/2003)
-- Ideas taken from tezlo's retrobot

tabTimers = {n=0}

-- Time Definition
Sec  = 1000
Min  = 60*Sec
Hour = 60*Min
Day  = 24*Hour

TmrFreq = 20*Sec

function tmrone()
SendToAll( "PtokaX", "First Timer")
end

function tmrtwo()
SendToAll( "PtokaX", "Second Timer" )
end

function Main()
RegTimer(tmrone, 1*Min)
RegTimer(tmrtwo, 20*Sec)
SetTimer(TmrFreq)
StartTimer()
end

function OnTimer()
for i=1, getn(tabTimers) do
tabTimers[i].count = tabTimers[i].count + 1
if tabTimers[i].count > tabTimers[i].trig then
tabTimers[i].count=1
tabTimers[i]:func()
end
end
end

function RegTimer(f, Interval)
local tmpTrig = Interval / TmrFreq
assert(Interval >= TmrFreq , "RegTimer(): Please Adjust TmrFreq")
local Timer = {n=0}
Timer.func=f
Timer.trig=tmpTrig
Timer.count=1
tinsert(tabTimers, Timer)
end
Title:
Post by: NightLitch on 31 October, 2003, 10:12:58
ThX OpiumVolage but how would this be done:

function Main()
frmHub:RegBot(BotName)
SetTimer(60000)
StartTimer()
end

function OnTimer()
for key, value in tabAdvert do
if (tabAdvert[key].iClock > clock()+60) then
tabAdvert[key]=nil
end
end
end

It's taken from Lucifer 6.6.6 I want to add this to my script so how would this look in your time-script...

Regards / NightLitch
Title:
Post by: NightLitch on 31 October, 2003, 10:57:34
No Need to Help I fixed it myself I think...

/NightLitch
Title:
Post by: shipiz on 20 January, 2004, 14:28:56
I get:

No syntax errors in script file timer.lua
Syntax error: attempt to perform arithmetic on global `Hub' (a nil value)
stack traceback:
   1:  method `func' at line 17 [file `F:\hub\scripts\timer.lua']
   2:  function `OnTimer' at line 31 [file `F:\hub\scripts\timer.lua']

Title: Re: More that one time in a script
Post by: Madman on 04 November, 2006, 01:09:32

function RegTimer(F,Interval,Name)
local tmpTrig = Interval / TmrFreq
assert(Interval >= TmrFreq, "RegTimer(): Please Adjust TmrFreq")
local Timer = {n=0}
local Hit = nil
for I in ipairs(tabTimers) do
if tabTimers[I].Name == Name then
Hit = 1
end
end
if Hit then
-- Don't add...
else
Timer.Name = Name
Timer.Func = F
Timer.Trig = tmpTrig
Timer.count=1
table.insert(tabTimers, Timer)
end
end

function RemoveTimer(Name)
local count = 0
for I in ipairs(tabTimers) do
count = count+1
if string.find(tabTimers[I].Name, Name) then
Timer2Remove = count
break
end
end
table.remove(tabTimers, Timer2Remove)
end


Updated the code...
Now it can't add the same timer twice, and added a function to remover timers...