Multi dimensional arrays

BlitzMax Forums/BlitzMax Programming/Multi dimensional arrays

Hi people. I need to know how to define an array within a type, using a function (within the type) which takes the arrays dimensions which are passed to it.


Type gameBoard

	Global x,y,z,width,height,trans;
	Global layer;
	Global data[1,1];  <-----THIS IS THE ARRAY I WOULD LIKE TO SPECIFY THE SIZE OF FROM CALLING A FUNCTION

	Global cellWidth = 10;
	Global cellHeight = 10;
	
		
	Function createBoard:gameBoard(getWidth,getHeight)
		layer:+1;
		width = getWidth;
		height = getHeight;
		data=data[getWidth,getHeight]	
		'use slice to resize array
		Return New gameBoard;
	End Function
	
	Method drawBoard()
		'For Local repx=EachIn data
		For repx = 0 To (width-1)
			For repy = 0 To (height-1)
				xpixel = repx*cellWidth;
				ypixel = repy*cellHeight;
				data[repx,repy] = 1;
				If data[repx,repy] = 1 Then DrawOval(x+xpixel, y+ypixel, cellWidth, cellHeight);
			Next
		Next
	End Method
	
End Type




I would like to know how to accomplish this without using lists :)

Thankyou for any help

field data:Int [,]     'Or Global
...
...
gameboard:data = new Int [getWidth,getHeight]
or
Field data:Int [][]    'Or Global
...
...
gameboard:data = New Int [getWidth][GetHeight]
(But wait till someone who knows what they are talking about posts ;)

Edit: Ash, arent you normaly a Blitz3d poster?

Type TGameBoard

	Global x,y,z,width,height,trans
	Global layer
	Global data:Int[][] '  <-----THIS IS THE ARRAY I WOULD LIKE To SPECIFY THE SIZE OF FROM CALLING A Function

	Global cellWidth = 10
	Global cellHeight = 10
	
		
	Function Create:TGameBoard(Width,Height)
		Local tempGameBoard:TGameBoard = New TGameBoard
		layer:+1
		'width = getWidth
		'height = getHeight
		
			tempGameBoard.width = Width
			tempGameBoard.Height = Height
			
			tempGameBoard.data = tempGameBoard.data[..Width]
			For Local i:Int = 0 Until Len(tempGameBoard.data)
				tempGameBoard.data[i] = tempGameBoard.data[i][..Height]
			Next
		
		'use slice to resize array
		Return tempGameBoard
	End Function
	
	Method drawBoard()
		'For Local repx=EachIn data
		For repx = 0 To (width-1)
			For repy = 0 To (height-1)
				xpixel = repx*cellWidth
				ypixel = repy*cellHeight
				data[repx][repy] = 1
				If data[repx][repy] = 1 Then DrawOval(x+xpixel, y+ypixel, cellWidth, cellHeight)
			Next
		Next
	End Method
	
End Type

Local t:TGameBoard = TGameBoard.Create(10,20)

Graphics 640,480,0,0

While Not KeyDown(KEY_ESCAPE)
Cls
t.drawBoard()
Flip
Wend
End


You can't resize a multi-dimensional array with slices, but you can use an array of arrays for this. (a 1D array of 1D arrays).

Type TGameBoard

	Global x,y,z,width,height,trans
	Global layer
	Global data:Int[1,1] '  <-----THIS IS THE ARRAY I WOULD LIKE To SPECIFY THE SIZE OF FROM CALLING A Function

	Global cellWidth = 10
	Global cellHeight = 10
	
		
	Function Create:TGameBoard(Width,Height)
		Local tempGameBoard:TGameBoard = New TGameBoard
		layer:+1
			Local tempArray:Int[Width,Height]
		
			tempGameBoard.width = Width
			tempGameBoard.Height = Height
			tempGameBoard.data = tempArray

		Return tempGameBoard
	End Function
	
	Method drawBoard()
		'For Local repx=EachIn data
		For repx = 0 To (width-1)
			For repy = 0 To (height-1)
				xpixel = (repx*cellWidth)+(10*repx)
				ypixel = (repy*cellHeight)+(10*repy)
				data[repx,repy] = 1
				If data[repx,repy] = 1 Then DrawOval(x+xpixel, y+ypixel, cellWidth, cellHeight)
			Next
		Next
	End Method
	
End Type

Local t:TGameBoard = TGameBoard.Create(20,20)

Graphics 640,480,0,0

While Not KeyDown(KEY_ESCAPE)
Cls
t.drawBoard()
Flip
Wend
End


Or you can re-assign the array via a temp one when creating.

Thanks Perturbatio :o) But H&K's method worked fine. Thankyou H&K and thank you Perturbatio for your help. I really do appriciate it :o)

Thanks Perturbatio :o) But H&K's method worked fine. Thankyou H&K and thank you Perturbatio for your help. I really do appriciate it :o)


Yeah, I really should refresh the page before replying to threads :)