Hello everyone.
I created this little DBP-Function-Collection for advanced timers. It’s quite simple: You create new timers and you are able to control if they or automatically restart them.
Example:
You are programming an RTS, where the player should wait 20 seconds after creating a unit. You just start a timer and control if it expires, after expiring you create the unit and delete the timer.
Commands:
Mt_init_Timer()
Mt_New_timer(Timer_Number, Restarttime)
Mt_update_timer()
Mt_get_miliseconds(Timer_Number)
Mt_get_seconds(Timer_Number)
Mt_get_minutes(Timer_Number)
Mt_get_hours(Timer_Number)
Mt_get_time(Timer_Number)
Mt_set_timer(Timer_Number, Time)
Mt_timer_maxtime(Timer_Number,Maxtime)
Mt_Timer_expired(Timer_Number)
Mt_Get_Percentage(Timer_Number)
Code (with example):
REM Timer-Functions
REM By MrKohlenstoff
REM 8.7.06
REM This is the Example! ***
REM Some basic options
set display mode 800,600,32
set window on
set window layout 1,0,0
set window title "Timer Example.dba"
randomize timer()
sync on : sync rate 0
set text font "Arial"
set text size 20
REM Initialise the timerfunctions
mt_init_timer()
REM 8 Timers
for t = 1 to 8
REM Create an automatically restart-time in some cases
r = rnd(1)
if r = 1 then r = rnd(15000)
mt_new_timer(t,r)
REM Randomize the timers actual time up to one minute
mt_set_timer(t,rnd(60000))
next t
REM The mainloop ***
do
cls
print "Nr.> Hours:Minutes:Seconds:Miliseconds -> restart? -> restarttime"
REM All the timers
for t = 1 to 8
print str$(t) + "> " + mt_get_time(t) + "->" + str$(mt_timer(t).restart) + "->"+ str$(mt_timer(t).restarttime)
next t
REM Update the timers
mt_update_timer()
sync
loop
REM End of the example ***
REM Here starts the code you may include in some programs ***
type mt_Timer
starttime as integer
lasttime as integer
restart as boolean
restarttime as integer
exist as boolean
endtype
REM Initialises the timers
function mt_Init_Timer()
global mt_Max_Timers = 1000
global mt_Timer_Starttime
global mt_highest_timer = 1
dim mt_Timer(mt_max_Timers) as mt_timer
endfunction
REM Creates a new timer, if restarttime is > 0, the timer will automatically restart if it reaches the time (in ms)
function mt_New_Timer(Timer_Number,restarttime)
mt_timer(timer_Number).exist = 1
mt_timer(timer_Number).starttime = timer()
mt_timer(timer_Number).lasttime = timer()
mt_timer(timer_Number).restarttime = restarttime
if restarttime > 0 then mt_timer(timer_Number).restart = 1
if timer_number>mt_highest_timer then mt_highest_timer = timer_number
endfunction
REM Updates all timers
function mt_Update_timer()
for timer_number = 1 to mt_highest_timer
if timer() - mt_timer(timer_Number).starttime => mt_timer(timer_Number).restarttime and mt_timer(timer_Number).restart=1
mt_timer(timer_Number).starttime = timer()
endif
next timer_number
endfunction
REM The following functions return the actual timer-values
function mt_Get_miliseconds(Timer_Number)
ms = timer()-mt_timer(timer_Number).starttime
endfunction ms
function mt_Get_seconds(Timer_Number)
s = (timer()-mt_timer(timer_Number).starttime)/1000.0
s$ = str$(s)
if len(s$)>2 then s$ = right$(s$,2)
s = val(s$)
endfunction s
function mt_get_Minutes(Timer_Number)
m = (timer()-mt_timer(timer_Number).starttime)/60000.0
m$ = str$(m)
if len(m$)>2 then m$ = right$(m$,2)
m = val(m$)
endfunction m
function mt_Get_Hours(Timer_Number)
h = (((timer()-mt_timer(timer_Number).starttime)/1000.0)/60.0)/60
endfunction h
REM Tis function returns a string containing all time-types in a row.
function mt_get_Time(Timer_Number)
h$ = str$(mt_get_hours(Timer_Number))
`if val(h$)>60 then h$ = str$(mt_wrapvalue(val(h$),0,24))
h$ = mt_numberlen(val(h$),2)
m$ = str$(mt_get_minutes(Timer_Number))
if val(m$)=>60 then m$ = str$(mt_wrapvalue(val(m$),0,60))
m$ = mt_numberlen(val(m$),2)
s$ = str$(mt_get_seconds(Timer_Number))
if val(s$)=>60 then s$ = str$(mt_wrapvalue(val(s$),0,60))
s$ = mt_numberlen(val(s$),2)
ms$ = str$(mt_get_miliseconds(Timer_Number))
if len(ms$)>3 then ms$ = right$(ms$,3)
ms$ = mt_numberlen(val(ms$),3)
t$ = h$+":"+m$+":"+s$+":"+ms$
endfunction t$
REM Sets the timer to the time
function mt_Set_Timer(Timer_Number, time)
mt_Timer(Timer_Number).starttime = timer()-time
endfunction
REM Returns a value between min and max, works similar to "wrapvalue" but you can set the min and max-value
function mt_wrapvalue(v,min,max)
dif = abs(max-min)
repeat
if v => max then dec v, dif
if v < min then inc v, dif
until v => min and v < max
endfunction v
REM This function changes then length of a value, by sometimes adding '0's at the front.
function mt_numberlen(n,l)
n$ = str$(n)
nl = len(n$)
if nl => l then exitfunction n$
for z = l-1 to nl step -1
n$ = "0"+n$
next z
endfunction n$
function mt_free_timer()
for t = 1 to mt_max_timers
if mt_timer(t).exist = 0 then exitfunction t
next t
endfunction
function mt_Timer_expired(Timer_Number)
ex = 0
if mt_Timer(Timer_Number).lasttime <= timer()-mt_Timer(Timer_Number).starttime then ex = 1
endfunction ex
function mt_Timer_Maxtime(Timer_Number,Max_Time)
mt_Timer(Timer_Number).lasttime = Max_Time
endfunction
function mt_Get_Percentage(Timer_Number)
max = mt_Timer(Timer_Number).lasttime
act = timer()-mt_Timer(Timer_Number).starttime
if max <> 0
p# = 100.0*act/max
endif
endfunction p#