TList Help

BlitzMax Forums/BlitzMax Beginners Area/TList Help

Steve The Rock(Posted 2008) [#1]
Hi guys. I’m trying to get my head around TLists and I think I’ve got the basics. But I’m not sure how to handle one object in the list or even a range of objects? Here is my example, this code creates 1000 particles on the screen and draws a line from point 500, 500 to each one. You can use the arrow keys to move the particles left and right. My question is how I edit my code so I can draw a line to just particle 100 or a range of particles 100 to 200. Maybe even use the up and down arrow keys to select a range. Hope you can help me!

' Adding objects to an object-specific list...
Global sp

Type Particle

	Global ParticleList:TList ' The list for all objects of this type...

	Field x#
	Field y#

	' The New method is called whenever one of these objects is created. If
	' the list hasn't yet been created, it's created here. The object is then
	' added to the list...

	Method New ()
		If ParticleList = Null
		ParticleList = New TList
		EndIf
		ParticleList.AddLast Self
	End Method

	Function Create:Particle ()
		p:Particle = New Particle
		p.x = Rnd (1000)
		p.y = Rnd (1000)
	End Function

	Function UpdateAll ()

	' Better check the list exists before trying to use it...

	If ParticleList = Null Return

	' Run through list...

	For p:Particle = EachIn ParticleList
		p.x=p.x+sp
		DrawLine 500,500,p.x,p.y
		DrawRect p.x, p.y,8,8
		If p.y > GraphicsHeight () p = Null
		Next

	End Function

End Type

Graphics 1024, 768

' Create 1000 Particles...
For loop=1 To 1000
p:Particle = Particle.Create ()
Next

Repeat

Cls
' Update all Particle objects...
Particle.UpdateAll ()

If KeyDown(KEY_RIGHT) Then sp=sp+1
If KeyDown(KEY_LEFT) Then sp=sp-1
If sp>5 Then sp=5
If sp<-5 Then sp=-5

Flip
Until KeyHit (KEY_ESCAPE)
End



Rimmsy(Posted 2008) [#2]
You can use ValueAtIndex(index) to get the particle at the relevant... index.

for local i=1 to 100 step 2
local p:Particle=Particle(particleList.valueAtIndex(i))
local nextP:Particle=Particle(particleList.valueAtIndex(i+1))
   DrawLine p.x,p.y,    nextP.x,nextP.y
next


Obviously there are downsides. If you wantfull control then you can number each particle and run through the list as you would normally, extracting the particle ID. This way is fast, mind. Is this what you were after?


GfK(Posted 2008) [#3]
ValueAtIndex() iterates through the list, counting as it goes. Meaning, its slow with big lists and frequent use.

If you want to regularly access the Nth particle of a thousand etc, you're better off using an array of your Particle objects.


Steve The Rock(Posted 2008) [#4]
Cool Ty for your help guys