Code archives/Miscellaneous/[bmax] Interval system

This code has been declared by its author to be Public Domain code.

Download source code

[bmax] Interval system by Beaker2006
Call any function(s) at regular intervals - set & go. Very simple but useful system to setup regular timed calls to functions. It has many uses including: de-coupling game logic from screen updates. Simple syntax:

myInterval = Tinterval.Set(funcname,hz#) - to set an interval
myInterval.Clear() - to clear a previously set interval
Tinterval.ClearAll() - to clear all previously set intervals and clean up hooks and stuff

Designed to be similar to ActionScripts interval system.
SuperStrict


Type Tinterval
	Field _func()
	Field timer:TTimer
	Field hz#

	Global _list:TList

	Method New()
		If _list = Null
			_list = New TList
			AddHook EmitEventHook,_EventHandler
		EndIf
		_list.addlast(Self)
	End Method

	Function Set:Tinterval(func(),hz#)
		Local interval:Tinterval = New Tinterval
		interval.timer = CreateTimer(hz)
		interval.hz = hz
		interval._func = func
		Return interval
	End Function

	Method Clear()
		StopTimer Self.timer
		_list.remove(Self)
	EndMethod

	Function ClearAll()
		If _list <> Null
			_list.clear()
			_list = Null
			RemoveHook EmitEventHook,_EventHandler
		EndIf
	EndFunction

	Function _EventHandler:Object(id%, data:Object, context:Object)
		Local ev:TEvent=TEvent(data)
		If ev.id = EVENT_TIMERTICK 
			If _list <> Null
				For Local interval:Tinterval = EachIn _list
					If ev.source = interval.timer
						interval._func()
						Exit
					EndIf
				Next
			EndIf
		EndIf 
	End Function


End Type

'Example use
Graphics 320,240

Function test()
	DebugLog "TIMER1"
End Function

Function test2()
	Global cnt%
	cnt :+1
	If cnt > 4
		myint.Clear()
	EndIf
	DebugLog "TIMER2"
End Function


Tinterval.Set(test,1)
Global myint:Tinterval = Tinterval.Set(test2,0.5)


While Not AppTerminate()
	Delay 1
Wend
End

Comments

Beaker2006
Here is a variant that lets you access the TimerTicks() of the timer that has fired:



GW2006
This is nice!
I have a question however.
Everytime an object is created you add a hook, But the hook is only removed when all the intervals are cleared. will this create a problem if lots of Tinterval objects are created?

Also, in the _eventhandler function, are the argument objects supposed to be returned from the function? Is this an oversight or on purpose?


Beaker2006
The hook is only added once! If you look you will see that it is only added when the list is empty (ie. before there are any intervals set).

The argument objects not being returned is an oversight, but I'm not sure if you will need them or not. I suppose you will if you are making a maxgui project. It really is a starting point. Feel free to adapt to your own purposes, and if you improve it then post it here.

Thanks for taking a look.

You could add these methods if you need them:
	Method Adjust(hz#)
		StopTimer Self.timer
		Self.timer = CreateTimer(hz)
		Self.hz = hz
	End Method
	
	Function Find:Tinterval(timer:TTimer)
		If _list <> Null
			For Local interval:Tinterval = EachIn _list
				If timer = interval.timer
					Return interval
					Exit
				EndIf
			Next
		EndIf
		Return Null
	End Function



USNavyFish2008
I created something similar, in order to call object methods instead of functions.

Hope this is useful to somebody. Posted in the code archives: http://www.blitzmax.com/codearcs/codearcs.php?code=2306


Code Archives Forum