TList

BlitzMax Forums/BlitzMax Beginners Area/TList

(tu) ENAY(Posted 2005) [#1]
Ok, I've got an object created but I can't create two Old Skool Blitz style.

I'm guessing I need to use a Tlist. But what I don't understand is the way you can create one type in a variable without needing a list.

Have a Pat Butcher's:-

Type Vector2D
	Field _x:Int	= 0
	Field _y:Int	= 0
End Type

Global MyVector2D:Vector2D = New Vector2D

MyVector2D._x = 50
MyVector2D._y = 70

MyVector2D:Vector2D = New Vector2D

MyVector2D._x = 10
MyVector2D._y = 90

FUnction Render()
	DrawText "TEST",50,50
	
	Local p:Int = 0
	
	For MyVector2D:Vector2D = EachIn Vector2D
		DrawText MyVector2D._x,50, 80+p
		DrawText MyVector2D._y,50, 90+p
		
		p :+ 50
	Next
End Function



tonyg(Posted 2005) [#2]
You can access the type instance directly using it's handle variable. However, once you create another type instance using the same handle you will lose contact with the first. Same thing happened in BB except they were automatically added to a list you could loop through.
Bmax doesn't do the auto-add to a list so you have to do it yourself. Advantage is you can add the same type instance to numerous lists.
<edit> Just in case it's needed. Using your code...
Type Vector2D
	Field _x:Int	= 0
	Field _y:Int	= 0
End Type
Global myList:TList=CreateList()
MyVector2D:Vector2D = New Vector2D
	MyVector2D._x = 50
	MyVector2D._y = 70
	ListAddLast mylist,myvector2d
MyVector2D:Vector2D = New Vector2D
	MyVector2D._x = 10
	MyVector2D._y = 90
        ListAddLast mylist,myvector2d
Graphics 640,480

render()
Flip
WaitKey()

Function Render()
	DrawText "TEST",50,50
	
	Local p:Int = 0
	
	For MyVector2D:Vector2D = EachIn mylist
		DrawText MyVector2D._x,50, 80+p
		DrawText MyVector2D._y,50, 90+p
		
		p :+ 50
	Next
End Function



SillyPutty(Posted 2005) [#3]
you must add them to a list that you create

first create a vector list
then add each vector to it

there is no global list as in b3d :)


fredborg(Posted 2005) [#4]
This I have found is the easiest way to emulate oldschool Blitz behaviour:
Type Vector2D
	Global _list:TList

	Field _x:Int	= 0
	Field _y:Int	= 0
	
	'
	' This adds a new Vector2D to the global list Vector2D._list
	Method New()
		If Vector2D._list = Null
			Vector2D._list = New TList
		EndIf
		Vector2D._list.addlast(Self)
	EndMethod
	
	'
	' Removes this instance from the list
	Method Destroy()
		Vector2D._list.remove(Self)
	EndMethod
	
	'
	' Removes all instances
	Function DestroyEach()
		Vector2D._list.clear()
	EndFunction
	
End Type

Graphics 640,480,0,0

MyVector2D:Vector2D = New Vector2D
MyVector2D._x = 50
MyVector2D._y = 70

MyVector2D:Vector2D = New Vector2D
MyVector2D._x = 10
MyVector2D._y = 90

While Not KeyHit(KEY_ESCAPE)

	'
	' A temporary Vector2D	
	MyVector2D:Vector2D = New Vector2D
	MyVector2D._x = Rand(100)
	MyVector2D._y = Rand(100)
	
	Render()

	'
	' Same as Delete MyVector2D in Blitz2D/3D/+
	MyVector2D.Destroy()

	FlushMem

Wend

'
' Same as Delete Each Vector2D in
Vector2D.DestroyEach()

End

Function Render()

	Cls

	DrawText "TEST",50,50
	
	Local p:Int = 0
	
	For MyVector2D:Vector2D = EachIn Vector2D._list
		DrawText MyVector2D._x,50, 80+p
		DrawText MyVector2D._y,50, 90+p
		
		p :+ 50
	Next
	
	Flip
		
End Function



(tu) ENAY(Posted 2005) [#5]
Cheers guys. THat really helps. :) Fredborg even better, your code rules!

<ctrl+c><ctrl+v>