Missing List?

BlitzMax Forums/BlitzMax Beginners Area/Missing List?

pc_tek(Posted 2011) [#1]
I am obviously missing a key step here...

Global Ball_List:TList=CreateList()

Type Ball
	Field x:Int
	Function Create:Ball()
	   Local b:Ball = New Ball
		b.x=Rand(0,20)
	EndFunction
End Type


Ball_List.AddLast(Ball.Create())
While Not KeyDown(KEY_ESCAPE)
	PROCESS_Balls
Wend

Function PROCESS_Balls()
	Local b:ball
	For b = EachIn Ball_List
		DebugLog "HERE"
	Next
End Function


The debuglog shows no program output at all. Whyyyyyyyyy?


Jesse(Posted 2011) [#2]
you need to return the ball instance on the create function.


pc_tek(Posted 2011) [#3]
Cheers Jesse!


Czar Flavius(Posted 2011) [#4]
This is something I would like the compiler to check for, like in C++! If the return type is specified, the function must have at least one return statement and not assume null. I've made the same error pc_tek, it's an annoying one!


pc_tek(Posted 2011) [#5]
I need to pin this down. The code now works to add an item to the list, but how do you remove an item?

I have looked at a lot of examples on here and found them unhelpful. How can I use my code (on this post at the top) and remove an item?


Htbaa(Posted 2011) [#6]
Doesn't using Strict or SuperStrict catch these errors (about a method not returning a value, when its signature says it should)?

For removing, lookup the TList documentation.


Czar Flavius(Posted 2011) [#7]
When you add an object to a list, it returns a link which is a bookmark into its position in the list. If you call remove on this link, it removes the object straight from the list. If you use the remove function on the list, and give it the object, it must iterate through the whole list until it finds the object. This is slow. The best way is to store the link when you add it to a list, so you can remove it quickly later.

Usually you have one big list with all your instances. If you need multiple lists with quick removal, that is certainly possible. But this example gets you started on the simple case.

You must remember to explicitly call remove on every ball when you are done with it.

Type TBall
	Global all:TList = New TList
	Field link:TLink
	
	Field x
	
	Method New()
		link = all.AddLast(Self)
	End Method
	
	Method remove()
		link.Remove()
		link = Null 'for safety
	End Method
	
	Function Create:TBall(x)
		Local ball:TBall = New TBall
		ball.x = x
		Return ball
	End Function
End Type

For Local i = 0 To 10
	TBall.Create(i)
Next

For Local ball:TBall = EachIn TBall.all
	ball.remove()
Next



pc_tek(Posted 2011) [#8]
Thanks Flavius....

Just what the Dr ordered!