memory errors caused bu looping

BlitzMax Forums/BlitzMax Beginners Area/memory errors caused bu looping

Ryan Burnside(Posted 2006) [#1]
Graphics 640,480,0

Global instance_count=0
Include "Enemy.bmx"
Include "Tplayer.bmx" 
Include "shot.bmx"
Include "fragment.bmx"
Include "star.bmx"
SeedRnd MilliSecs () 


SetBlend LIGHTBLEND
SetAlpha 1



Player.Create(400,240,90)

'___________________________________variables____________________________________________________
rings_position#=1
cycletimer#=0
Global score=0
Global game_state=0 '0=title,1=game,2=gasme over

'____________________________________functions____________________________________________________

	' draw rect unfilled
	Function draw_rect_hollow#(x1#,y1#,width#,Height#)
		DrawLine x1,y1,x1+width-1,y1
		DrawLine x1,y1,x1,y1+Height-1
		DrawLine x1+width-1,y1,x1+width-1,y1+Height-1
		DrawLine x1,y1+Height-1,x1+width-1,y1+Height-1
	EndFunction

	' draw circle unfilled
	Function draw_ellipse_hollow#(x1#,y1#,width#,Height#,segments:Int,rotation#)

		Local center_x#=x1+(width/2)
		Local center_y#=y1+(Height/2)
		Local x_radius#=((width-1)/2)
		Local y_radius#=((Height-1)/2)
		Local angle#=360/segments
			For i=0 To segments-1 Step 1
			
				DrawLine center_x+Sin((i*angle)+rotation)*x_radius,center_y+Cos((i*angle)+rotation)*y_radius,center_x+Sin(((i+1)*angle)+rotation)*x_radius,center_y+Cos(((i+1)*angle)+rotation)*y_radius
			
			Next
	EndFunction

	' draw lines from center
	Function draw_ellipse_lines#(x1#,y1#,width#,Height#,segments:Int)

		Local center_x#=x1+(width/2)
		Local center_y#=y1+(Height/2)
		Local x_radius#=((width-1)/2)
		Local y_radius#=((Height-1)/2)
		Local angle#=360/segments
			For i=0 To segments-1 Step 1
				DrawLine center_x+Sin(i*angle)*x_radius,center_y+Cos(i*angle)*y_radius,center_x,center_y
			Next
	EndFunction
	
	' compute the distance between two points
	Function point_distance#(x1#,y1#,x2#,y2#)
		Local x_distance#=x1-x2
		Local y_distance#=y1-y2
		Local distance#=Sqr((x_distance*x_distance)+(y_distance*y_distance))
		Return distance
	EndFunction
	
	'compute the angle between 2 points
	Function point_direction#(x1#,y1#,x2#,y2#)
	    
	    Local Angle# = ATan2(y2-y1,x2-x1)
	    	If angle<0
	    		angle:+ 360
	    	End If
		
		Return angle 

		
	End Function
	
    	
	While not KeyDown(KEY_ESCAPE)		
		

	
	While game_state=0
	
	Cls
	' set the general crapps
	SetColor 255,255,255
	DrawText"Instances"+instance_count,0,0
	'draw background color
	SetColor 0,0,0
	DrawRect 0,0,640,480
	' set the positions of the flamming burrons
	
		' 1st button
		start_button_x=320+Cos(270)*64
		start_button_y=240+Sin(270)*64
		SetColor 255,255,255
	
		If point_distance(start_button_x,start_button_y,MouseX(),MouseY())<48
			fragment.Create(start_button_x,start_button_y,3,270,90,96,8,16,24,60,-1.5)
		
		Else
			fragment.Create(start_button_x,start_button_y,3,270,90,96,5,5,5,60,-1.5)
		EndIf
		SetColor 255,255,255
		
	
		'2nd button
		option_button_x=320+Cos(150)*64
		option_button_y=240+Sin(150)*64
	
		If point_distance(option_button_x,option_button_y,MouseX(),MouseY())<48
			fragment.Create(option_button_x,option_button_y,3,150,90,96,24,8,16,60,-1.5)
		Else
			fragment.Create(option_button_x,option_button_y,3,150,90,96,5,5,5,60,-1.5)
		EndIf
	
	
	
		'3rd button
		quit_button_x=320+Cos(30)*64
		quit_button_y=240+Sin(30)*64
		If point_distance(quit_button_x,quit_button_y,MouseX(),MouseY())<48
			fragment.Create(quit_button_x,quit_button_y,3,30,90,96,16,24,8,60,-1.5)
		Else
			fragment.Create(quit_button_x,quit_button_y,3,30,90,96,5,5,5,60,-1.5)
		EndIf
      
		
		' check to see what the player has selected
		If MouseDown (MOUSE_LEFT)=True and point_distance(start_button_x,start_button_y,MouseX(),MouseY())<48
			
			
			
			game_state=1	
				
			
		End If
	
	
	' update fragments
	For Local m:fragment=EachIn fragment.fragment_list
		m.Update()
	Next 
	
	Flip
		
	Wend


'____Main Game_Loop_________________________________________________
    
	While game_state=1 ' make sure the user hasn't quit
	Cls
	
	
	
	'FPS_Counter    <> Runs And displays the FPS
'--------------------------------------------
  SetRotation 0 'Undo rotation 
  SetScale 1,1 'Undo scale
		
  FPS_Counter=FPS_Counter+1
			
  If FPS_Counter_time+1000 =< MilliSecs()
        FPS=FPS_Counter' <- Frames/Sec
        FPS_Counter=0
        FPS_Counter_time=MilliSecs()	
  EndIf
					
  DrawText ""+score,20,20
'--------------------------------------------

	
	
	
	
	
	
	
	' draw step
	' draw background
		SetColor 2,6,73
		DrawRect 0,0,640,480
	



		
		SetColor 45,56,68
		draw_ellipse_hollow(160,0,480,480,36,0)
		draw_ellipse_lines(160,0,480,480,36)
		draw_ellipse_hollow(400-rings_position,240-rings_position,rings_position*2,rings_position*2,18,0)
		
		'the font please
	SetScale 2,2
	DrawText "Instances: "+instance_count,0,0 
	SetScale 1,1

		
	' make the enemies	
	If cycletimer=0
	 
		Enemy.Create(400,240,Rand(360)+RndFloat(),1)
		
	EndIf
	' make the stars
	Star.Create(400,240)
	
	' update enemy
	For Local E:Enemy=EachIn Enemy.Enemy_List
		E.Update()
	Next 
    
    ' update player
	For Local f:Player=EachIn Player.Player_List
		f.Update()
	Next
	
	' update shots
	For Local g:Shot=EachIn Shot.shot_list
		g.Update()
	Next
	      
	 ' update fragments
	For Local k:fragment=EachIn fragment.fragment_list
		k.Update()
	Next 
	
	' update stazrs
	For Local l:Star=EachIn Star.Star_list
		l.Update()
	Next
	 
	
	' set the cycle timer +1
	If cycletimer<10
	cycletimer:+1
	Else 
	cycletimer=0
	EndIf
	
		rings_position:*1.03

' draw the movement lines
	If rings_position>240 
		rings_position=1
	EndIf




	

	Flip
	

Wend 

    Wend

I hate to throw that whole thing at you at once but now it's doing it again, I think it's happening in the lower while loops.

the global game_state value is first set to 0

then in the firsst while loop it execuces code untill the player presses a button which sets game_state to 1. This should exit the first while loop and move to the second while loop. But it never does and it says there is a memory error and shuts down.


Dreamora(Posted 2006) [#2]
1. Stop posting code questions if you are unable to use [ code ] or [ codebox ] tags ... No one is wasting its time to reformat your mess (I'm sorry, thats what it actually is)

2. Don't post anything unless you tested it with strict. In most cases its caused by wrongly written variables that are automatically intialized with 0.
Especially when using local, you must use it, otherwise variable scopes do NOT work (ie a variable within a loop is not local to that loop only *unless this changed in some new version since 1.12*)

3. On your problem: Where are start_button_x and _y defined? perhaps you just compare to the wrong position ... like: you did not try in the top left corner, where you should for a test in [0,0] - [48,48] box.

And you don't need to use =true ... either it is or is not ... :-) (if true then just mousedown(mouse_left) else not mousedown(mouse_left) )


Sin of Nature(Posted 2006) [#3]
A little harsh Dreamora. For a list of commands (including the [code] tags Dreamora was talking about, use this link:

www.blitzbasic.com/faq/faq_entry.php?id=2

I've been looking for that [code] tag for a while. The list of command is on the FAQ on the main www.blitzbasic.com homepage not anywhere on the forum.

Sin


Ryan Burnside(Posted 2006) [#4]
Oh thanks! I just don't know the code for the forum commands.

I'm using the BLide compiler:


Maybe the prolem lies in vars, I still feel like I don't know enough about bmax, do I need to declare every var as local or global? The while loops work in a small demo I made but it doesn't work in my game which is getting to be like spagetti code. I noticed i strict mode everything is going wrong, which mean's i have been using horrid syntax and poor declarations. Hopefully I can find the problem, everything was going great until now. I'm really not used to BASIC, i do use my TI-83 calc but this is very new to me syntax wise. I kind of miss all those curly braces.


FlameDuck(Posted 2006) [#5]
Okay to add to what Dreamora said:

1) Please provide examples that can compile (add a link to any includes or media you're using).
2) Please use indentation consistently.

Anyway, I don't see anything obviously wrong with your code (but I can't compile it so it's hard to say for sure). In any case it looks to me like you have DebugMode switched off? Try enabling it, and see if you don't get a more meaningful error.


Ryan Burnside(Posted 2006) [#6]
Ok I'll upload all dependant files

here is the folder
http://s6.exoload.com/uploads/506/1148423329.zip

Thanks for your help, I know n00bs are annoying, but I have to start somewhere...


QuietBloke(Posted 2006) [#7]
I had a quick look at your code but I cant really work out what it is doing.
In the first loop you are creating the buttons... but you are creating buttons in every frame and then cycling through the fragment_list to display them.
Well... if Create is creating a new item in the list then surely every frame is creating 3 more fragments.
Or have I missed something and the fragment_list is being cleared down every frame ?


Dreamora(Posted 2006) [#8]
The error is in Shot.Update, as debugger says.

Further checking the debugger data shows, that size in Draw is over 200 millions
The x position is something like - 250million and other things like that.

Hope that helps you to find out where to start :)


REDi(Posted 2006) [#9]
Tip of the day :)

Run in debug mode, when you get the exception error click ok, then double click on the last item in the debug window, it should show you the line where the error happend.


Ryan Burnside(Posted 2006) [#10]
No there are no button objects, they are just place values.

I'll have to look into the rest thanks for now guys!


FlameDuck(Posted 2006) [#11]
Here is the problem (in TPlayer.bmx):
	Function Create(x,y,angle=90)
	
		Local temp_player:Player= New Player
			temp_player.x=400+Tan(270)*240
			temp_player.y=240+Cos(270)*240
			temp_player.angle=angle
		
		ListAddLast(Player_List,temp_player)
		instance_count:+1
	EndFunction
the fix:
	Function Create(x,y,angle=90)
	
		Local temp_player:Player= New Player
			temp_player.x=400+Sin(270)*240
			temp_player.y=240+Cos(270)*240
			temp_player.angle=angle
		
		ListAddLast(Player_List,temp_player)
		instance_count:+1
	EndFunction
Nice work BTW, for some reason it doesn't quit here when you hit escape tho'.

I know n00bs are annoying
No they aren't. They give the rest of us an oppertunity to look big and clever.