Page 2 of 3

Re: [script] Extended timer (followParty, tickLimit, callbac

Posted: Sun Nov 25, 2012 1:25 am
by Diarmuid
I have an idea. We could create a global timer based on my lightTracker model. This would activate something (a button) with a very small time resolution. So now we have a constant tick at, let's say 1/100s. Then instead of spawning timers, we spawn counters and connect the tick to them with a decrement action. So such a counter of a value of 100 would always activate after 1s no matter where it's placed or where the party is. It could then self-reset and continue until destroyed.

How does that sound?

Re: [script] Extended timer (followParty, tickLimit, callbac

Posted: Sun Nov 25, 2012 3:38 am
by Komag
Batty wrote:For longer time periods couldn't you do:

Code: Select all

x = 0
function ticktock(timer)
   if party.level == timer.level then
      x = x + 1
   end
   if x == 100 then
      do this
      all timers:deactivate()
   end
end
The timer on each level calling this function would be set to one second then you don't have the long interval problem.
It sounds like your saying just never use long timers, but always use short ones and if you want a long one then just nest a tick counter in there to do your bidding?

Re: [script] Extended timer (followParty, tickLimit, callbac

Posted: Sun Nov 25, 2012 3:39 am
by Komag
Diarmuid wrote:I have an idea. We could create a global timer based on my lightTracker model. This would activate something (a button) with a very small time resolution. So now we have a constant tick at, let's say 1/100s. Then instead of spawning timers, we spawn counters and connect the tick to them with a decrement action. So such a counter of a value of 100 would always activate after 1s no matter where it's placed or where the party is. It could then self-reset and continue until destroyed.

How does that sound?
I'm not sure I follow correctly, but in any case I suspect trying to have a constant 1/100 timer running would hurt performance

Re: [script] Extended timer (followParty, tickLimit, callbac

Posted: Sun Nov 25, 2012 5:10 am
by Diarmuid
I was suggesting 1/100 just like that... 1/30 or 1/20 could probably be enough. I'll need to try to code an example and see how it goes.

Re: [script] Extended timer (followParty, tickLimit, callbac

Posted: Sun Nov 25, 2012 10:32 am
by JKos
I think Battys idea should work, I just have to handle long intervals differently than short ones. Have to try it. Damn, you made me to spend more time on this :)

Re: [script] Extended timer (followParty, tickLimit, callbac

Posted: Sun Nov 25, 2012 4:43 pm
by Batty
Komag wrote:It sounds like your saying just never use long timers, but always use short ones and if you want a long one then just nest a tick counter in there to do your bidding?
Yes, and if you want it to continue on for more long intervals, replace:

Code: Select all

all timers:deactivate()
with:

Code: Select all

x = 0 
should work fine, but who knows.

Re: [script] Extended timer (setConstant, tickLimit, callbac

Posted: Sun Nov 25, 2012 6:09 pm
by JKos
I made it, now it works pretty nicely. Only limitation for constant timers is that if the interval is over 1 second you can't use decimal numbers.

setTimerInterval(1.5) wont work but setTimerInterval(100) or setTimerInterval(0.1) works.
Decimal numbers can be use for "normal" (not constant) timers of course.

Updated the documentation to first post.

Re: [script] Extended timer (setConstant, tickLimit, callbac

Posted: Sun Nov 25, 2012 10:20 pm
by JKos
I made simple ingame clock with it which uses the compass icon:
Items.lua

Code: Select all

cloneObject{
	name='clock',
	baseObject='compass',
	uiName = "Pocket watch",
	description='',
	onUseItem = function(self)
		eTimer = timers:find('clock_timer')
		local hours = math.floor((eTimer.tick / 60) % 24)
		local minutes = eTimer.tick % 60 
		if minutes < 10 then minutes = '0'..minutes end
		if hours < 10 then hours = '0'..hours end
		hudPrint('Time is: '..hours..':'..minutes)
	end
}
to some script entity:

Code: Select all

function initClock()
	party:getChampion(1):insertItem(14,spawn('clock',nil,nil,nil,nil,'clock'))
	local clockTimer = timers:create('clock_timer')
	clockTimer:setTimerInterval(1)
	clockTimer:setConstant()
	clockTimer.tick = 7190 -- set the initial time to 23:50
	clockTimer:activate()
end
1 second == 1 minute in game time

Re: [script] Extended timer (setConstant, tickLimit, callbac

Posted: Sun Nov 25, 2012 10:56 pm
by JKos
I just realized that I can add functions to extended timer dynamically so I changed the clock implementation so that I can access the time easily from anywhere:
Modified example:

Code: Select all

 
cloneObject{
	name='clock',
	baseObject='compass',
	uiName = "Pocket watch with compass",
	description='',
	onUseItem = function(self)
		local clockTimer = timers:find('clock_timer')
		hudPrint('Time is: '..clockTimer:getTime())
	end
}

Code: Select all

function initClock()
	party:getChampion(1):insertItem(14,spawn('clock',nil,nil,nil,nil,'clock'))
	
	local clockTimer = timers:create('clock_timer')
	clockTimer:setTimerInterval(1)
	clockTimer:setConstant()
	clockTimer.tick = 7190 -- set the initial time to 23:50
	clockTimer:activate()
	
	clockTimer.getHours = function(self)
		return math.floor((self.tick / 60) % 24)
	end
	clockTimer.getMinutes = function(self)
		return self.tick % 60 
	end
	clockTimer.getTime = function(self)
		local minutes = self:getMinutes()
		local hours = self:getHours()
		if minutes < 10 then minutes = '0'..minutes end
		if hours < 10 then hours = '0'..hours end
		return hours..':'..minutes
	end
end
Now I can for example get current hour by calling timers:find('clock_timer'):getHours().

Re: [script] Extended timer (setConstant, tickLimit, callbac

Posted: Sun Nov 25, 2012 11:07 pm
by Komag
this is crazy stuff! I guess whenever we get the update with get statistic grime played a lot of this might become moot, but until then it will be pretty useful