Keeping Track of Instances

BlitzMax Forums/BlitzMax Beginners Area/Keeping Track of Instances

What methods are people using to keep track of all their different object instances?

For example when a player fires a bullet instance, how do u track the number of bullets flying around? and when one is destroyed?

I was thinking of a variable size array! are these possible in BMax?

If it is possible, how would you re-organise the array when one is destroyed? Say for example u have 3 bullets, bullet[0], bullet[1], bullet[2]. Now assume bullet[1] is destroyed, you would want to move bullet[2] in to bullet[1] wouldn't you??

I would recommend a linked list.

The linked list has the advantage of never being smaller or larger than the amount of active bullets.
Insertion and deletion of bullets is fast as well.

Just iterate through the list to check if each bullet are colliding and should be removed from the list.

Tlist are really the best method for keep track of bullets

Graphics 1024,768,32

Global Bullet_List:TList=CreateList()

Type Bullet
	Field XCoor#
	Field YCoor#
	Field Speed#
	
	Method Move_bullet()
		Xcoor#:+Speed#
	End Method
	
	Method Draw_Bullet()
		DrawOval(Xcoor#,Ycoor#,10,10)
	End Method
	
	Method Check_Dead()
		If xcoor#>1024+10 Then
		ListRemove Bullet_list,Self
		EndIf
	End Method
	
End Type

While Not KeyHit(Key_Escape)
Update_Bullets()
If KeyHit(KEY_SPACE) Then Create_Bullets


Flip
FlushMem
Cls
Wend

Function Create_Bullets()
Bull:Bullet=New Bullet
Bull.Xcoor#=Rnd(100)+100
Bull.Ycoor#=Rnd(700)+50
Bull.Speed#=(Rnd(20)+1)/10
ListAddLast Bullet_List,bull
End Function

Function Update_Bullets()
For Bull:Bullet=EachIn Bullet_List
Bull.Move_Bullet
Bull.Draw_bullet
Bull.Check_Dead
Total:+1
Next
DrawText ("Total Bullets="+Total,50,50)
End Function


Better yet:

Type Bullet
	Global List:TList
	Field X#,Y#
	Field XV#,YV#

	Method Move()
		X+:XV
		Y+:XY
	EndMethod

	Method Draw()
		DrawOval(x,y,10,10)
	EndMethod

	Method New()	'Automatically called
		List.AddLast(Self)
	EndMethod
EndType

Bullet.List=New TList
'etc etc


duckstab: http://www.blitzbasic.com/Community/posts.php?topic=42822

Can See how that improves the coding God now ive good 2000 lines of code to sort

Bugger :)

I use

CountList(List:Tlist)