Well, if there is a DBP version there is also a DBC version!
This is very weird... A ton of people suddenly wanted a function for timer based movement...
There are two types of timer-based movement. One is, like the DBP version, when you multiply the variables by a factor calculated by the system time and the DBP loop using vectors. The other one, like this one, directly affects the loop speed.
cometSYNC()
This function will affect the synchronization so your main loop will run at a constant speed of a given value. You can use this function instead of using "sync", but never use both! The sync rate must be specified using an integer value, the update value must be specified using an integer value, and the return value is specified using an integer value.
cometSYNC() will count the loops actually performed, and subtract the result from the loops it should have done. From that it calculates which synchronizations should be evenly skipped the next loop.
Pros
-Very easy to implement
-variable function
Cons
-Too unstable for Net Games
SYNTAX
return value=cometSYNC(sync rate,update)
how to use
Replace every "sync" command with "cometSYNC()". The return value returns the FPS rate. The sync rate is the rate you want your loop to run at. The update value specifies after how long the function should update the counter in milliseconds. The more objects you have in your game, the higher this value should be. I found a value of 500-1000 runs well. If you have no idea what to set this to, then just leave it at 500
Function Code
Compressed (I could even post this in the 20-liner!):
rem comet sync function ----------------------------------------------------------------------------------
function cometSYNC(syncrate,update)
syncrate=syncrate/(1000/update):ssync=syncrate:inc syncrate,offset:time=timer():time_elapsed=time-oldtimer:inc cloop:if time_elapsed>=update:lastloopsmissed=loopsmissed:loopsmissed=((syncrate-cloop)+lastloopsmissed)/2:result=cloop
cloop=1:oldtimer=time:if loopsmissed<1:skip#=0:incskip#=0:else:x#=syncrate:y#=loopsmissed:skip#=x#/y#:incskip#=skip#:endif:inc offset,(ssync-result):result=result*(1000/update):endif:if int(skip#)=cloop then inc skip#,incskip# else sync
endfunction result
Uncompressed:
rem comet sync function
function cometSYNC(syncrate,update)
rem get time elapsed
syncrate=syncrate/(1000/update)
ssync=syncrate
inc syncrate,offset
time=timer()
time_elapsed=time-oldtimer
rem count loops
inc cloop
rem check if 1 second passed
if time_elapsed>=update
rem calculate loops missed
lastloopsmissed=loopsmissed
loopsmissed=((syncrate-cloop)+lastloopsmissed)/2
rem reset values
result=cloop
cloop=1
oldtimer=time
rem calculate when to skip next loops
if loopsmissed<1
skip#=0
incskip#=0
else
x#=syncrate
y#=loopsmissed
skip#=x#/y#
incskip#=skip#
endif
inc offset,(ssync-result)
result=result*(1000/update)
endif
rem sync
if int(skip#)=cloop then inc skip#,incskip# else sync
endfunction result
Example
rem comet sync
rem by TheComet
rem setup screen
sync on
sync rate 60
backdrop on
color backdrop 0
hide mouse
rem make some cubes
for t=1 to 180
make object cube t,20
position object t,rnd(300)-150,rnd(300)-150,rnd(300)-150
color object t,rgb(rnd(255),rnd(255),rnd(255))
next t
rem main loop
position camera 0,0,0
point camera 0,0,0
do
rotate camera wrapvalue(camera angle x()+mousemovey()),wrapvalue(camera angle y()+mousemovex()),0
if spacekey()=1 then move camera 1
rem print fps
ink rgb(0,255,0),0
if mode=0 then text 0,40,"Comet SYNC - Constantly runs at 60 FPS"
if mode=1 then text 0,40,"Normal SYNC - notice how it lags"
ink rgb(255,255,255),0
text 0,0,"Screen FPS : "+str$(screen fps())
text 0,20,"Comet FPS : "+str$(cometfps)
text 0,60,"Move mouse to view/Space to move"
text 0,80,"Press Enter to switch sync modes."
rem change mode
if returnkey()=1
inc mode:if mode>1 then mode=0
repeat:until returnkey()=0
endif
rem rotate cube
for t=1 to 180
rotate object t,wrapvalue(object angle x(t)+3),wrapvalue(object angle y(t)+4),0
next t
rem refresh screen with comet sync
if mode=0
cometfps=cometSYNC(60,500)
else
sync
endif
rem end of main loop
loop
rem comet sync function ----------------------------------------------------------------------------------
function cometSYNC(syncrate,update)
syncrate=syncrate/(1000/update):ssync=syncrate:inc syncrate,offset:time=timer():time_elapsed=time-oldtimer:inc cloop:if time_elapsed>=update:lastloopsmissed=loopsmissed:loopsmissed=((syncrate-cloop)+lastloopsmissed)/2:result=cloop
cloop=1:oldtimer=time:if loopsmissed<1:skip#=0:incskip#=0:else:x#=syncrate:y#=loopsmissed:skip#=x#/y#:incskip#=skip#:endif:inc offset,(ssync-result):result=result*(1000/update):endif:if int(skip#)=cloop then inc skip#,incskip# else sync
endfunction result
Enjoy!
TheComet