Adding standard datatypes to a TList

BlitzMax Forums/BlitzMax Beginners Area/Adding standard datatypes to a TList

monotonic(Posted 2008) [#1]
Hi,

I have just tried creating a TList then adding a list of floating point variables to it. When I came to compile, it gave an error saying 'Unable to convert from Float to Object'. Now, I know I could solve this by grouping some of my float variables into a type then adding the type objects, but this seems counter productive.

Is there another way of doing this, or do I have to jump through the hoop on this one?

Here is my code:
' this function builds a static tri mesh from an entity
' PARAMS:
'		entity - the entity to build the tri mesh from
'		ode_space - the ODE space to add this tri mesh to
Function BuildTriMesh:Int(entity:Int, ode_space:Int) 
	
	' locals
	Local vert_list:TList = New TList			' this is a temp list to store vertices
	Local tri_list:TList = New TList			' this is a temp list to store triangle vertex indices
	Local vert_bank:TBank						' the actual vertex bank
	Local tri_bank:TBank						' the actual index bank
	Local vertex_index:Int						' this is used to hold vertex indices
	Local Offset:Int = 0						' the vertex offset
	Local TriOffset:Int = 0 					' the triangle offset
	
	' cycle through all of the surfaces
	For Local surf:Int = 1 To bbCountSurfaces(entity) 
		
		' cycle through each triangle in this surface
		For Local tri:Int = 1 To bbCountTriangles(surf) 
			
			' add all three vertices and indices to the list
			For Local vert:Int = 0 To 2
				vertex_index = bbTriangleVertex(surf, tri, vert) 
				vert_list.AddLast(bbVertexX(surf, vertex_index)) 
				vert_list.AddLast(bbVertexY(surf, vertex_index)) 
				vert_list.AddLast(bbVertexZ(surf, vertex_index)) 
				vert_list.AddLast(Float(0.0)) 
			TList
				' add the vertex index to the list
				tri_list.AddLast(vert_list.count() - 1) 
			Next
		
		Next
	
	Next
	
	
	' now create the banks
	vert_bank = CreateBank(vert_list.count() * 4 * 4) 
	tri_bank = CreateBank(tri_list.count() * 3 * 4) 
	
	' the vertex bank
	For Local v:Int = 0 To vert_list.count() - 1
		PokeFloat(vert_bank, Offset, vert_list[v] ) 
		Offset:+4
	Next

	' the triangle bank
	For Local t:Int = 0 To tri_list.count() - 1
		PokeInt(tri_bank, TriOffset, tri_list[t] ) 
		TriOffset:+4
	Next
	
	
	' now create the tri mesh in ODE
	Local TriMeshData:Int = dGeomTriMeshDataCreate() 
	dGeomTriMeshDataBuildSimple(TriMeshData, BankBuf(vert_bank), vert_list.count(), BankBuf(tri_bank), tri_list.count()) 
	
	' return the tri mesh
	Return dCreateTriMesh(ode_space, TriMeshData) 

End Function


Thanks in advance.


SebHoll(Posted 2008) [#2]
The only way you can store a group of Ints/Floats/Doubles directly is by using an array - lists can only store Objects, and as the primitive types listed aren't Objects, you can't store them in a list (atleast, not directly).

However, you can store them if you convert them to an Object before adding them to the list, and the quickest way to do this is to convert them into strings (which are objects) just before adding them. For example,

Local tmpFloat:Float, myList:TList = New TList
myList.AddLast String(tmpFloat)
If myList.Contains(String(tmpFloat)) Then Print "Found!" Else Print "Error!"
Obviously, when looping through the items, you'll have to cast them back to Floats again (if you want to use them in calculations).


monotonic(Posted 2008) [#3]
Ahh, ok that makes sense. I am used to programming in C# where everything is an object.

Thanks for that.


Dreamora(Posted 2008) [#4]
The alternative approach are TInt, TFloat and TDouble wrapper classes. For simple decision what it contains and the like, this is the favorable approach. As well then you could use reflection to find out what it is.