Need help with lists within types

BlitzMax Forums/BlitzMax Programming/Need help with lists within types

The r0nin(Posted 2006) [#1]
I have a particular problem with items that have references to other items imbedded in an internal list. For example, lets say I have four points (of type TPoint). One of those points is linked by a chain to the other three, and the chains have various qualities (like length, strength, etc.).

So I create a Type TChain and give it the qualities necessary. I add it to a ChainList:Tlist Field in the TPoint type. Yet, when I iterate through the ChainList for each point, looking for the other points that are chained, I get nothing. The Following sample should show what I'm trying to do.

SuperStrict

Type TChain
	Field Size:Int
	Field Weight:Int
	Field LinkedPoint:TPoint
End Type

Type TPoint
	Field Name:String
	Field x:Int,y:Int
	Field ChainList:TList = New TList
	
	
	Method makeChain(Point_in:TPoint,size_in:Int,Weight_in:Int)
	
		Local TempChain:TChain = New TChain
		tempChain.size = size_in
		tempChain.weight = weight_in
		tempChain.LinkedPoint = Point_in
		self.ChainList.addlast(Point_in)
	
	End Method
	
	Method CheckChains()
	
		For Local temp:TChain = EachIn self.ChainList
			Print temp.LinkedPoint.name
		Next
	
	EndMethod
	
	
EndType

Local Point1:TPoint = New TPoint
	Point1.name = "Point #1"
Local Point2:TPoint = New TPoint
	Point2.name = "Point #2"
Local Point3:TPoint = New TPoint
	Point2.name = "Point #3"

Point1.makeChain(Point2,1,20)
Point1.makeChain(Point3,2,40)

Point1.checkChains()



I originally tried doing this with an array, but it became ugly very quickly... so Tlists seem to be the best option. What am I missing?


Dreamora(Posted 2006) [#2]
the mainproblem from what I see is that you add point_in to the chain insted of tempChain


The r0nin(Posted 2006) [#3]
point_in goes into the tempchain. That way, when needed, I can get the info (such as location, etc.) out of the stored point without having to For-Eachin or otherwise loop through all of the points. For example. Point1 and point2 are connected by a chain. Point1 has a list of all is chains, and each chain stores where its other endpoint is. So Point1's chain would have point2 in its .Linkedto field. That way I can iterate through Point1's Chainlist to find all the points it is attached to, then through Point2's Chainlist to find what it is attached to, etc.


Dreamora(Posted 2006) [#4]
and wherefor do you create tempChain then? as it isn't use anywhere it will simply be destroyed at the end of the function.

I think you have a very elemental missdesign in there as it does not do what you say it should in your description. It does not add the point 2 to point 1 chain list and then recursively build that one up ...

It adds all to point1s chain and thats it ... there is no chain for point2 ...

What you would need is a single reference that points to then next chain element and the checkchain then would go through that "self created" linked list until there is no next chain element anymore.


The r0nin(Posted 2006) [#5]
Ooops. You are correct, in that I have a typo on the function. This:
	Method makeChain(Point_in:TPoint,size_in:Int,Weight_in:Int)
	
		Local TempChain:TChain = New TChain
		tempChain.size = size_in
		tempChain.weight = weight_in
		tempChain.LinkedPoint = Point_in
		self.ChainList.addlast(Point_in)
	
	End Method

Should be:
	Method makeChain(Point_in:TPoint,size_in:Int,Weight_in:Int)
	
		Local TempChain:TChain = New TChain
		tempChain.size = size_in
		tempChain.weight = weight_in
		tempChain.LinkedPoint = Point_in
		self.ChainList.addlast(tempChain)
                DebugLog "Added Chain"

	
	End Method


However, when you run it, the program only prints "Point #3" when both chains have been added to the list (it should print out points 2&3). What's wrong with the loop to print it out?


The r0nin(Posted 2006) [#6]
OK, I got it to work (I had another typo... Point #3 never got named). So I'm even more confused... because this works here, but not in my larger program.

I may have to go back to arrays...