Timming issues - Millisecs()

Blitz3D Forums/Blitz3D Beginners Area/Timming issues - Millisecs()

Being a newbie, I'm wondering if everyone uses the same methods for the in-game timers ?

eg.

; this code starts my timer
player(1)\accel_timer = Millisecs() ; start timer
player(1)\accel_flag = true

; check if timer has reached designated time
if millisecs() - player(1)\accel_timer => 60
player(1)\accel_timer = 0 ; stop timer
player(1)\accel_flag = false
end if

Is this a reliable method, or is there another way ?

I'm not sure what you're trying to achieve here. You want to explain what the problem is rather than suggesting a solution.

sorry, I'll explain...

during my game I have a various timers that are used for things like accelerating a car sprite and braking a car sprite

if the user presses the accelerate key, then I start a timer running....

player(1)\accel_timer = millisecs()

during the main loop I check the status of that timer to see if 60ms have elapsed...

if millisecs() - player(1)\accel_timer =>60

if they have, I run so code to increment the car's gear.

I'm just wondering if everyone uses simple timers like these ?

this is the first code and the first game I've ever written so the whole concept of timing in my game is complete guesswork. I just want confirmation this is how other people do it too!

Timers are used throughout pretty much any game.

Your use of timers however I'm still slightly hazy about what you're trying to achieve.

For timing of game play you either want to use delta timing or set the amount of updates per second of your game loop.

Regarding your gear changing thing, I would suggest that you have a revs and targetrevs value and do something like: (psuedo code)

if accelerate key_is_pressed then increment targetrevs else decrement targetrevs
if revs<1000 and gear > 1 then gear = gear - 1 : targetrevs = 8000
if revs<1000 and gear = 1 then targetrevs = 10000
if revs>8000 and gear < 6 then gear = gear + 1: targetrevs=1500
revs = (targetrevs-revs)/100 ; rev smoothing

I'll write a proper bit of code later... but got to go home first!

ok, what am I trying to achieve in this instance ?

answer - to make sure the "gear" variable is incremented exactly every 60ms

reagrdless of what I do with the variable "gear" I just want to know that I'm using the right method in order to increment a variable after exactly 60ms

Then it should work, but changing gear every 60ms sounds very very fast, that's over 16 gear changes per second.

Anyway... As promised!

; auto gear shifting

accelkey = 57 ; spacebar

targetrevs#=2000
revs#=2000
gear = 0

x=200
y=150

SetBuffer BackBuffer()

Repeat
Cls

; accelerator key, space bar
If KeyDown(accelkey) Then targetrevs = targetrevs + 100 Else targetrevs=targetrevs - 90

; calcuate revs and stuff
If targetrevs<800 Then targetrevs=Rand(900,1300)
If targetrevs>9000 Then targetrevs = Rand(8000,9000)
If revs<3000 And gear > 1 Then gear = gear - 1 : targetrevs = Rand(7000,8500)
If revs>8000 And gear < 6 Then gear = gear + 1: targetrevs=Rand(1500,2000)
revs = revs + ((targetrevs-revs)/10) ; rev smoothing 


; draw the rev counter
Color 150,150,255
rad=100
Oval x-rad,y-rad,rad*2,rad*2,True
rad=80
Color 0,0,0
Oval x-rad,y-rad,rad*2,rad*2,True
rad=90
Color 0,0,255
For n=1000 To 9000 Step 1000
Text x+(Sin(n/30+180)*rad),y-(Cos(n/30+180)*rad),n/1000,True,True
Next
Color 255,255,255
rad=90
Line x,y,x+(Sin(revs/30+180)*rad),y-(Cos(revs/30+180)*rad)
Text x,y-20,Mid("N123456",gear+1,1),True,True

Delay 30 ; slow it down a bit! You'll probably have to change this on your pc to make it look right!

Flip
Until KeyHit(1)


cheers Rob!

If I added that the game is in 2D, runs at 60fps and the cars are tiny sprites, would it make any sense ?

thanks again!