[script] Extended timer (setConstant, tickLimit, callbacks)

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
User avatar
Diarmuid
Posts: 807
Joined: Thu Nov 22, 2012 6:59 am
Location: Montreal, Canada
Contact:

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

Post 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?
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

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

Post 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?
Finished Dungeons - complete mods to play
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

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

Post 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
Finished Dungeons - complete mods to play
User avatar
Diarmuid
Posts: 807
Joined: Thu Nov 22, 2012 6:59 am
Location: Montreal, Canada
Contact:

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

Post 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.
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

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

Post 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 :)
- LoG Framework 2http://sites.google.com/site/jkoslog2 Define hooks in runtime by entity.name or entity.id + multiple hooks support.
- cloneObject viewtopic.php?f=22&t=8450
Batty
Posts: 509
Joined: Sun Apr 15, 2012 7:04 pm

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

Post 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.
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

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

Post 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.
- LoG Framework 2http://sites.google.com/site/jkoslog2 Define hooks in runtime by entity.name or entity.id + multiple hooks support.
- cloneObject viewtopic.php?f=22&t=8450
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

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

Post 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
- LoG Framework 2http://sites.google.com/site/jkoslog2 Define hooks in runtime by entity.name or entity.id + multiple hooks support.
- cloneObject viewtopic.php?f=22&t=8450
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

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

Post 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().
Last edited by JKos on Sun Nov 25, 2012 11:12 pm, edited 2 times in total.
- LoG Framework 2http://sites.google.com/site/jkoslog2 Define hooks in runtime by entity.name or entity.id + multiple hooks support.
- cloneObject viewtopic.php?f=22&t=8450
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

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

Post 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
Finished Dungeons - complete mods to play
Post Reply