Timer Memory access violation

Archives Forums/Blitz3D Bug Reports/Timer Memory access violation

linkjay1(Posted 2015) [#1]
I have been looking all over and i can't find a solution so maybe this will help:

The problem is that when i run the code i get a memory access violation

CODE:

Graphics3D 800,600,0,2
SetBuffer BackBuffer()

timer = (30)

font=LoadFont("Arial",15)
SetFont font


Type bullet
Field x
Field y
End Type


Type bomb
Field x
Field y
End Type


Type alien
Field x
Field y
End Type


While result=0
result=MainMenu()
;put these options inside the menu loop, as these
;return 0 - continuing the mainmenu loop.
If result=2 Then result=Options()
If result=3 Then result=Credits()
Wend
If result=1 Then StartGame()
;if the result isn't 1, then it must be 4 - hence QUIT
End

Function MainMenu()
Cls
Text 400,50,"Start Menu",1,1
Rect 300,100,200,50,0
Text 400,125,"Start Game",1,1
Rect 300,200,200,50,0
Text 400,225,"Options",1,1
Rect 300,300,200,50,0
Text 400,325,"Credits",1,1
Rect 300,400,200,50,0
Text 400,425,"Quit",1,1
Flip
result=0
While result=0
mx=MouseX():my=MouseY()
If MouseHit(1) Then
If RectsOverlap(300,100,200,50,mx,my,1,1) Then result = 1
If RectsOverlap(300,200,200,50,mx,my,1,1) Then result = 2
If RectsOverlap(300,300,200,50,mx,my,1,1) Then result = 3
If RectsOverlap(300,400,200,40,mx,my,1,1) Then result = 4
End If
Wend
Return result
End Function

Function Options()
Cls
Text 400,300,"Options Screen! Any key to return to main menu",1,1
Flip
WaitKey()
Return 0
End Function

Function Credits()
Cls
Text 400,300,"Credits!!",1,1
Text 400,330,"Intro Made by: BlackD",1,1
Text 400,350,"Everything Else: linkjay1", 1, 1
Text 400,400,"Any key to return",1,1
Flip
WaitKey()
Return 0
End Function

Function StartGame()

Cls


AutoMidHandle True
img_ship = LoadImage("ship.png")
img_bullet = LoadImage("bullet.png")
img_alien = LoadImage("alien.png")

wav_music = LoadSound("music.wav")
wav_explode = LoadSound("explode.wav")
wav_shoot = LoadSound("shoot.wav")
wav_shiphit = LoadSound("shiphit.wav")
wav_alienshoot = LoadSound("alienshoot.wav")

x = 320
y = 400


lives = 3

score = 0

aspeed = 2
amx = aspeed
chdir = False

SetBuffer BackBuffer()



For z = 1 To 7
For w = 1 To 2
a.alien = New alien
a\x = 100 + 50*z
a\y = 150 + 50*w
Next
Next



While Not KeyDown(1)


Cls



DrawImage img_ship,x,y

If KeyDown(30) Then x = x - 3
If KeyDown(32) Then x = x + 3

If KeyHit(57) Then
PlaySound(wav_shoot)
b.bullet = New bullet
b\x = x
b\y = y - 5
EndIf

For b.bullet = Each bullet
b\y = b\y - 5
DrawImage img_bullet,b\x,b\y
If b\y < 0 Then Delete b
Next

If chdir = True Then
amx = -amx
EndIf
chdir = False

For a.alien = Each alien
a\x = a\x + amx
If a\x > 620 Then chdir = True
If a\x < 20 Then chdir = True

If Rand(500) = 25 Then
PlaySound(wav_alienshoot)
bombs.bomb = New bomb
bombs\x = a\x
bombs\y = a\y
EndIf
DrawImage img_alien,a\x,a\y
For b.bullet = Each bullet
If ImagesCollide(img_bullet,b\x,b\y,0,img_alien,a\x,a\y,0) Then
PlaySound(wav_explode)
Delete b
Delete a
score = score + 10
Exit
EndIf
Next
Next

For bombs.bomb = Each bomb
bombs\y = bombs\y + 4
DrawImage img_bullet,bombs\x,bombs\y

If ImagesCollide(img_bullet,bombs\x,bombs\y,0,img_ship,x,y,0) Then
PlaySound(wav_shiphit)
lives = lives - 1
Delete bombs
ElseIf bombs\y > 500 Then
Delete bombs
EndIf

Next

Text 50,420,"Lives: " + lives

Text 530, 420, "SCORE: " + score

If score = 140 Then
Text 320, 100, "YOU WIN!!!", 1, 1
Flip
Delay 3000
End
EndIf

If lives = 0 Then
Text 320, 100, "GAME OVER", 1, 1
Flip
Delay 3000
End
EndIf


Flip

WaitTimer(timer)


Wend

End

End Function


Floyd(Posted 2015) [#2]
Timer Memory access violation

How to use a timer:
myTimer = CreateTimer( 4 ) ; four times per second

While Not KeyHit( 1 )
	Cls
	WaitTimer( myTimer )
	ticks = ticks + 1
	Text 60, 120, "ESCAPE quits, timer ticks = " + ticks
	Flip
Wend



_PJ_(Posted 2015) [#3]
This is not a Blitz bug, just an error in your coding.

There are two main problems:

1:
You are declaring timer in your 'main program' scope
timer=(30)

but referencing it inside the StartGame Function
WaitTimer(timer)

The scope of this function assumes 'timer' to be a local variable, which by default is undeclared and therefore = 0

Your error occurs because you try to call WaitTimer on a null timer handle.

-

2:
Your declaration:
tiemr=(30)

Is incorrect syntax for declaring a timer handle.
What you are actually doing is simply defining a local integer variable called "timer" with a value of 30

_______________________________________


Making the following alteration will solve the problem completely:

Change
timer = (30)

Into
Global timer = CreateTimer(30)


Firstly, it now actual defines "timer" as a Timer handle with a parameter of 30 hz, rather than just a numeric variable with an arbitrary value of 30.
Also ensures that scope is global so 'timer' used anywhere will refer to this actual timer handle.

Read up on the differences between Handles and Variables, also how Scope affects references to these values inside functions and outside of them.