array of type bad code !

BlitzMax Forums/BlitzMax Beginners Area/array of type bad code !

hub(Posted 2006) [#1]
Could you help me to understand what's bad with this code ? Thanks !

My goal is to replace list by arrays into my game to access quickly and directly to an occurence (avoid a lot of 'for-eachin MyList-next loops) !

Strict

Global Rect_list : TList = CreateList()

Type TRect

	Field Id : Int
	Field x : Int
	Field y : Int
	Field r : Int
	Field g : Int
	Field b : Int
	
	Function Create:TRect (id, x,y)
	
		Local r:TRect = New TRect
		
		r.Id = Id
		r.x = x
		r.y = y
		
		r.r = Rand (0,255)
		r.g = Rand (0,255)
		r.b = Rand (0,255)
		
		ListAddLast Rect_list, r
		
	End Function
	
	Function Draw_all_with_list ()
	
		For Local r:TRect = EachIn Rect_list
		
			SetColor r.r,r.g,r.b
			DrawRect r.x, r.y, 32,32
			
		Next
		
		SetColor 255,255,255
		
	End Function
	
	Method Draw_it ()
	
			SetColor r,g,b
			DrawRect x, y, 32,32
			SetColor 255,255,255
			
	End Method
	
End Type

Graphics 800,600			

Global array_of_rect : TRect[]
			
Local NbRect = Rand (100,200)

For Local i=1 To NbRect
	
	TRect.Create (i,Rand (0,800),Rand (0,600))
Next

array_of_rect = New TRect[NbRect]

For Local i=0 To NbRect - 1
	array_of_rect[i] = TRect.Create (i,Rand (0,800),Rand (0,600))
Next

While Not KeyDown (KEY_ESCAPE)

	Cls
	
	TRect.Draw_all_with_list()
	
	array_of_rect[15].Draw_it() ' <---- i want display item 15 : ERROR : attempt to access field or null object

	Flip
	
Wend 



hub(Posted 2006) [#2]
oups, corrected myself

	Function Create:TRect (id, x,y)
	
		Local r:TRect = New TRect
		
		r.Id = Id
		r.x = x
		r.y = y
		
		r.r = Rand (0,255)
		r.g = Rand (0,255)
		r.b = Rand (0,255)
		
		ListAddLast Rect_list, r
		
		Return r ' <---- i've forgot this !
		
	End Function



Dreamora(Posted 2006) [#3]
There is no reason this code should not work.
works here without any problem, so you most likely have some other problem. (you are on BM 1.22, right?)


hub(Posted 2006) [#4]
Thanks Dreamora, i've corrected myself the code by adding the 'return r' line ;-)


Dreamora(Posted 2006) [#5]
Ah so that meant that this actually solved the problem. thought you meant it more of a "correction to above code" posting where you forgot to copy / write something.

but none the less: good it works now


hub(Posted 2006) [#6]
but i don't understand why the following code doesn't work.

Strict

Global array_of_rect : TRect[]

Type TRect

	Field Id : Int
	Field x : Int
	Field y : Int
	Field r : Int
	Field g : Int
	Field b : Int
	
	Function Create:TRect (id, x,y)
	
		Local r:TRect = New TRect
		
		r.Id = Id
		r.x = x
		r.y = y
		
		r.r = Rand (0,255)
		r.g = Rand (0,255)
		r.b = Rand (0,255)
		
		Return r
		
	End Function
	
	Function Draw_all ()
	
		For Local r:TRect = EachIn array_of_rect
		
			SetColor r.r,r.g,r.b
			DrawRect r.x, r.y, 32,32
			
		Next
		
		SetColor 255,255,255
		
	End Function
	
	Method Draw_it ()
	
			SetColor r,g,b
			DrawRect x, y, 32,32
			SetColor 255,255,255
			
	End Method
	
	
End Type

Graphics 800,600			
			

For Local i=0 To 99

	array_of_rect = New TRect[i+1]
	array_of_rect[I] = TRect.Create (i,Rand (0,800),Rand (0,600))
		
Next

'array_of_rect.sort

While Not KeyDown (KEY_ESCAPE)

	Cls
	
	TRect.Draw_all()
	array_of_rect[15].Draw_it()
	
	Flip
	
Wend 



Dreamora(Posted 2006) [#7]
Thats quite easy:

With array_of_rect = new TRect[i+1] you replace the old one, ie all the objects within as well.

What you would need to do there is slicing:

array_of_rect = array_of_rect[..i+1]

BUT: if you raise it by one, this would be the worst idea you could have as it is massively slowing down. If you use slicing, only do it by length*2 and length/2 (as general rule)


tonyg(Posted 2006) [#8]
You have your slice wrong and you're creating 2 new trect each loop.