Array Sizing

BlitzMax Forums/BlitzMax Beginners Area/Array Sizing

I just realized you can only slice one dimensional arrays, so what I'm wondering is how can I size my array to the width/height of my loaded map. Is it possible to declare a global array and then define it's size later? Can you release an existing array and then redim it inside a function? My current method makes a huge array to accommodate any map, and this wastes a lot of memory. Any suggestions welcome.

Global Map:Tile[1000, 1000, 2]

Function LoadMap()

' I now know map width + height. How can I make a global array of this size now

End Function


you could use an array of arrays

You could use an array of arrays.
You can then resize the main array and you can resize all subarrays all independently.

Global Map$[][]

Function ResizeArray(width, height)
	' Resize the main array (which is used here as the width-index)
	Map = Map[..width]

	' Resize all subarrays (each index of the main array points to a different subarray)
	For i = 0 Until width ' "Until" is used, because you have index "0" to index "width - 1" (0-based arrays)
		Map[i] = Map[i][..height]
	Next
End Function

Function PrintArraySize()
	Print Map.length ' Print the total width of the array ("0" - "width-1")

	Print Map[0].length ' Print the total height of the array ("0" - "height-1")
End Function

ResizeArray(640, 480)
PrintArraySize()

' To put something inside the array at x = 20 and y = 50, do this:

Map[20][50] = "This is arrayindex 20, 50"
Print Map[20][50]


Note that I've used a string-array here, but you can use any type you want (replace "Global Map$" by "Global Map:Tile").

do you mean something like that?
Global Map:Tile[,,]

Function LoadMap()

' I now know map width + height. How can I make a global array of this size now
  Map = new Tile[width, height, 2]
End Function


Thanks for the help everyone. I finally understood the concept thanks to PowerPC's excellent example. Here is the final code incase anyone else needs to size a 3 dimensional array of types.
Type Tile
	Field txt$
End Type

Global Map:Tile[][][]

ResizeArray(5, 5, 2)
Map[4][4][0] = New Tile; Map[4][4][1] = New Tile
Map[4][4][0].txt$ = "Hello"
Map[4][4][1].txt$ = "Hello again"

PrintArraySize()
Print Map[4][4][0].txt$
Print Map[4][4][1].txt$
WaitKey()

Function ResizeArray(width, height, f)
	Map = Map[..width]

	For i = 0 Until width
		Map[i] = Map[i][..height]
	Next

	For i = 0 Until width
		For b = 0 Until height
			Map[i][b] = Map[i][b][..f]
		Next
	Next
End Function

Function PrintArraySize()
	Print Map.length
	Print Map[0].length
	Print Map[0][0].length

End Function