Developing a timer

Blitz3D Forums/Blitz3D Beginners Area/Developing a timer

W
 #1
Hey guys I have a little delimma and I haven't made any timers in Blitz before.In my FPS I have an enemy who is going to attack the player once he gets soo close, obviously. But I want to add time between the players health drains. Soo I was thinking is it possible to make a timer, maybe one that uses the frame rate or something to put in the delay between health drains??

Yes, you can do it several ways. I wouldn't use the framerate as a 'timer' though since everyones computers run at different frame rates.

Just use the MilliSecs() command.
draintimer = MilliSecs() + 1000	;1 second
.
.
.
If MilliSecs() > draintimer Then ;do your drain health thing again


Type ticker
	Field LastTick%
	Field Frequency%
End Type

Function ticker_Create.ticker( nFreq% = 1000 )
			Local nTicker.ticker = New ticker
			nTicker\Frequency% = nFreq%
			nTicker\LastTick% = MilliSecs()
			Return nTicker
End Function

Function ticker_HasTicked%( tTicker.ticker )
			If MilliSecs() > ( tTicker\LastTick + tTicker\Frequency )
				tTicker\LastTick = MilliSecs()
				Return True
			EndIf
End Function


Simple usage

DamageTimer.ticker = ticker_Create( 1000 )
If ticker_HasTicked( DamageTimer ) Then DrainHealth()

W
 #4
Jams your confusing me just a little, soo could you give me a little more indepth breakdown of the timer system??

-Also the enemy going twords the user is becoming a little frustrating to me as well here's some of my coding

insight = EntityVisible(enemy\model,user\model)


If insight = 1 And EntityDistance#(enemy\model,user\model) < 20 And crouch = 0
	PointEntity enemy\model,playerhead 
	follow()
EndIf 

If enemy\health <= 0
	HideEntity enemy\model
EndIf 	


Oh and the Follow function

Function Follow()
If EntityX#(user\model) > EntityX#(enemy\model)
	MoveEntity enemy\model,.03,0,0
ElseIf EntityZ#(user\model) > EntityZ#(enemy\model)
	MoveEntity enemy\model,0,0,.03
ElseIf EntityX#(user\model) < EntityX#(enemy\model) 
	MoveEntity enemy\model,-.03,0,0
ElseIf EntityZ#(user\model) < EntityZ#(enemy\model)	
	MoveEntity enemy\model,0,0,-.03
EndIf 			



End Function


Any help will be greatly appreciated!!

Check the render loop, this may help:

Graphics3D 800,600,0,2

; ### Create light

Global Light=CreateLight()

RotateEntity Light,45,-90,0
LightColor Light,255,255,255
AmbientLight 64,64,64

; ### Create camera

Global Camera=CreateCamera()
CameraClsColor Camera,0,0,0
CameraRange Camera,1,1000
PositionEntity Camera,0,20,-50
RotateEntity Camera,30,0,0


; ### Create plane

dCreatePlane(Space,0,1,0,0)

Plane=CreatePlane()

EntityAlpha Plane,0.8

PlaneTexture=CreateTexture(128,128,9)

ClsColor 0,200,80
Cls

Color 255,255,255

Rect 0,0,64,64,1
Rect 64,64,64,64,1

CopyRect 0,0,128,128,0,0,BackBuffer(),TextureBuffer(PlaneTexture)
ScaleTexture PlaneTexture,20,20
EntityTexture Plane,PlaneTexture,0,0

;=============================
; Render Loop

While Not KeyHit(1)

; Get Time 
T=MilliSecs() 

; Track FPS
If T > Time1 + 1000 
	Time1=T

	FPS=Frame
	Frame=0
EndIf

; Drain Player Health once every 2 seconds (2000ms)
If T > Time2 + 2000 
	Time2=T

	Health=Health-100
EndIf


; Cap Framerate at 50FPS (1000ms/50=20ms)
If T > Time0 + 20 
	Time0=T

	UpdateWorld
	RenderWorld
	
	Frame=Frame+1
	Li=0
	Text 0,0,"FPS:"+FPS : Li=Li+15
	Text 0,Li,"Health:"+Health : Li=Li+15
	
	Flip
EndIf

Wend
;===============================