Extend Array?

BlitzMax Forums/BlitzMax Programming/Extend Array?

Grey Alien(Posted 2006) [#1]
I can't really think of a way to do this but is there a TArray object you could extend? I don't think there is.

Why? Well you could extend it and make your array handle a specific Type. It could have a method to initialise the array so it wasn't null pointers, and a method to clean it out, and a method to add new objects to it etc. Yet you could still access it like MyArray[0].X or whatever. That would be nice. This is possible in C++ with overloading of course, you specify what the square brackets do.

I know I could embed the array in a type (wrap it) but then I won't be able to access it with [].

Yes I know I've just described the functionality of a TList (sort of), but I really wanted an array for speed and *reuseability* etc. I'm fed up of having to keep writing the same type of functions to handle arrays. Oh well. Just rambling...


Grey Alien(Posted 2006) [#2]
I'd like to bump this to see if anyone has any bright ideas. Thanks.


Dreamora(Posted 2006) [#3]
What is the actual question?

You already seem know the type "Array" which is used and there isn't anything else than that and writting it directly in C / C++ code to be imported as BM does it ...


Grey Alien(Posted 2006) [#4]
so basically you can't extend array and add in your own methods in BMax? OK thanks.


Damien Sturdy(Posted 2006) [#5]
No set way, no. But you could write the array to a bank, re-dim the array and then fill it up again :) By the sounds of it this is exactly what you wanted to avoid though?


Grey Alien(Posted 2006) [#6]
yeah I just want to use an array like an array but with some extra cool methods on it to clear it, add stuff to it etc. Oh and for reuse as well.


Kurator(Posted 2006) [#7]
Is this some kind of "embeddig an array in a type" you mentioned in your first posting?

Type TField
	Field image:TImage
	Field value:Short
	Field owner:Short
	Field cost:Short
End Type

Type TTileMap
	Field data:TField[,]		' 2 Dimensionales Array
	Field dim_x:Int
	Field dim_y:Int

	Method Create(xdim%,ydim%)
		data = New TField[xdim+1,ydim+1]
		For Local y% = 0 To xdim
			For Local x% = 0 To ydim
				data[x%,y%] = New TField
			Next
		Next
		dim_x=xdim%
		dim_y=ydim%
	End Method
	
	Method Draw(x_start:Int, y_start:Int, x_tiles:Short, y_tiles:Short)
		For Local y%=y_start To y_start+y_tiles
			For Local x%=x_start To x_start+x_tiles
				If data[x%,y%].image <> Null
					DrawImage(data[x%,y%].image, x%*64, y%*64,0)
				EndIf
			Next
		Next
	End Method

End Type



Grey Alien(Posted 2006) [#8]
Yes. It's pretty much the only solution I could do. Oh well.